source: cowsclient/cowsclient/controllers/wmsviz.py @ 5360

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cowsclient/cowsclient/controllers/wmsviz.py@5360
Revision 5360, 5.3 KB checked in by domlowe, 10 years ago (diff)

Now clears out old endpoints upon reload

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 - allowing the display of WMC map layers
8
9 @author C Byrom Feb 08, Modified D Lowe, May 09
10"""
11
12from cowsclient.lib.base import *
13from paste.request import parse_querystring
14#from ows_server.models import Utilities
15from cowsclient.lib.wmc_util import *
16from cowsclient.model import selectedItem
17from pylons import config, g
18import copy, logging
19log = logging.getLogger(__name__)
20import urllib
21
22class WmsvizController(BaseController):
23   
24    def index(self):
25        """
26        Default controller method to handle the initial requests to the page
27        """
28        log.debug('wmsviz controller')
29       
30        g.helpIcon='layout/icons/help.png'  #needs to go in config
31       
32
33        self.inputs=dict(parse_querystring(request.environ))
34        log.info(self.inputs)
35        c.wmcURL = ""
36        # check if all we're doing is removing a view item
37        if 'removeItem' in self.inputs:
38            return self.removeViewItem(self.inputs['removeItem'])
39        # check if we're doing an AJAX callback to get some WMC data
40        if 'REQUEST' in self.inputs:           
41            if self.inputs['REQUEST'] == 'GetWebMapContext':
42                wmc= GetWebMapContext(self)                   
43                return wmc
44            elif self.inputs['REQUEST'] == 'GetLegend':
45                return GetLegend(self)
46
47       
48        #get server information from config file
49        g.server=config['app_conf']['serverurl']
50   
51       
52        #TODO: WORK OUT HOW TO COUPLE THIS TO BROWSE
53        # otherwise, we can get here by two routes:
54        # i) either by clicking on the WMC icon in the details view - if so, get passed endpoint and add to
55        #    selected items;
56        # ii) or from the selections tab - which will already have set up the correct selected items
57           
58        # if ENDPOINT specified, we've reached the page via the WMC icon
59               
60        if ('ENDPOINT' in self.inputs):
61            #clear out old endpoints NOTE. this means only one endpoint at a time can be viewed. May want to
62        #rethink this to enable 'shopping cart' type selection.
63            self.removeAllViewItems()
64            urlstring=str(self.inputs['ENDPOINT'])
65            req = urllib2.Request(urlstring)
66            req.add_header('Cookie', request.headers.get('Cookie', ''))
67            try:
68                    filehandle = urllib2.urlopen(req)
69            except urllib2.HTTPError, e:
70                if e.code == 401:
71                    log.info ('401 unauthorized error in cowsclient')
72                    return abort(401) #triggers ndg security framework
73                elif e.code == 403:  #TODO: 403 response is UNTESTED.
74                    # User is authenticated but doesn't have the required permissions
75                    # or an error occurred in the authorization process
76                    # Read response
77                    response = e.read()
78                    # Send response to user
79                    self.start_response("%d %s" % (e.code, e.msg), e.headers.dict.items())
80                    return response
81            self.addViewItem(self.inputs['ENDPOINT'])
82       
83        # avoid page crashing if we come here without view items defined
84#        if 'viewItems' not in session:
85#            h.redirect_to(h.url_for(controller='discovery'))
86#                                       
87        session.save() 
88        log.info('SAVED SESSION')
89       
90        # check if page has been visited before; if not display tab
91#        if (Utilities.isTabRequired(c.pageTabs, 'View')):
92#            c.pageTabs.append(('View', h.url_for(controller='viewItems',action='index')))
93
94        return render('wmsviz')
95
96
97    def addViewItem(self,endpoint):
98        """
99        Add a selected item to the session
100         - if this is the first item, then display the selections tab
101         @param endpoint: WMC endpoint
102        """
103       
104        item = selectedItem.SelectedItem(None, None, None, endpoint)
105       
106        selections = [item,]
107        # avoid duplicates
108        if 'viewItems' in session:
109            for selection in session['viewItems']:
110                if selection.wmcURL != endpoint:
111                    selections.append(selection)
112                   
113        session['viewItems'] = selections
114        session.save() 
115       
116   
117    def removeViewItem(self,endpoint):
118        """
119        Remove view item from session data
120        - NB, do this by rebuilding the session data without the input data included
121        @param endpoint: The WMC endpoint of the view item to remove
122        """
123        selections = []
124        for selection in session['viewItems']:
125            if selection.wmcURL != endpoint:
126                selections.append(selection)
127               
128        # if the new list is empty, remove the session variable
129        if len(selections) == 0:
130            del session['viewItems']
131            c.UpdatePageTabs=1
132        else:
133            session['viewItems'] = selections
134        session.save()
135       
136    def removeAllViewItems(self):
137        """
138        Remove all old view items - clears out old endpoints
139        """
140        session['viewItems']=[]
141        session.save()
Note: See TracBrowser for help on using the repository browser.