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

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

Add docs for python scripts.

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