Package omeroweb :: Package webclient :: Module decorators
[hide private]
[frames] | no frames]

Source Code for Module omeroweb.webclient.decorators

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  # 
  5  # Copyright (C) 2011 University of Dundee & Open Microscopy Environment. 
  6  # All rights reserved. 
  7  # 
  8  # This program is free software: you can redistribute it and/or modify 
  9  # it under the terms of the GNU Affero General Public License as 
 10  # published by the Free Software Foundation, either version 3 of the 
 11  # License, or (at your option) any later version. 
 12  # 
 13  # This program is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU Affero General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU Affero General Public License 
 19  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 20  # 
 21   
 22  """ 
 23  Decorators for use with the webclient application. 
 24  """ 
 25   
 26  import logging 
 27   
 28  import omeroweb.decorators 
 29   
 30  from django.http import HttpResponse, HttpResponseServerError, Http404 
 31  from django.utils.http import urlencode 
 32  from django.conf import settings 
 33  from django.core.urlresolvers import reverse 
 34   
 35  from omeroweb.webgateway import views as webgateway_views 
 36  from omeroweb.connector import Server 
 37  from omeroweb.webclient.webclient_http import HttpLoginRedirect 
 38   
 39  logger = logging.getLogger('omeroweb.webclient.decorators') 
 40   
41 -class login_required(omeroweb.decorators.login_required):
42 """ 43 webclient specific extension of the OMERO.web login_required() decorator. 44 """ 45
46 - def __init__(self, ignore_login_fail=False, setGroupContext=False, login_redirect=None, **kwargs):
47 """ 48 Initialises the decorator. 49 """ 50 super(login_required, self).__init__(**kwargs) 51 self.ignore_login_fail = ignore_login_fail 52 self.setGroupContext = setGroupContext 53 self.login_redirect = login_redirect
54
55 - def on_logged_in(self, request, conn):
56 """Called whenever the users is successfully logged in.""" 57 super(login_required, self).on_logged_in(request, conn) 58 self.prepare_session(request) 59 if self.setGroupContext: 60 if request.session.get('active_group'): 61 conn.SERVICE_OPTS.setOmeroGroup(request.session.get('active_group')) 62 else: 63 conn.SERVICE_OPTS.setOmeroGroup(conn.getEventContext().groupId)
64
65 - def on_not_logged_in(self, request, url, error=None):
66 """ This can be used to fail silently (not return 403, 500 etc. E.g. keepalive ping)""" 67 if self.ignore_login_fail: 68 return HttpResponse("Connection Failed") 69 if self.login_redirect is not None: 70 try: 71 url = reverse(self.login_redirect) 72 except: 73 pass 74 return super(login_required, self).on_not_logged_in(request, url, error)
75
76 - def prepare_session(self, request):
77 """Prepares various session variables.""" 78 changes = False 79 if request.session.get('callback') is None: 80 request.session['callback'] = dict() 81 changes = True 82 if request.session.get('shares') is None: 83 request.session['shares'] = dict() 84 changes = True 85 if request.session.get('imageInBasket') is None: 86 request.session['imageInBasket'] = set() 87 changes = True 88 if request.session.get('basket_counter') is None: 89 request.session['basket_counter'] = 0 90 changes = True 91 if changes: 92 request.session.modified = True
93
94 -class render_response(omeroweb.decorators.render_response):
95 """ Subclass for adding additional data to the 'context' dict passed to templates """ 96
97 - def prepare_context(self, request, context, *args, **kwargs):
98 """ 99 This allows templates to access the current eventContext and user from the L{omero.gateway.BlitzGateway}. 100 E.g. <h1>{{ ome.user.getFullName }}</h1> 101 If these are not required by the template, then they will not need to be loaded by the Blitz Gateway. 102 The results are cached by Blitz Gateway, so repeated calls have no additional cost. 103 We also process some values from settings and add these to the context. 104 """ 105 106 # we expect @login_required to pass us 'conn', but just in case... 107 if 'conn' not in kwargs: 108 return 109 conn = kwargs['conn'] 110 111 context.setdefault('ome', {}) # don't overwrite existing ome 112 context['ome']['eventContext'] = conn.getEventContext 113 context['ome']['user'] = conn.getUser 114 context['ome']['basket_counter'] = request.session.get('basket_counter', 0) 115 context['ome']['user_id'] = request.session.get('user_id', None) 116 context['ome']['group_id'] = request.session.get('group_id', None) 117 context['ome']['active_group'] = request.session.get('active_group', conn.getEventContext().groupId) 118 if settings.WEBSTART: 119 context['ome']['insight_url'] = request.build_absolute_uri(reverse("webstart_insight")) 120 self.load_settings(request, context, conn)
121 122
123 - def load_settings(self, request, context, conn):
124 125 # Process various settings and add to the template context dict 126 ping_interval = settings.PING_INTERVAL 127 if ping_interval > 0: 128 context['ping_interval'] = ping_interval 129 130 top_links = settings.TOP_LINKS 131 links = [] 132 for tl in top_links: 133 label = tl[0] 134 link_id = tl[1] 135 try: 136 link = reverse(link_id) 137 links.append( {"label":label, "link":link} ) 138 except: 139 # assume we've been passed a url 140 links.append( {"label":label, "link":link_id} ) 141 context['ome']['top_links'] = links 142 143 right_plugins = settings.RIGHT_PLUGINS 144 r_plugins = [] 145 for rt in right_plugins: 146 label = rt[0] 147 include = rt[1] 148 plugin_id = rt[2] 149 r_plugins.append( {"label":label, "include":include, "plugin_id": plugin_id} ) 150 context['ome']['right_plugins'] = r_plugins 151 152 center_plugins = settings.CENTER_PLUGINS 153 c_plugins = [] 154 for cp in center_plugins: 155 label = cp[0] 156 include = cp[1] 157 plugin_id = cp[2] 158 c_plugins.append( {"label":label, "include":include, "plugin_id": plugin_id} ) 159 context['ome']['center_plugins'] = c_plugins
160