source: MILK/trunk/milk_server/milk_server/controllers/visualise/viewItems.py @ 5302

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/MILK/trunk/milk_server/milk_server/controllers/visualise/viewItems.py@5302
Revision 5302, 4.3 KB checked in by cbyrom, 11 years ago (diff)

Simplify structure of discovery controller, using the results template
to render searches even when no results are found. Add new error
dict to avoid confusing with the error dict used by the formencode
input checker. Add additional input checks and improve error handling.
Add dropdown list to select order by values + add new constants
module to store associated data and error keys + tidy up DiscoveryState?
object to make clearer and logic more consistent + fix paging controls
for results data.

Line 
1# Copyright (C) 2007 STFC & NERC (Science and Technology Facilities Council).
2# This software may be distributed under the terms of the
3# Q Public License, version 1.0 or later.
4# http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
5#
6"""
7 Controller for the 'View' tab - NB, this is a stub to allow the WMC client
8 to be specified externally - in the config file
9
10 @author C Byrom, Tessella Nov 08
11"""
12
13from milk_server.lib.base import *
14from paste.request import parse_querystring
15from milk_server.lib import Utilities
16from milk_server.models import selectedItem
17import logging
18
19class ViewitemsController(BaseController):
20   
21    def index(self):
22        """
23        Default controller method to handle the initial requests to the page
24        """
25        logging.info("Processing selected items")
26       
27        # firstly check if a WMC client has been specified
28        if not g.wmcClientURL:
29            errorMessage = "No WMC client has been configured for use with server\n \
30            - until this is done, the data is not available to view locally"
31            logging.warning(errorMessage)
32            c.xml = errorMessage
33            return render('error')
34       
35        self.inputs=dict(parse_querystring(request.environ))
36        c.wmcURL = ""
37       
38        # TODO: the following code needs to be updated to connect to a wmc client
39        # g.wmcClientURL
40       
41        # check if all we're doing is removing a view item
42        if 'removeItem' in self.inputs:
43            logging.info("- removing selected item")
44            return self.removeViewItem(self.inputs['removeItem'])
45       
46        # check if we're doing an AJAX callback to get some WMC data
47        if 'REQUEST' in self.inputs:
48            logging.info("- lookup WMC data")
49            if self.inputs['REQUEST'] == 'GetWebMapContext':
50                return GetWebMapContext(self)
51            elif self.inputs['REQUEST'] == 'GetLegend':
52                return GetLegend(self)
53
54       
55        # otherwise, we can get here by two routes:
56        # i) either by clicking on the WMC icon in the details view - if so, get passed endpoint and add to
57        #    selected items;
58        # ii) or from the selections tab - which will already have set up the correct selected items
59           
60        # if ENDPOINT specified, we've reached the page via the WMC icon
61        if ('ENDPOINT' in self.inputs):
62            self.addViewItem(self.inputs['ENDPOINT'])
63       
64        # avoid page crashing if we come here without view items defined
65        if 'viewItems' not in session:
66            h.redirect_to(h.url_for(controller='browse/discovery'))
67                                       
68        session.save()
69       
70        # check if page has been visited before; if not display tab
71        if (Utilities.isTabRequired(c.pageTabs, 'View')):
72            c.pageTabs.append(('View', \
73                               h.url_for(controller='visualise/viewItems', \
74                                         action='index')))
75
76        return render('visualise/viewItems')
77
78
79    def addViewItem(self,endpoint):
80        """
81        Add a selected item to the session
82         - if this is the first item, then display the selections tab
83         @param endpoint: WMC endpoint
84        """
85       
86        item = selectedItem.SelectedItem(None, None, None, endpoint)
87       
88        selections = [item,]
89        # avoid duplicates
90        if 'viewItems' in session:
91            for selection in session['viewItems']:
92                if selection.wmcURL != endpoint:
93                    selections.append(selection)
94                   
95        session['viewItems'] = selections
96        session.save()
97       
98   
99    def removeViewItem(self,endpoint):
100        """
101        Remove view item from session data
102        - NB, do this by rebuilding the session data without the input data included
103        @param endpoint: The WMC endpoint of the view item to remove
104        """
105        selections = []
106        for selection in session['viewItems']:
107            if selection.wmcURL != endpoint:
108                selections.append(selection)
109               
110        # if the new list is empty, remove the session variable
111        if len(selections) == 0:
112            del session['viewItems']
113            c.UpdatePageTabs=1
114        else:
115            session['viewItems'] = selections
116
117        session.save()
118       
Note: See TracBrowser for help on using the repository browser.