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_clm.user
22 # @author Piotr W√≥jcik
23 # @author Krzysztof Danielowski
24 # @date 21.09.2010
25 #
26 
27 from django.shortcuts import render_to_response
28 from django.template import RequestContext
29 from django.template.defaultfilters import force_escape
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.utils import messages_ajax
35 from wi.utils.decorators import admin_clm_permission, django_view
36 from wi.utils.forms import PasswordForm
37 from wi.utils.messages_ajax import ajax_request
38 from wi.utils.states import user_active_reversed as user_states
39 from wi.utils.views import prep_data
40 
41 
42 @django_view
43 @ajax_request
44 @admin_clm_permission
45 ##
46 #
47 # Ajax view for fetching users list.
48 #
50  if request.method == 'GET':
51  users = prep_data('admin_clm/user/get_list/', request.session)
52 
53  for item in users:
54  item['is_activeName'] = unicode(user_states[item['is_active']])
55 
56  return messages_ajax.success(users)
57 
58 
59 @django_view
60 @admin_clm_permission
61 ##
62 #
63 # User account details view.
64 #
65 def clm_user_account(request, userid, template_name='admin_clm/user_account.html'):
66  return render_to_response(template_name, {'user_id': userid}, context_instance=RequestContext(request))
67 
68 
69 @django_view
70 @ajax_request
71 @admin_clm_permission
72 @csrf_protect
73 ##
74 #
75 # Ajax view for setting user's password.
76 #
77 def clm_ajax_set_password(request, id1, template_name='admin_clm/ajax/set_password.html', form_class=PasswordForm):
78  id1 = int(id1)
79  if request.method == 'POST':
80  form = form_class(request.POST)
81  if form.is_valid():
82  new_password = form.cleaned_data['new_password']
83  prep_data(('admin_clm/user/set_password/', {'user_id': id1,
84  'new_password': new_password}), request.session)
85 
86  if id1 == request.session['user'].user_id:
87  request.session['user'].set_password(new_password)
88  request.session.modified = True
89  return messages_ajax.success(_('You have successfully set password for user <b>%(desc)s</b>.') % {'desc': (force_escape(request.POST.get('desc')))})
90  else:
91  form = form_class()
92  return messages_ajax.success(render_to_string(template_name,
93  {'form': form,
94  'text': _('Setting password for user <b>%(desc)s</b>:') % {'desc': (force_escape(request.REQUEST.get('desc')))}},
95  context_instance=RequestContext(request)),
96  status=1)
97