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.admin_cm.user
22 # @author Krzysztof Danielowski
23 # @author Piotr W√≥jcik
24 # @date 03.02.2012
25 #
26 from django.shortcuts import render_to_response
27 from django.template import RequestContext
28 from django.template.loader import render_to_string
29 from django.utils.translation import ugettext as _
30 from django.views.decorators.csrf import csrf_protect
31 
32 from wi.commontags.templatetags.templatetags import filesizeformatmb
33 from wi.forms.user import MultipleChangeQuotaForm
34 from wi.utils import messages_ajax
35 from wi.utils.decorators import admin_cm_permission
36 from wi.utils.decorators import django_view
37 from wi.utils.forms import PasswordForm
38 from wi.utils.messages_ajax import ajax_request
39 from wi.utils.views import prep_data
40 
41 
42 @django_view
43 @ajax_request
44 @admin_cm_permission
45 ##
46 #
47 # Ajax view for fetching user list.
48 #
50  if request.method == 'GET':
51  rest_data = prep_data({'users': 'admin_cm/user/get_list/',
52  'admins': 'admin_cm/admin/list_admins/'},
53  request.session)
54 
55  for user in rest_data['users']:
56  if user['user_id'] in rest_data['admins']:
57  user['is_cmadmin'] = True
58 
59  return messages_ajax.success(rest_data['users'])
60 
61 
62 @django_view
63 @admin_cm_permission
64 ##
65 #
66 # View rendering user account page.
67 #
68 def cma_user_account(request, user_id, template_name='admin_cm/user_account.html'):
69  return render_to_response(template_name, {'user_id': user_id}, context_instance=RequestContext(request))
70 
71 
72 @django_view
73 @ajax_request
74 @admin_cm_permission
75 ##
76 #
77 # Ajax view for fetching user data and quotas.
78 #
79 def cma_ajax_get_user_data(request, user_id):
80  if request.method == 'GET':
81  rest_data = prep_data({'user': ('admin_cm/user/get_by_id/', {'user_id': user_id}),
82  'quota': ('admin_cm/user/get_quota/', {'user_id': user_id})},
83  request.session)
84 
85  quota = rest_data['quota']
86  quota['memory'] = filesizeformatmb(quota['memory'])
87  quota['used_memory'] = filesizeformatmb(quota['used_memory'])
88  quota['storage'] = filesizeformatmb(quota['storage'])
89  quota['used_storage'] = filesizeformatmb(quota['used_storage'])
90 
91  rest_data['user']['quota'] = quota
92 
93  return messages_ajax.success(rest_data['user'])
94 
95 
96 @django_view
97 @ajax_request
98 @admin_cm_permission
99 @csrf_protect
100 ##
101 #
102 # Ajax view for changing logged CM admin password.
103 #
104 def cma_ajax_change_cm_password(request, template_name='admin_cm/ajax/change_password.html', form_class=PasswordForm):
105  if request.method == 'POST':
106  form = form_class(request.POST)
107  if form.is_valid():
108  prep_data(('admin_cm/user/change_password/', {'new_password': form.cleaned_data['new_password']}), request.session)
109 
110  request.session['user'].cm_password = form.cleaned_data['new_password']
111  request.session.modified = True
112  return messages_ajax.success(_('You have successfully changed your CM password.'))
113  else:
114  form = form_class()
115  return messages_ajax.success(render_to_string(template_name, {'form': form},
116  context_instance=RequestContext(request)),
117  status=1)
118 
119 
120 @django_view
121 @ajax_request
122 @admin_cm_permission
123 @csrf_protect
124 ##
125 #
126 # Ajax view for changing users quota.
127 #
128 def cma_ajax_change_quota(request, template_name='generic/form.html', form_class=MultipleChangeQuotaForm):
129  if request.method == 'POST':
130  form = form_class(data=request.POST)
131  user_list = request.POST.getlist('userids[]')
132  if form.is_valid():
133  dictionary = {'user_ids': [int(a) for a in user_list]}
134  for value, key in form.cleaned_data.items():
135  if key is not None:
136  dictionary[value] = key
137  prep_data(('admin_cm/user/multiple_change_quota/', dictionary), request.session)
138 
139  return messages_ajax.success(_('You have successfully changed quota.'))
140  else:
141  form = form_class()
142  return messages_ajax.success(render_to_string(template_name, {'form': form,
143  'confirmation': _('Change'),
144  'text': ''},
145  context_instance=RequestContext(request)))
146