format uris from site, node, pcu details
[monitor.git] / web / MonitorWeb / monitorweb / templates / links.py
1 from monitor import config 
2 import urllib
3
4 def plc_node_uri(hostname):
5         return "https://" + config.PLC_WWW_HOSTNAME + "/db/nodes/index.php?nodepattern=" + str(hostname)
6 def plc_site_uri(loginbase):
7         return "https://" + config.PLC_WWW_HOSTNAME + "/db/sites/index.php?site_pattern=" + str(loginbase)
8 def plc_site_uri_id(site_id):
9         return "https://" + config.PLC_WWW_HOSTNAME + "/db/sites/index.php?id=" + str(site_id)
10 def plc_pcu_uri_id(pcu_id):
11         return "https://" + config.PLC_WWW_HOSTNAME + "/db/sites/pcu.php?id=" + str(pcu_id)
12
13
14 def query_to_path(**kwargs):
15         args = []
16         tgpath = ""
17         tgparams = kwargs
18         if tgparams:
19                 for key, value in tgparams.iteritems():
20                         if value is None:
21                                 continue
22                         if isinstance(value, (list, tuple)):
23                                 pairs = [(key, v) for v in value]
24                         else:
25                                 pairs = [(key, value)]
26                         for k, v in pairs:
27                                 if v is None:
28                                         continue
29                                 if isinstance(v, unicode):
30                                         v = v.encode('utf8')
31                                 args.append((k, str(v)))
32         if args:
33                 query_string = urllib.urlencode(args, True)
34                 if '?' in tgpath:
35                         tgpath += '&' + query_string
36                 else:
37                         tgpath += '?' + query_string
38         return tgpath
39
40 def link(base, ext=True, **kwargs):
41         if ext:
42                 str = "?query=" + base + query_to_path(**kwargs)
43         else:
44                 str = tg.url(base, **kwargs)
45         #print "CREATED %s" % str
46         return str