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

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

Upgrade the server code to make consistent with newer pylons codebase - v0.9.6.1.
This mainly involves the replacement of the Response object, and associated methods,
with the inbuild, default response object. Typical changes include:

render_response -> render - with required settings on the response object
made before the render call against the response object

Response(...) -> response.write() - for content + response.headers.. = .. for headers info

  • also included the replacement of depricated functions, as highlighted by

the server logging

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