cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
decorators.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.wi.utils.decorators
22 #
23 # @author Piotr W√≥jcik
24 # @author Krzysztof Danielowski
25 #
26 
27 import logging
28 
29 from django.conf import settings
30 from django.http import HttpResponseRedirect
31 from django.utils.http import urlquote
32 
33 from wi.utils import REDIRECT_FIELD_NAME
34 from wi.utils.messages_ajax import success
35 from wi.utils.messages_codes import auth_error_text
36 
37 
38 ##
39 #
40 # Logs any exception thrown by a view.
41 #
42 def django_view(fun):
43  wi_logger = logging.getLogger('wi_logger')
44 
45  ##
46  #
47  # Returned decorated function.
48  #
49  def wrapper(*args, **kwargs):
50  try:
51  ret = fun(*args, **kwargs)
52  except Exception, ex:
53  wi_logger.exception('General exception: %s' % str(ex))
54  raise ex
55  return ret
56  wrapper.__module__ = fun.__module__
57  wrapper.__name__ = fun.__name__
58  wrapper.__doc__ = fun.__doc__
59  return wrapper
60 
61 login_url = settings.LOGIN_URL
62 cm_login_url = '/admin_cm/login/'
63 
64 
65 ##
66 #
67 # \b Decorator for views with logged user permissions.
68 #
69 def user_permission(view_func):
70  ##
71  #
72  # Returned decorated function.
73  #
74  def wrap(request, *args, **kwds):
75  if 'user' in request.session:
76  return view_func(request, *args, **kwds)
77  if request.is_ajax():
78  return success(unicode(auth_error_text), status=8002)
79  path = urlquote(request.get_full_path())
80  tup = login_url, REDIRECT_FIELD_NAME, path
81  return HttpResponseRedirect('%s?%s=%s' % tup)
82  return wrap
83 
84 
85 ##
86 #
87 # \b Decorator for views with logged Cloud Manager admin permissions.
88 #
89 def admin_clm_permission(view_func):
90  ##
91  #
92  # Returned decorated function.
93  #
94  def wrap(request, *args, **kwds):
95  if 'user' in request.session and request.session['user'].is_admin_clm:
96  return view_func(request, *args, **kwds)
97  if request.is_ajax():
98  return success(auth_error_text, status=8003)
99  path = urlquote(request.get_full_path())
100  tup = login_url, REDIRECT_FIELD_NAME, path
101  return HttpResponseRedirect('%s?%s=%s' % tup)
102  return wrap
103 
104 
105 ##
106 #
107 # \b Decorator for views with logged Cluster Manager admin permissions.
108 #
109 def admin_cm_permission(view_func):
110  ##
111  #
112  # Returned decorated function.
113  #
114  def wrap(request, *args, **kwds):
115  if 'user' in request.session and request.session['user'].is_logged_admin_cm:
116  return view_func(request, *args, **kwds)
117  if request.is_ajax():
118  return success(auth_error_text, status=8004)
119  path = urlquote(request.get_full_path())
120  tup = cm_login_url, REDIRECT_FIELD_NAME, path
121  return HttpResponseRedirect('%s?%s=%s' % tup)
122  return wrap
123