source: cowsclient/tags/1.6.0-py2.6/cowsclient/controllers/oldwmsviz.py @ 7593

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cowsclient/tags/1.6.0-py2.6/cowsclient/controllers/oldwmsviz.py@7593
Revision 7593, 5.4 KB checked in by astephen, 10 years ago (diff)

Tagged version updated to be compatible with:

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