source: MILK/trunk/milk_server/milk_server/controllers/selectedItems.py @ 4476

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/MILK/trunk/milk_server/milk_server/controllers/selectedItems.py@4476
Revision 4476, 9.7 KB checked in by cbyrom, 11 years ago (diff)

Remove the viewItems controller - for visualising WCS/WMS data - and remove associated code.

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