source: mauRepo/HPFos/trunk/src/HPFos/view/view.py @ 8360

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/HPFos/trunk/src/HPFos/view/view.py@8360
Revision 8360, 4.5 KB checked in by mnagni, 8 years ago (diff)

The _packResult "missed" the first result
The view built a wrong hostURL

  • Property svn:mime-type set to text/plain
Line 
1'''
2BSD Licence
3Copyright (c) 2012, Science & Technology Facilities Council (STFC)
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without modification,
7are 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
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29Created on 1 Nov 2011
30
31@author: Maurizio Nagni
32'''
33from django.shortcuts import render_to_response
34from django.core.context_processors import csrf
35from django.utils.safestring import mark_safe
36from HPFos.settings import os_engine
37from HPFos.osImpl.myimpl import COLLECTION, OBSERVATION, RESULT
38
39hostURL = 'http://localhost:8000'
40
41def _buildHostURL(request):
42    '''
43        Cannot hardcode "HPFos" in the URL. This has to be fixed later
44    '''   
45    if 'localhost' in request.get_host():
46        return 'http://%s' % (request.get_host())
47   
48    if request.is_secure():
49        return 'https://%s/HPFos' % (request.get_host())
50    else:
51        return 'http://%s/HPFos' % (request.get_host())
52
53
54
55
56def getHome(request):
57    context = {}
58    context['hostURL'] = _buildHostURL(request)
59    return _dispatchResponse(request, 'homeTemplate', context)
60
61def getDescription(request, collection_guid = None, observation_guid = None, result_guid = None):
62    hostURL = _buildHostURL(request)
63    ospath = _buildDescriptionOsPath(hostURL, collection_guid, observation_guid, result_guid)
64    response = os_engine.getDescription(ospath)
65    context = {}
66    context['response'] = mark_safe(response)
67    return _dispatchResponse(request, 'responseTemplate', context)
68
69def _doSearch(request, iformat, collection_guid = None, observation_guid = None, result_guid = None):
70    hostURL = _buildHostURL(request)   
71    #params = {'q': ['ice', 'snow']}
72    params = request.GET.copy()
73    if collection_guid:
74        params[COLLECTION] = collection_guid   
75    if observation_guid:
76        params[OBSERVATION] = observation_guid       
77    if result_guid:
78        params[RESULT] = result_guid               
79   
80    response = os_engine.doSearch(hostURL, iformat, params_values = params, moles3EPB = request.moles3EPB)
81    context = {}
82    context['response'] = mark_safe(response)
83    return _dispatchResponse(request, 'responseTemplate', context)
84
85def doSearchL0(request, iformat):
86    return _doSearch(request, iformat)
87
88def doSearchL1(request, collection_guid, iformat):
89    return _doSearch(request, iformat, collection_guid)
90
91def doSearchL2(request, collection_guid, observation_guid, iformat):
92    return _doSearch(request, iformat, collection_guid, observation_guid)
93
94def doSearchL3(request, collection_guid, observation_guid, result_guid, iformat):
95    return _doSearch(request, iformat, collection_guid, observation_guid, result_guid)   
96
97def _buildDescriptionOsPath(hostURL, collection_guid = None, observation_guid = None, result_guid = None):
98    ospath = "%s/search/" % (hostURL)
99    if collection_guid:
100        ospath = "%s%s/" % (ospath, collection_guid)
101    if observation_guid:
102        ospath = "%s%s/" % (ospath, observation_guid)
103    if result_guid:
104        ospath = "%s%s/" % (ospath, result_guid)
105    return ospath       
106       
107def _dispatchResponse(request, template, context):
108    context.update(csrf(request))
109    return render_to_response(template, context)   
Note: See TracBrowser for help on using the repository browser.