source: MILK/trunk/milk_server/milk_server/lib/htmlUtilities.py @ 4668

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/MILK/trunk/milk_server/milk_server/lib/htmlUtilities.py@4668
Revision 4668, 3.2 KB checked in by cbyrom, 11 years ago (diff)

Refactor to reflect updates to ndgUtils structure.
Pull out various constants from controllers to separate file - so these
can be referenced easily by tests.
Implement test_listatom.py - to test the listatom controller.

Line 
1#
2# This is a collection of template utilities for producing html
3#
4import logging
5
6GRANULE_ASSOCIATION = 1
7DEPLOYMENT_ASSOCIATION = 2
8ENTITY_ASSOCIATION = 3
9
10class 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
21def span(x,id):
22    return '<span class="%s">%s</span>'%(id,x)
23
24def 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   
33def 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
39def 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       
50def 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
59def getVocabTermDataSelectValue(vti):
60    '''
61    Given a vocab term item, return the select value for use in a drop down list
62    @param vti: a VocabTermItem
63    @return: select value - in format, vti.vocabURL--vti.termID
64    '''
65    return "%s--%s" %(vti.vocabURL, vti.termID)
66
67
68def getVocabTermDataDropdown(list, selected=None, defaultVal=None):
69    '''
70    Gets a dropdown option list suitable for use in the h.select helper
71    @param list: list of VocabTermItems
72    @keyword selected: the value currently selected
73    @keyword defaultVal: an additional item to add - to represent a default - e.g.
74    'all' or 'none'
75    '''
76    logging.debug("Setting up drop down list for data")
77    if selected:
78        logging.debug("(current val:'%s')" %selected)
79    r=''
80   
81    if defaultVal:
82        r+='<option value="%s">%s</option>' %(getVocabTermDataSelectValue(defaultVal), \
83                                              defaultVal.title or defaultVal.termID)
84
85    # NB, need to include both vocabURL and term ID for the data to be
86    # meaningful
87    for val in list:
88        default=''
89        # NB, the selected val will be the full, latest term url - so only
90        # compare the start and end for match
91        if selected and selected.endswith(val.termID) and \
92            selected.startswith(val.vocabURL):
93            default='selected="selected"'
94        r+='<option value="%s" %s>%s</option>' %(getVocabTermDataSelectValue(val), \
95                                                 default, val.title or val.termID)
96   
97    logging.debug("- produced drop down contents: '%s'" %r)
98    return r
99   
Note: See TracBrowser for help on using the repository browser.