views.py
1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import permission_required, login_required
from django.contrib.auth.models import User, Group
from common.decorators import render
from accounts.forms import AddUserForm, SettingsForm
from accounts.models import UserSettings, manager_groups
from dictionary.models import editable_vocabularies
@permission_required('auth.add_user')
@render('registration/registration_form.html')
def register(request):
if request.method == 'POST':
form = AddUserForm(data=request.POST)
if form.is_valid():
form.save(request=request)
return HttpResponseRedirect(reverse('registration_complete'))
else:
form = AddUserForm()
if not request.user.has_perm('accounts.create_admin'):
choices = ((g.pk, g.name) for g in manager_groups())
form.fields['groups'].choices = choices
return {'form': form}
@login_required
@render()
def settings(request):
user_settings, created = UserSettings.objects.get_or_create(
user=request.user)
if request.method == 'POST':
form = SettingsForm(data=request.POST, instance=user_settings)
if form.is_valid():
form.save()
return HttpResponseRedirect(request.POST['next'])
else:
form = SettingsForm(
instance=user_settings,
vocabularies=editable_vocabularies(request.user))
return {'form': form, 'next': request.META.get('HTTP_REFERER', '')}
@permission_required('auth.add_user')
@render()
def manage_groups(request):
users = User.objects.filter(is_superuser=False)
if request.user.has_perm('accounts.create_admin'):
groups = Group.objects.all()
else:
groups = manager_groups()
js_vars = {'ajax_set_group': reverse('set_group')}
return {'users': users, 'groups': groups, 'js_vars': js_vars}