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

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

Add functionality to determine what selections have been made in the
'Selections' tab and then use this data with a new template, kml_aggregate.kid,
to produce a kml doc will all the associated kml info in.

Line 
1from ows_server.lib.base import *
2from paste.request import parse_querystring
3from ows_server.models import Utilities
4import urllib
5
6class SelecteditemsController(BaseController):
7   
8    def index(self):
9        # Return a rendered template
10        #   return render_response('/some/template.html')
11        # or, Return a response object
12        self.inputs=dict(parse_querystring(request.environ))
13               
14        # if 'remove all' button clicked, clear out selection session data
15        if 'ClearSelections' in self.inputs: 
16            if 'selection' in session:
17                del session['selection']
18            session.save()
19        elif 'PlotSelectedInGoogle' in self.inputs:
20            # get all selected selections
21            self.getSelectedItems()
22           
23            # ensure selections have been made
24            if 'selectedItems' in session:
25                # now create the aggregated KML file
26                # NB, set header info correctly to open as a file
27                response = render_response('kml_aggregate', format='xml')
28                response.headers['Content-Type'] = 'text/xml'
29                response.headers['Content-Disposition'] = 'attachment;filename=kmlAggregate.kml'
30                return response; 
31
32        elif 'PlotSelectedInConTerra' in self.inputs:
33            # TODO: not yet implemented
34            self.getSelectedItems()
35           
36        return render_response('selectedItems')
37   
38    # check through the inputs to determine which checkboxes
39    # have been selected in the 'selections' tab; use these
40    # to build a new session list to use with the kml_aggregate template
41    def getSelectedItems(self):
42        # clear out existing data
43        if 'selectedItems' in session:
44            del session['selectedItems']
45       
46        # now check for checkboxes as inputs; NB, these have the
47        # same names as the DIF entryIDs - i.e. the first element
48        # in the selection arrays
49        selectedItems = []
50        for selection in session['selection']:
51            if selection[0] in self.inputs:
52                selectedItems.append(selection)
53       
54        if len(selectedItems) > 0:
55            session['selectedItems'] = selectedItems
56            session.save()
57       
58       
59   
60    def addSelectedItem(self,entryid,kmlurl,title,divid):
61        # add a selected item to the session
62        # - if this is the first item, then display the selections tab
63        selections = [(entryid,kmlurl,title),]
64        if 'selection' not in session:
65            newtabs=1
66            session['selection']=selections
67        else:
68            newtabs=0
69            for selection in session['selection']:
70                if len(selection) > 0 and selection[0] != entryid:
71                    selections.append(selection)
72                   
73            session['selection']=selections
74        session.save()
75   
76        # TODO: would be nice to have page refresh upon selection of
77        # first item - so that selections tab not always visible
78        if newtabs:
79            c.UpdatePageTabs=1
80            c.current='Results'
81            c.pageTabs.append(('Selections',
82                               h.url_for(controller='selectedItems',
83                                         action='index')))
84
85        return h.link_to_remote('Unselect',
86                              dict(update=divid,
87                                    url=h.url_for(controller="selectedItems",
88                                               action="unSelectItem",
89                                               entryid=entryid,
90                                               kmlurl=urllib.quote(kmlurl,''),
91                                               title=title,
92                                               divid=divid)))
93
94   
95    def removeSelectedItem(self,entryid):
96        # remove selected item from session data
97        self.deleteSelectedItem(entryid)
98        # rerender selections to update with item removed
99        return render_response('selectedItems')
100   
101    def deleteSelectedItem(self,entryid):
102        # remove selected item from session data
103        # - NB, do this by rebuilding the session data
104        # without the input data included
105        selections = [[]]
106        for selection in session['selection']: 
107            if len(selection) > 0 and selection[0] != entryid:
108                selections.append(selection)
109               
110        # if the new list is empty, remove the session variable and hide the selection page
111        if len(selections) == 1 and len(selections[0]) == 0:
112            del session['selection']
113            c.UpdatePageTabs=1
114        else:
115            session['selection']=selections
116        session.save()
117       
118
119    def unSelectItem(self,entryid,kmlurl,title,divid):
120        # remove selected item from session data and adjust the 'select' link
121        # to be 'unselect'
122        self.deleteSelectedItem(entryid)
123       
124        return h.link_to_remote("Select",
125                               dict(update=divid,
126                                    url=h.url_for(controller="selectedItems",
127                                               action="addSelectedItem",
128                                               entryid=entryid,
129                                               kmlurl=urllib.quote(kmlurl,''),
130                                               title=title,
131                                               divid=divid)))
Note: See TracBrowser for help on using the repository browser.