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