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