views.py 1.96 KB
#-*- 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}