1 | ''' |
---|
2 | BSD Licence |
---|
3 | Copyright (c) 2012, Science & Technology Facilities Council (STFC) |
---|
4 | All rights reserved. |
---|
5 | |
---|
6 | Redistribution and use in source and binary forms, with or without modification, |
---|
7 | are permitted provided that the following conditions are met: |
---|
8 | |
---|
9 | * Redistributions of source code must retain the above copyright notice, |
---|
10 | this list of conditions and the following disclaimer. |
---|
11 | * Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | this list of conditions and the following disclaimer in the documentation |
---|
13 | and/or other materials provided with the distribution. |
---|
14 | * Neither the name of the Science & Technology Facilities Council (STFC) |
---|
15 | nor the names of its contributors may be used to endorse or promote |
---|
16 | products derived from this software without specific prior written permission. |
---|
17 | |
---|
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
---|
20 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
---|
21 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
---|
22 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
---|
23 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
24 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
25 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
---|
26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
28 | |
---|
29 | Created on 1 Nov 2011 |
---|
30 | |
---|
31 | @author: Maurizio Nagni |
---|
32 | ''' |
---|
33 | from django.shortcuts import render_to_response |
---|
34 | from django.core.context_processors import csrf |
---|
35 | from django.utils.safestring import mark_safe |
---|
36 | from HPFos.settings import os_engine |
---|
37 | from HPFos.osImpl.myimpl import COLLECTION, OBSERVATION, RESULT |
---|
38 | |
---|
39 | hostURL = 'http://localhost:8000' |
---|
40 | |
---|
41 | def _buildHostURL(request): |
---|
42 | if request.is_secure(): |
---|
43 | return 'https://%s' % (request.get_host()) |
---|
44 | else: |
---|
45 | return 'http://%s' % (request.get_host()) |
---|
46 | |
---|
47 | def getHome(request): |
---|
48 | context = {} |
---|
49 | context['hostURL'] = _buildHostURL(request) |
---|
50 | return _dispatchResponse(request, 'homeTemplate', context) |
---|
51 | |
---|
52 | def getDescription(request, collection_guid = None, observation_guid = None, result_guid = None): |
---|
53 | ospath = _buildDescriptionOsPath(collection_guid, observation_guid, result_guid) |
---|
54 | response = os_engine.getDescription(ospath) |
---|
55 | context = {} |
---|
56 | context['response'] = mark_safe(response) |
---|
57 | return _dispatchResponse(request, 'responseTemplate', context) |
---|
58 | |
---|
59 | def _doSearch(request, iformat, collection_guid = None, observation_guid = None, result_guid = None): |
---|
60 | host = _buildHostURL(request) |
---|
61 | #params = {'q': ['ice', 'snow']} |
---|
62 | params = request.GET.copy() |
---|
63 | if collection_guid: |
---|
64 | params[COLLECTION] = collection_guid |
---|
65 | if observation_guid: |
---|
66 | params[OBSERVATION] = observation_guid |
---|
67 | if result_guid: |
---|
68 | params[RESULT] = result_guid |
---|
69 | |
---|
70 | response = os_engine.doSearch(hostURL, iformat, params_values = params, moles3EPB = request.moles3EPB) |
---|
71 | context = {} |
---|
72 | context['response'] = mark_safe(response) |
---|
73 | return _dispatchResponse(request, 'responseTemplate', context) |
---|
74 | |
---|
75 | def doSearchL0(request, iformat): |
---|
76 | return _doSearch(request, iformat) |
---|
77 | |
---|
78 | def doSearchL1(request, collection_guid, iformat): |
---|
79 | return _doSearch(request, iformat, collection_guid) |
---|
80 | |
---|
81 | def doSearchL2(request, collection_guid, observation_guid, iformat): |
---|
82 | return _doSearch(request, iformat, collection_guid, observation_guid) |
---|
83 | |
---|
84 | def doSearchL3(request, collection_guid, observation_guid, result_guid, iformat): |
---|
85 | return _doSearch(request, iformat, collection_guid, observation_guid, result_guid) |
---|
86 | |
---|
87 | def _buildDescriptionOsPath(collection_guid = None, observation_guid = None, result_guid = None): |
---|
88 | ospath = "%s/search/" % (hostURL) |
---|
89 | if collection_guid: |
---|
90 | ospath = "%s%s/" % (ospath, collection_guid) |
---|
91 | if observation_guid: |
---|
92 | ospath = "%s%s/" % (ospath, observation_guid) |
---|
93 | if result_guid: |
---|
94 | ospath = "%s%s/" % (ospath, result_guid) |
---|
95 | return ospath |
---|
96 | |
---|
97 | def _dispatchResponse(request, template, context): |
---|
98 | context.update(csrf(request)) |
---|
99 | return render_to_response(template, context) |
---|