source: mauRepo/MolesManager/trunk/src/MolesManager/views/cedaSearch.py @ 8430

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/MolesManager/trunk/src/MolesManager/views/cedaSearch.py@8430
Revision 8430, 1.8 KB checked in by mnagni, 7 years ago (diff)

Fixed many problems with the search function. Obviously need to be improved more.

Line 
1'''
2Created on 1 Nov 2011
3
4@author: mnagni
5'''
6from django.core.context_processors import csrf
7from django.shortcuts import render_to_response
8
9"""
10def __getResponseByGuid(request, guid):
11    ceda_guid = Moles3EPB.search(CedaGUID, guid, request.moles_session)
12    if ceda_guid and ceda_guid.ceda_observation:
13        return redirect(cedaObservationView.coView, ceda_guid.ceda_observation)
14"""
15
16def __doSearch(request, keywords):
17    '''
18        @param request: an HttpRequest
19        @param keywords: an list of strings
20        @return: the first Observation or None
21        '''
22    if not keywords.startswith("'"):
23        keywords = "'" + keywords
24    if not keywords.endswith("'"):
25        keywords = keywords + "'"
26    obsevations = request.moles3EPB.extractObservationByTitleKeywords(keywords) 
27    titles = []
28    ids = []
29    list_list = []
30    baseCov = "http://" + request.get_host() + "/cov/"
31    for o in obsevations:
32        a = "".join([baseCov, str(o.id)])
33        responsibleAuthors = request.moles3EPB.observationAuthor(o)
34        list_list.append((a, o.description, _appendAuthors(responsibleAuthors))) 
35    return list_list
36
37def _appendAuthors(party):
38    res = None
39    for item in party:
40         res = ("%s%s,") % (res, item.name)
41    if len(res) > 1:
42        return res[:len(res)-1]
43    return res
44
45
46def coSearch(request):
47    c = {}   
48    if 'searchTerm' in request.POST:
49        c['searchResults'] = __doSearch(request, request.POST['searchTerm'])
50           
51    c.update(csrf(request))
52    if c == {} :
53        return render_to_response('cedaObservation.html', c) 
54    else :
55        found = False
56    try:
57        a = c['searchResults']
58        found = True
59    except Exception, ex:
60        pass
61    if not found:
62            return render_to_response('cedaSearch.html', c)
63    return render_to_response('cedaSearchResults.html', c)
Note: See TracBrowser for help on using the repository browser.