source: MILK/trunk/milk_server/milk_server/lib/Utilities.py @ 5302

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/MILK/trunk/milk_server/milk_server/lib/Utilities.py@5302
Revision 5302, 2.5 KB checked in by cbyrom, 10 years ago (diff)

Simplify structure of discovery controller, using the results template
to render searches even when no results are found. Add new error
dict to avoid confusing with the error dict used by the formencode
input checker. Add additional input checks and improve error handling.
Add dropdown list to select order by values + add new constants
module to store associated data and error keys + tidy up DiscoveryState?
object to make clearer and logic more consistent + fix paging controls
for results data.

Line 
1import urllib, logging
2from milk_server.lib.base import *
3from ndg.common.src.clients.xmldb.eXist.atomclient import AtomClient
4
5def getExistClient(providerID):
6    '''
7    Use the config data to set up and return an eXist client
8    '''
9    logging.info("Setting up eXist client to provider, '%s'" %providerID)
10    # firstly check if there is a current connection available
11    eXistClient = g.eXistDBCons.get(providerID)
12    if eXistClient:
13        logging.info("- found existing client to provider - returning this")
14        return eXistClient
15
16    existHost = request.environ['ndgConfig'].get('NDG_EXIST', providerID)
17    configFile = request.environ['ndgConfig'].get('NDG_EXIST','passwordFile')
18    eXistClient = AtomClient(dbHostName = existHost, configFileName = configFile)
19   
20    # add the client to the global variables for re-use
21    g.eXistDBCons[providerID] = eXistClient
22    logging.info("Returning eXist client")
23    return eXistClient
24
25
26def recreateListFromUnicode(string):
27    ''' Parse a list that has been passed as unicode over http and
28    recreate it in its list form
29    '''
30    if not string:
31        return
32   
33    newList = []
34    element = []
35    for c in string:
36        if c == '[' or c == ' ' or c == ']' or c == '\'':
37            continue
38        elif c == ',':
39            if (len(element) > 0):
40                newList.append(''.join(element))
41                element = []
42            continue
43        element.append(c)
44
45    # don't forget the last element
46    if (len(element) > 0):
47        newList.append(''.join(element))
48       
49    return newList
50
51
52def isTabRequired(pageTabs, string):
53    for tab in pageTabs:
54        if tab[0] == string:
55            return False
56   
57    return True
58       
59
60def urlListEncode(urlList):
61    """
62    Encode a list of URLs so that they can be embedded in another URL
63    """
64    return '|'.join(urllib.quote(x, '%') for x in urlList)
65
66
67def urlListDecode(string):
68    """
69    Decode an encoded list of URLs.
70    """
71    return [urllib.unquote(x) for x in string.split('|')]
72
73
74def getURLConstraints(urlConstraints):
75    '''
76    Given a set of contraints passed as a uri parameter, convert these into
77    a dict and return this, avoiding duplicate entries
78    @return dict with key, val = param, value
79    '''
80    constraints = {}
81    inputs = urlConstraints.split('&')
82    for input in inputs:
83        data = input.split('=')
84        if len(data) > 2:
85            import pdb
86            pdb.set_trace()
87        key, val = input.split('=')
88        if val:
89            constraints[key] = val
90    return constraints
91
Note: See TracBrowser for help on using the repository browser.