1 | # |
---|
2 | # This is a collection of template utilities for producing html |
---|
3 | # |
---|
4 | import logging |
---|
5 | |
---|
6 | GRANULE_ASSOCIATION = 1 |
---|
7 | DEPLOYMENT_ASSOCIATION = 2 |
---|
8 | ENTITY_ASSOCIATION = 3 |
---|
9 | |
---|
10 | class selector: |
---|
11 | ''' Provides a selection icon to add a dataset to a selector box ''' |
---|
12 | def __init__(self,baseURL,arg,icon): |
---|
13 | self.baseURL=baseURL |
---|
14 | self.argument=arg |
---|
15 | self.icon=icon |
---|
16 | def target(self,id,name=None): |
---|
17 | url='%s&%s=%s'%(self.baseURL,self.argument,id) |
---|
18 | if name is not None: url+='&name=%s'%name |
---|
19 | return hyperlink(image(self.icon,self.argument),url) |
---|
20 | |
---|
21 | def span(x,id): |
---|
22 | return '<span class="%s">%s</span>'%(id,x) |
---|
23 | |
---|
24 | def hyperlink(n,u,t=None): |
---|
25 | ''' Make a hyperlink ... provided there is an address to jump to ''' |
---|
26 | if u !='': |
---|
27 | if t is None: |
---|
28 | return '<a href="%s">%s</a>'%(u,n) |
---|
29 | else: |
---|
30 | return '<a href="%s" title="%s">%s</a>'%(u,n,t) |
---|
31 | else: return n |
---|
32 | |
---|
33 | def image(l,a,t=None): |
---|
34 | if t is None: |
---|
35 | return '<img src="%s" alt="%s"/>'%(l,a) |
---|
36 | else: |
---|
37 | return '<img src="%s" alt="%s" title="%s"/>'%(l,a,t) |
---|
38 | |
---|
39 | def abbreviate(string,newlen): |
---|
40 | ''' We want to ensure that a particular block of text does not exceed |
---|
41 | a specific length - and finishes on a word boundary. ''' |
---|
42 | if len(string)< newlen: |
---|
43 | return string |
---|
44 | else: |
---|
45 | r='' |
---|
46 | for w in string[0:newlen].split(' ')[:-1]:r+='%s '%w |
---|
47 | r+=' ...' |
---|
48 | return r |
---|
49 | |
---|
50 | def htmlTime(string): |
---|
51 | ''' Take a yyyy-mm-dd and turn into two line html ''' |
---|
52 | if string in [None,'','Unknown']: return '' |
---|
53 | t=string.split('-') |
---|
54 | if len(t)==3: |
---|
55 | return '%s<br/>%s-%s'%tuple(t) |
---|
56 | else: |
---|
57 | return t |
---|
58 | |
---|
59 | |
---|
60 | def getVocabTermDataDropdown(list, selected=None, defaultVal=None): |
---|
61 | ''' |
---|
62 | Gets a dropdown option list suitable for use in the h.select helper |
---|
63 | @param list: list of VocabTermItems |
---|
64 | @keyword selected: the value currently selected |
---|
65 | @keyword defaultVal: an additional item to add - to represent a default - e.g. |
---|
66 | 'all' or 'none' |
---|
67 | ''' |
---|
68 | logging.debug("Setting up drop down list for data") |
---|
69 | if selected: |
---|
70 | logging.debug("(current val:'%s')" %selected) |
---|
71 | r='' |
---|
72 | |
---|
73 | if defaultVal: |
---|
74 | r+='<option value="%s--%s">%s</option>' %(defaultVal.vocabURL, \ |
---|
75 | defaultVal.termID, defaultVal.title or defaultVal.termID) |
---|
76 | |
---|
77 | # NB, need to include both vocabURL and term ID for the data to be |
---|
78 | # meaningful |
---|
79 | for val in list: |
---|
80 | default='' |
---|
81 | # NB, the selected val will be the full, latest term url - so only |
---|
82 | # compare the start and end for match |
---|
83 | if selected and selected.endswith(val.termID) and \ |
---|
84 | selected.startswith(val.vocabURL): |
---|
85 | default='selected="selected"' |
---|
86 | r+='<option value="%s--%s" %s>%s</option>' %(val.vocabURL, \ |
---|
87 | val.termID, default, val.title or val.termID) |
---|
88 | |
---|
89 | logging.debug("- produced drop down contents: '%s'" %r) |
---|
90 | return r |
---|
91 | |
---|