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

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

Adjust templates to use new, selectedItem object.
Extend scripts to cope with multiple KML and WMC URLs - these are
presented in the selections table on separate rows allowing their
individual selection when creating aggregated KML/WMC docs. NB,
selected docs which don't have relevant KML/WMC info are ignored.

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