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

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

Add functionality to produce XML files for use with the Con Terra service.

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_response('/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 = Response(render('kml_aggregate', format='xml'), code=200)
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            # TODO: not yet implemented
37            self.getSelectedItems()
38       
39        else:
40            for input in self.inputs:
41                if str(input).startswith('PlotConTerra____'):
42                    # if here a WMC has been requested to be plotted
43                    # - get the URL and plot
44                    dummy, url = str(input).split('____')
45                    # NB, the image key that we're parsing has either .x or .y at the end
46                    url = url[0:len(url)-2]
47                    urls = [url]
48                    return getConTerraDoc(urls)
49           
50        return Response(render('selectedItems'), code=200)
51   
52    # check through the inputs to determine which checkboxes
53    # have been selected in the 'selections' tab; use these
54    # to build a new session list to use with the kml_aggregate template
55    def getSelectedItems(self):
56        # clear out existing data
57        if 'selectedItems' in session:
58            del session['selectedItems']
59       
60        # now check for checkboxes as inputs; NB, these have the
61        # same names as the DIF entryIDs - i.e. the first element
62        # in the selection arrays - but with an extension appended to
63        # allow specific WMC/KMLs to be retrieved from list
64        selectedItems = []
65        for input in self.inputs:
66            if input.find('____') > -1:
67                entryID, i = input.split('____')
68                i = int(i)
69                for selection in session['selection']:
70                    if selection.entryID in entryID:
71                        newSelection = copy.deepcopy(selection)
72                        url = None
73                        if len(selection.kmlList) > i:
74                            url = selection.kmlList[i]
75                        newSelection.kmlURL = url
76                        url = None
77                        if len(selection.wmcList) > i:
78                            url = selection.wmcList[i]
79                        newSelection.wmcURL = url
80                        selectedItems.append(newSelection)
81       
82        if len(selectedItems) > 0:
83            session['selectedItems'] = selectedItems
84
85        session.save()
86       
87   
88    # add a selected item to the session
89    # - if this is the first item, then display the selections tab
90    def addSelectedItem(self,entryid,kmlurl,wmcurl,title,divid):
91       
92        item = stubB.SelectedItem(entryid, title, kmlurl, wmcurl)
93       
94        selections = [item,]
95        if 'selection' not in session:
96            newtabs=1
97            session['selection']=selections
98        else:
99            newtabs=0
100            for selection in session['selection']:
101                if selection.entryID != entryid:
102                    selections.append(selection)
103                   
104            session['selection']=selections
105        session.save()
106   
107        # TODO: would be nice to have page refresh upon selection of
108        # first item - so that selections tab not always visible
109        if newtabs:
110            c.UpdatePageTabs=1
111            c.current='Results'
112            c.pageTabs.append(('Selections',
113                               h.url_for(controller='selectedItems',
114                                         action='index')))
115
116        # Return a link to allow the item to be unselected
117        # - effectively this provides a remove function from the Results page
118        return h.link_to_remote('Unselect',
119                              dict(update=divid,
120                                    url=h.url_for(controller="selectedItems",
121                                               action="unSelectItem",
122                                               entryid=entryid,
123                                               divid=divid)))
124
125   
126    # remove selected item from session data
127    def removeSelectedItem(self,entryid):
128        self.deleteSelectedItem(entryid)
129        # rerender selections to update with item removed
130        return Response(render('selectedItems'))
131   
132    # Remove selected item from session data
133    # - NB, do this by rebuilding the session data
134    # without the input data included
135    def deleteSelectedItem(self,entryid):
136        selections = []
137        deletedItem = None
138        for selection in session['selection']: 
139            if selection.entryID != entryid:
140                selections.append(selection)
141            else:
142                deletedItem = selection
143               
144               
145        # if the new list is empty, remove the session variable and hide the selection page
146        if len(selections) == 0:
147            del session['selection']
148            c.UpdatePageTabs=1
149        else:
150            session['selection']=selections
151        session.save()
152       
153        return deletedItem
154       
155
156    # remove selected item from session data and adjust the 'select' link
157    # to be 'unselect'
158    def unSelectItem(self,entryid,divid):
159        deletedItem = self.deleteSelectedItem(entryid)
160       
161        return h.link_to_remote("Select",
162                               dict(update=divid,
163                                    url=h.url_for(controller="selectedItems",
164                                               action="addSelectedItem",
165                                               entryid=deletedItem.entryID,
166                                               kmlurl=urllib.quote(str(deletedItem.kmlURL),''),
167                                               wmcurl=urllib.quote(str(deletedItem.wmcURL),''),
168                                               title=deletedItem.title,
169                                               divid=divid)))
Note: See TracBrowser for help on using the repository browser.