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

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

Adjust code to allow multiple WMC docs to be selected in the selections
tab and displayed in the view tab. Also rename the display tab to 'details'
and add new exceptions to deal with URL lookup failures.

Line 
1from ows_server.lib.base import *
2from paste.request import parse_querystring
3from ows_server.models import Utilities
4from ows_server.models import stubB
5from ows_server.lib.wmc_util import *
6from ows_server.controllers import viewItems
7import copy
8import urllib
9
10class SelecteditemsController(BaseController):
11   
12    def index(self):
13        # Return a rendered template
14        #   return render('/some/template.html')
15        # or, Return a response object
16        self.inputs=dict(parse_querystring(request.environ))
17               
18        # if 'remove all' button clicked, clear out selection session data
19        if 'ClearSelections' in self.inputs: 
20            if 'selection' in session:
21                del session['selection']
22            session.save()
23        elif 'PlotSelectedInGoogle' in self.inputs:
24            # get all selected selections
25            self.getSelectedItems()
26           
27            # ensure selections have been made
28            if 'selectedItems' in session:
29                # now create the aggregated KML file
30                # NB, set header info correctly to open as a file
31                response.write(render('kml_aggregate', format='xml'))
32                response.headers['Content-Type'] = 'text/xml'
33                response.headers['Content-Disposition'] = 'attachment;filename=kmlAggregate.kml'
34                return response; 
35
36        elif 'PlotSelectedInConTerra' in self.inputs:
37            # get all selected selections
38            self.getSelectedItems()
39
40            # ensure selections have been made
41            if 'selectedItems' in session:
42                wmcURLs = []
43                for item in session['selectedItems']:
44                    if (item.wmcURL):
45                        wmcURLs.append(item.wmcURL)
46                return getConTerraDoc(wmcURLs)
47       
48        elif 'PlotSelectedInLocal' in self.inputs:
49            # get all selected selections
50            self.getSelectedItems()
51
52            # ensure selections have been made
53            if 'selectedItems' in session:
54                # the view tab uses its own selections session variable
55                # - this should initially match that set here
56                session['viewItems'] = copy.deepcopy(session['selectedItems'])
57               
58                viewController = viewItems.ViewitemsController()
59                return viewController.index()
60       
61        else:
62            # Check to see if individual con terra plots have been requested
63            for input in self.inputs:
64                if str(input).startswith('PlotConTerra____'):
65                    # if here a WMC has been requested to be plotted
66                    # - get the URL and plot
67                    dummy, wmcURL = str(input).split('____')
68                    # NB, the image key that we're parsing has either .x or .y at the end
69                    wmcURL = wmcURL[0:len(wmcURL)-2]
70                    wmcURLs = [wmcURL]
71                    return getConTerraDoc(wmcURLs)
72           
73        return render('selectedItems')
74   
75    # check through the inputs to determine which checkboxes
76    # have been selected in the 'selections' tab; use these
77    # to build a new session list to use with the kml_aggregate template
78    def getSelectedItems(self):
79        # clear out existing data
80        if 'selectedItems' in session:
81            del session['selectedItems']
82       
83        # now check for checkboxes as inputs; NB, these have the
84        # same names as the DIF entryIDs - i.e. the first element
85        # in the selection arrays - but with an extension appended to
86        # allow specific WMC/KMLs to be retrieved from list
87        selectedItems = []
88        for input in self.inputs:
89            if input.find('____') > -1:
90                entryID, i = input.split('____')
91                i = int(i)
92                for selection in session['selection']:
93                    if selection.entryID in entryID:
94                        newSelection = copy.deepcopy(selection)
95                        # NB, cope with multiple WMC/KML urls by splitting these
96                        # into multiple rows and redefining the selection item URL appropriately
97                        url = None
98                        if len(selection.kmlList) > i:
99                            url = selection.kmlList[i]
100                        newSelection.kmlURL = url
101                        url = None
102                        if len(selection.wmcList) > i:
103                            url = selection.wmcList[i]
104                        newSelection.wmcURL = url
105                        selectedItems.append(newSelection)
106       
107        if len(selectedItems) > 0:
108            session['selectedItems'] = selectedItems
109
110        session.save()
111       
112   
113    # add a selected item to the session
114    # - if this is the first item, then display the selections tab
115    def addSelectedItem(self,entryid,kmlurl,wmcurl,title,divid):
116       
117        item = stubB.SelectedItem(entryid, title, kmlurl, wmcurl)
118       
119        selections = [item,]
120        if 'selection' not in session:
121            newtabs=1
122            session['selection']=selections
123        else:
124            newtabs=0
125            for selection in session['selection']:
126                if selection.entryID != entryid:
127                    selections.append(selection)
128                   
129            session['selection']=selections
130        session.save()
131   
132        # TODO: would be nice to have page refresh upon selection of
133        # first item - so that selections tab not always visible
134        if newtabs:
135            c.UpdatePageTabs=1
136            c.current='Results'
137            c.pageTabs.append(('Selections',
138                               h.url_for(controller='selectedItems',
139                                         action='index')))
140
141        # Return a link to allow the item to be unselected
142        # - effectively this provides a remove function from the Results page
143        return h.link_to_remote('Unselect',
144                              dict(update=divid,
145                                    url=h.url_for(controller="selectedItems",
146                                               action="unSelectItem",
147                                               entryid=entryid,
148                                               divid=divid)))
149
150   
151    # remove selected item from session data
152    def removeSelectedItem(self,entryid):
153        self.deleteSelectedItem(entryid)
154        # rerender selections to update with item removed
155        return render('selectedItems')
156   
157    # Remove selected item from session data
158    # - NB, do this by rebuilding the session data
159    # without the input data included
160    def deleteSelectedItem(self,entryid):
161        selections = []
162        deletedItem = None
163        for selection in session['selection']: 
164            if selection.entryID != entryid:
165                selections.append(selection)
166            else:
167                deletedItem = selection
168               
169               
170        # if the new list is empty, remove the session variable and hide the selection page
171        if len(selections) == 0:
172            del session['selection']
173            c.UpdatePageTabs=1
174        else:
175            session['selection']=selections
176        session.save()
177       
178        return deletedItem
179       
180
181    # remove selected item from session data and adjust the 'select' link
182    # to be 'unselect'
183    def unSelectItem(self,entryid,divid):
184        deletedItem = self.deleteSelectedItem(entryid)
185       
186        return h.link_to_remote("Select",
187                               dict(update=divid,
188                                    url=h.url_for(controller="selectedItems",
189                                               action="addSelectedItem",
190                                               entryid=deletedItem.entryID,
191                                               kmlurl=urllib.quote(str(deletedItem.kmlURL),''),
192                                               wmcurl=urllib.quote(str(deletedItem.wmcURL),''),
193                                               title=deletedItem.title,
194                                               divid=divid)))
Note: See TracBrowser for help on using the repository browser.