source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/controllers/selectedItems.py @ 3842

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/controllers/selectedItems.py@3842
Revision 3842, 9.8 KB checked in by spascoe, 12 years ago (diff)

Various changes needed for compatibility with proxy configurations.

The URL encoding of selectItems/addSelectedItems has been overhalled
because it didn't work behind a proxy and had bugs anyway. Relative paths
in kid files have had $g.server added where appropriate.

Line 
1# Copyright (C) 2007 STFC & NERC (Science and Technology Facilities Council).
2# This software may be distributed under the terms of the
3# Q Public License, version 1.0 or later.
4# http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
5#
6"""
7 Controller for the 'Selections' tab - which displays selected WMC/granules and allows
8 these to be displayed via a selection of methods
9
10 @author: C Byrom
11"""
12
13from ows_server.lib.base import *
14from paste.request import parse_querystring
15from ows_server.models import Utilities
16from ows_server.models import selectedItem
17from ows_server.lib.wmc_util import *
18from ows_server.controllers import viewItems
19import copy
20import urllib
21
22import logging
23log = logging.getLogger(__name__)
24
25class SelecteditemsController(BaseController):
26   
27    def index(self):
28        """
29        Default controller method to handle the initial requests to the page
30        """
31        # Get the inputs passed via the URL
32        self.inputs=dict(parse_querystring(request.environ))
33               
34        # if 'remove all' button clicked, clear out selection session data
35        if 'ClearSelections' in self.inputs: 
36            if 'selection' in session:
37                del session['selection']
38            session.save()
39        elif 'PlotSelectedInGoogle' in self.inputs:
40            # get all selected selections
41            self.getSelectedItems()
42            # ensure selections have been made
43            if 'selectedItems' in session:
44                # now create the aggregated KML file
45                # NB, set header info correctly to open as a file
46                #response.write(render('kml_aggregate', format='xml'))
47                response.headers['Content-Type'] = 'text/plain or application/vnd.google-earth.kml+xml'
48                response.headers['Content-Disposition'] = 'attachment;filename=kmlAggregate.kml'
49                return render('kml_aggregate', format='xml')
50
51        elif 'PlotSelectedInConTerra' in self.inputs:
52            # get all selected selections
53            self.getSelectedItems()
54
55            # ensure selections have been made
56            if 'selectedItems' in session:
57                # NB, only add selections which have a WMC URL defined
58                wmcURLs = []
59                for item in session['selectedItems']:
60                    log.debug('SelectedItem: kmlList = %s, wmcList = %s' % (
61                        `item.kmlList`, `item.wmcList`))
62
63                    if (item.wmcURL):
64                        wmcURLs.append(item.wmcURL)
65                return getConTerraDoc(wmcURLs)
66       
67        elif 'PlotSelectedInLocal' in self.inputs:
68            # get all selected selections
69            self.getSelectedItems()
70
71            # ensure selections have been made
72            if 'selectedItems' in session:
73               
74                # the view tab uses its own selections session variable
75                # - this should initially match that set here
76                session['viewItems'] = copy.deepcopy(session['selectedItems'])
77                session.save()
78
79                # redirect to the view tab index code
80                h.redirect_to(h.url_for(controller='viewItems',action='index'))
81       
82        else:
83            # Check to see if individual con terra plots have been requested
84            for input in self.inputs:
85                if str(input).startswith('PlotConTerra____'):
86                    # if here a WMC has been requested to be plotted
87                    # - get the URL and plot
88                    dummy, wmcURL = str(input).split('____')
89                    # NB, the image key that we're parsing has either .x or .y at the end
90                    # - so strip this off
91                    wmcURL = wmcURL[0:len(wmcURL)-2]
92                    wmcURLs = [wmcURL]
93                    return getConTerraDoc(wmcURLs)
94           
95        return render('selectedItems')
96   
97    def getSelectedItems(self):
98        """
99        Check through the inputs to determine which checkboxes
100        have been selected in the 'selections' tab; use these
101        to build a new session list to use with the various visualisation methods
102        """
103
104        # clear out existing data
105        if 'selectedItems' in session:
106            del session['selectedItems']
107       
108        # now check for checkboxes as inputs; NB, these have the
109        # same names as the DIF entryIDs - i.e. the first element
110        # in the selection arrays - but with an extension appended to
111        # allow specific WMC/KMLs to be retrieved from list
112        selectedItems = []
113        for input in self.inputs:
114            if input.find('____') > -1:
115                entryID, i = input.split('____')
116                i = int(i)
117                for selection in session['selection']:
118                    if selection.entryID in entryID:
119                        newSelection = copy.deepcopy(selection)
120                        # NB, cope with multiple WMC/KML urls by splitting these
121                        # into multiple rows and redefining the selection item URL appropriately
122                        url = None
123                        if len(selection.kmlList) > i:
124                            url = selection.kmlList[i]
125                        newSelection.kmlURL = url
126                        url = None
127                        if len(selection.wmcList) > i:
128                            url = selection.wmcList[i]
129                        newSelection.wmcURL = url
130                        selectedItems.append(newSelection)
131       
132        if len(selectedItems) > 0:
133            session['selectedItems'] = selectedItems
134
135        session.save()
136       
137   
138    def addSelectedItem(self,entryid,divid):
139        """
140        Add a selected item to the session
141        - if this is the first item, then display the selections tab
142        @param entryid: Entry ID of related DIF record
143        @param divid: The div ID relating to the selected item in the results tab     
144        """
145
146        # URLs are unsafe to be added to the URL path_info so get them from the query_string
147        kmlurl = request.params['kmlurl']
148        wmcurl = request.params['wmcurl']
149        title = request.params['title']
150
151        log.debug('addSelectedItem(%s, %s, %s, %s, %s)' % (
152            `entryid`, `kmlurl`, `wmcurl`, `title`, `divid`))
153
154        item = selectedItem.SelectedItem(entryid, title, kmlurl, wmcurl)
155
156        log.debug('SelectedItem: kmlList = %s, wmcList = %s' % (
157            `item.kmlList`, `item.wmcList`))
158       
159        selections = [item,]
160        if 'selection' not in session:
161            newtabs=1
162            session['selection']=selections
163        else:
164            newtabs=0
165            for selection in session['selection']:
166                if selection.entryID != entryid:
167                    selections.append(selection)
168                   
169            session['selection']=selections
170        session.save()
171   
172        # TODO: would be nice to have page refresh upon selection of
173        # first item - so that selections tab not always visible
174        if newtabs:
175            c.UpdatePageTabs=1
176            c.current='Results'
177            c.pageTabs.append(('Selections',
178                               h.url_for(controller='selectedItems',
179                                         action='index')))
180
181        # Return a link to allow the item to be unselected
182        # - effectively this provides a remove function from the Results page
183        return h.link_to_remote('Unselect',
184                              dict(update=divid,
185                                    url=h.url_for(controller="selectedItems",
186                                               action="unSelectItem",
187                                               entryid=entryid,
188                                               divid=divid)))
189
190   
191    def removeSelectedItem(self,entryid):
192        """
193        Remove selected item from session data
194        @param entryid: Entry ID of record to remove from selected items list
195        """
196        self.deleteSelectedItem(entryid)
197        # rerender selections to update with item removed
198        return render('selectedItems')
199
200   
201    def deleteSelectedItem(self,entryid):
202        """
203        Remove selected item from session data
204        - NB, do this by rebuilding the session data
205        without the input data included
206        @param entryid: Entry ID of record to remove from selected items list
207        """
208        selections = []
209        deletedItem = None
210        for selection in session['selection']: 
211            if selection.entryID != entryid:
212                selections.append(selection)
213            else:
214                deletedItem = selection
215               
216        # if the new list is empty, remove the session variable and hide the selection page
217        if len(selections) == 0:
218            del session['selection']
219            c.UpdatePageTabs=1
220        else:
221            session['selection']=selections
222        session.save()
223       
224        return deletedItem
225       
226
227    def unSelectItem(self,entryid,divid):
228        """
229        Remove selected item from session data and adjust the 'Unselect' link
230        to display 'Select'
231        @param entryid: Entry ID of record to remove from selected items list
232        @param divid: The div ID relating to the selected item in the results tab     
233        """
234        deletedItem = self.deleteSelectedItem(entryid)
235       
236        return h.link_to_remote("Select",
237                               dict(update=divid,
238                                    url=h.url_for(controller="selectedItems",
239                                               action="addSelectedItem",
240                                               entryid=deletedItem.entryID,
241                                               kmlurl=deletedItem.kmlURL,
242                                               wmcurl=deletedItem.wmcURL,
243                                               title=deletedItem.title,
244                                               divid=divid)))
Note: See TracBrowser for help on using the repository browser.