source: ndgCommon/trunk/ndg/common/src/lib/htmlUtilities.py @ 4793

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/src/lib/htmlUtilities.py@4991
Revision 4793, 3.3 KB checked in by cbyrom, 11 years ago (diff)

Checking in initial codebase for ndgUtils restructure.

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