cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
user.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.views.user.user
22 #
23 # @author Piotr W√≥jcik
24 # @date 31.01.2014
25 #
26 
27 from django.contrib import messages
28 from django.shortcuts import render_to_response, redirect
29 from django.template import RequestContext
30 from django.template.loader import render_to_string
31 from django.utils.translation import ugettext as _
32 from django.views.decorators.csrf import csrf_protect
33 
34 from wi.commontags.templatetags.templatetags import filesizeformatmb
35 from wi.forms.user import HelpForm, PasswordChangeForm, AccountDataEdit
36 from wi.utils import get_dict_from_list, messages_ajax
37 from wi.utils.decorators import django_view, user_permission
38 from wi.utils.exceptions import RestErrorException
39 from wi.utils.messages_ajax import ajax_request
40 from wi.utils.messages_codes import get_message
41 from wi.utils.states import message_levels_reversed
42 from wi.utils.views import prep_data
43 
44 
45 @django_view
46 @user_permission
47 ##
48 #
49 # View changing used CM.
50 #
51 def change_cm(request, cm_id, success_url='mai_main'):
52  request.session['user'].cm_id = int(cm_id)
53  request.session.modified = True
54  messages.success(request, _('Cluster Manager changed.'))
55 
56  return redirect(request.META['HTTP_REFERER'] or success_url)
57 
58 
59 @django_view
60 @ajax_request
61 @user_permission
62 ##
63 #
64 # Ajax view fetching user messages.
65 #
66 def get_messages(request):
67  if request.method == 'POST':
68  response = prep_data('user/message/get_list/', request.session)
69 
70  for item in response:
71  item['text'] = get_message(item['code'], item['params'])
72  item['level'] = message_levels_reversed[item['level']]
73  return messages_ajax.success(response)
74 
75 
76 @django_view
77 @ajax_request
78 @user_permission
79 ##
80 #
81 # Ajax view. Returns user account data.
82 #
84  if request.method == 'GET':
85  rest_data = prep_data({'user': 'user/user/get_my_data/',
86  'cms': 'guest/cluster/list_names/'},
87  request.session)
88  user_data = rest_data['user']
89 
90  users_cm = get_dict_from_list(rest_data['cms'], user_data['default_cluster_id'], key='cluster_id')
91  if users_cm is None:
92  raise Exception('User\'s default_cluster_id=%d is not a valid CM id.' % user_data['default_cluster_id'])
93 
94  user_data['default_cluster_id'] = users_cm['name']
95  return messages_ajax.success(user_data)
96 
97 
98 @django_view
99 @ajax_request
100 @user_permission
101 @csrf_protect
102 ##
103 #
104 # Ajax view for user account data editing.
105 #
106 def acc_ajax_account_data_edit(request, template_name='generic/form.html', form_class=AccountDataEdit):
107  rest_data = prep_data({'cms': 'guest/cluster/list_names/'}, request.session)
108 
109  if request.method == 'POST':
110  form = form_class(data=request.POST, rest_data=rest_data)
111  if form.is_valid():
112  prep_data({'user': ('user/user/edit/', form.cleaned_data)}, request.session)
113 
114  request.session['user'].email = form.cleaned_data['email']
115  request.session['user'].default_cluster_id = form.cleaned_data['default_cluster_id']
116  request.session.modified = True
117 
118  return messages_ajax.success(_('Account data edited.'))
119  else:
120  form = form_class(data={'email': request.session['user'].email,
121  'default_cluster_id': request.session['user'].default_cluster_id}, rest_data=rest_data)
122 
123  return messages_ajax.success(render_to_string(template_name, {'form': form,
124  'text': '',
125  'confirmation': _('Save')},
126  context_instance=RequestContext(request)),
127  status=1)
128 
129 
130 @django_view
131 @ajax_request
132 @user_permission
133 ##
134 #
135 # Ajax view for fetching users' quotas.
136 #
138  if request.method == 'GET':
139  quota = prep_data('user/user/get_quota/', request.session)
140 
141  quota['memory_formatted'] = filesizeformatmb(quota['memory'])
142  quota['used_memory_formatted'] = filesizeformatmb(quota['used_memory'])
143  quota['storage_formatted'] = filesizeformatmb(quota['storage'])
144  quota['used_storage_formatted'] = filesizeformatmb(quota['used_storage'])
145 
146  return messages_ajax.success(quota)
147 
148 
149 @django_view
150 @csrf_protect
151 @user_permission
152 ##
153 #
154 # View for password changing (for logged users).
155 #
156 def acc_password_change(request, template_name='account/password_change_form.html', password_change_form=PasswordChangeForm):
157  if request.method == "POST":
158  form = password_change_form(user=request.session['user'], data=request.POST)
159  if form.is_valid():
160  new_password = form.cleaned_data['new_password1']
161  try:
162  prep_data(('user/user/set_password/', {'new_password': new_password}), request.session)
163  except RestErrorException as ex:
164  messages.error(request, ex.value)
165 
166  request.session['user'].set_password(new_password)
167  request.session.modified = True
168  return redirect('acc_password_change_done')
169  else:
170  form = password_change_form(user=request.session['user'])
171  return render_to_response(template_name, {'form': form}, context_instance=RequestContext(request))
172 
173 
174 @django_view
175 @user_permission
176 ##
177 #
178 # View handling help form.
179 #
180 def hlp_form(request, form_class=HelpForm, template_name='help/form.html'):
181  if request.method == 'POST':
182  form = form_class(data=request.POST)
183  if form.is_valid():
184  topic, issue, email = form.cleaned_data['topic'], form.cleaned_data['issue'], form.cleaned_data['email']
185 
186  name = str(request.session.get('user', form.cleaned_data['firstlast']))
187  topic += _(' from user:') + name + ', email: ' + email
188  dictionary = {'issue': issue, 'topic': topic}
189  try:
190  prep_data(('user/user/send_issue/', dictionary), request.session)
191  except Exception:
192  return redirect('hlp_issue_error')
193 
194  return redirect('hlp_issue_sent')
195  else:
196  form = form_class()
197 
198  rest_data = prep_data('guest/user/is_mailer_active/', request.session)
199 
200  return render_to_response(template_name, dict({'form': form}.items() + rest_data.items()),
201  context_instance=RequestContext(request))
202