2011-07-31 10:46:29 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
from django import forms
|
2012-12-16 17:26:53 +01:00
|
|
|
from django.conf import settings
|
2013-09-25 10:01:01 +02:00
|
|
|
from django.contrib.auth.models import Permission
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.utils.translation import ugettext_lazy
|
2011-11-14 20:45:04 +01:00
|
|
|
|
2013-06-20 08:33:55 +02:00
|
|
|
from openslides.config.api import config
|
2013-09-25 10:01:01 +02:00
|
|
|
from openslides.utils.forms import (CssClassMixin,
|
|
|
|
LocalizedModelMultipleChoiceField)
|
|
|
|
|
|
|
|
from .models import get_protected_perm, Group, User
|
2012-04-12 19:11:07 +02:00
|
|
|
|
2012-07-10 13:19:12 +02:00
|
|
|
|
2013-05-15 23:26:24 +02:00
|
|
|
class UserCreateForm(CssClassMixin, forms.ModelForm):
|
|
|
|
groups = LocalizedModelMultipleChoiceField(
|
|
|
|
# Hide the built-in groups 'Anonymous' (pk=1) and 'Registered' (pk=2)
|
|
|
|
queryset=Group.objects.exclude(pk=1).exclude(pk=2),
|
2013-08-31 00:17:32 +02:00
|
|
|
label=ugettext_lazy('Groups'), required=False)
|
2012-11-22 18:39:54 +01:00
|
|
|
|
2011-09-04 09:57:23 +02:00
|
|
|
class Meta:
|
2012-08-12 12:52:38 +02:00
|
|
|
model = User
|
2013-04-09 11:21:55 +02:00
|
|
|
fields = ('title', 'first_name', 'last_name', 'gender', 'email',
|
|
|
|
'groups', 'structure_level', 'committee', 'about_me', 'comment',
|
|
|
|
'is_active', 'default_password')
|
2011-09-04 09:57:23 +02:00
|
|
|
|
2013-05-16 22:36:20 +02:00
|
|
|
def clean(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Ensures that a user has either a first name or a last name.
|
|
|
|
"""
|
|
|
|
cleaned_data = super(UserCreateForm, self).clean(*args, **kwargs)
|
|
|
|
if not cleaned_data['first_name'] and not cleaned_data['last_name']:
|
|
|
|
error_msg = _('First name and last name can not both be empty.')
|
|
|
|
raise forms.ValidationError(error_msg)
|
|
|
|
return cleaned_data
|
|
|
|
|
2011-09-03 17:17:29 +02:00
|
|
|
|
2012-08-10 13:22:09 +02:00
|
|
|
class UserUpdateForm(UserCreateForm):
|
2013-04-13 19:18:51 +02:00
|
|
|
"""
|
|
|
|
Form to update an user. It raises a validation error, if a non-superuser
|
|
|
|
user edits himself and removes the last group containing the permission
|
|
|
|
to manage participants.
|
|
|
|
"""
|
2013-06-21 09:51:17 +02:00
|
|
|
user_name = forms.CharField()
|
|
|
|
"""
|
|
|
|
Field to save the username.
|
|
|
|
|
|
|
|
The field username (without the underscore) from the UserModel does not
|
|
|
|
allow whitespaces and umlauts.
|
|
|
|
"""
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
class Meta:
|
2012-08-12 12:52:38 +02:00
|
|
|
model = User
|
2013-06-21 09:51:17 +02:00
|
|
|
fields = ('user_name', 'title', 'first_name', 'last_name', 'gender', 'email',
|
2013-04-09 11:21:55 +02:00
|
|
|
'groups', 'structure_level', 'committee', 'about_me', 'comment',
|
|
|
|
'is_active', 'default_password')
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2013-04-13 19:18:51 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.request = kwargs.pop('request')
|
2013-06-21 09:51:17 +02:00
|
|
|
kwargs['initial']['user_name'] = kwargs['instance'].username
|
2013-04-13 19:18:51 +02:00
|
|
|
return super(UserUpdateForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def clean(self, *args, **kwargs):
|
|
|
|
"""
|
2013-06-03 20:13:06 +02:00
|
|
|
Raises a validation error if a non-superuser user edits himself
|
2013-04-13 19:18:51 +02:00
|
|
|
and removes the last group containing the permission to manage participants.
|
|
|
|
"""
|
2013-04-19 16:45:18 +02:00
|
|
|
# TODO: Check this in clean_groups
|
2013-06-03 20:13:06 +02:00
|
|
|
if (self.request.user == self.instance and
|
|
|
|
not self.instance.is_superuser and
|
|
|
|
not self.cleaned_data['groups'].filter(permissions__in=[get_protected_perm()]).exists()):
|
|
|
|
error_msg = _('You can not remove the last group containing the permission to manage participants.')
|
|
|
|
raise forms.ValidationError(error_msg)
|
2013-04-13 19:18:51 +02:00
|
|
|
return super(UserUpdateForm, self).clean(*args, **kwargs)
|
|
|
|
|
2011-09-03 17:17:29 +02:00
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
class GroupForm(forms.ModelForm, CssClassMixin):
|
|
|
|
permissions = LocalizedModelMultipleChoiceField(
|
2013-04-13 19:18:51 +02:00
|
|
|
queryset=Permission.objects.all(), label=ugettext_lazy('Permissions'),
|
2012-08-11 10:09:54 +02:00
|
|
|
required=False)
|
2012-08-11 11:36:55 +02:00
|
|
|
users = forms.ModelMultipleChoiceField(
|
2013-04-13 19:18:51 +02:00
|
|
|
queryset=User.objects.all(), label=ugettext_lazy('Participants'), required=False)
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2013-06-03 20:13:06 +02:00
|
|
|
class Meta:
|
|
|
|
model = Group
|
|
|
|
|
2012-04-14 18:53:18 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
2013-06-03 20:13:06 +02:00
|
|
|
# Take request argument
|
|
|
|
self.request = kwargs.pop('request', None)
|
2012-08-11 11:36:55 +02:00
|
|
|
# Initial users
|
2012-04-14 18:53:18 +02:00
|
|
|
if kwargs.get('instance', None) is not None:
|
2012-08-11 11:36:55 +02:00
|
|
|
initial = kwargs.setdefault('initial', {})
|
2012-08-12 12:52:38 +02:00
|
|
|
initial['users'] = [django_user.user.pk for django_user in kwargs['instance'].user_set.all()]
|
2012-08-11 11:36:55 +02:00
|
|
|
|
|
|
|
super(GroupForm, self).__init__(*args, **kwargs)
|
2013-06-20 08:33:55 +02:00
|
|
|
if config['participant_sort_users_by_first_name']:
|
|
|
|
self.fields['users'].queryset = self.fields['users'].queryset.order_by('first_name')
|
2012-08-11 11:36:55 +02:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
instance = forms.ModelForm.save(self, False)
|
|
|
|
|
|
|
|
old_save_m2m = self.save_m2m
|
2012-11-24 14:01:21 +01:00
|
|
|
|
2012-08-11 11:36:55 +02:00
|
|
|
def save_m2m():
|
2012-11-24 14:01:21 +01:00
|
|
|
old_save_m2m()
|
2012-08-11 11:36:55 +02:00
|
|
|
|
2012-11-24 14:01:21 +01:00
|
|
|
instance.user_set.clear()
|
|
|
|
for user in self.cleaned_data['users']:
|
|
|
|
instance.user_set.add(user)
|
2012-08-11 11:36:55 +02:00
|
|
|
self.save_m2m = save_m2m
|
|
|
|
|
|
|
|
if commit:
|
|
|
|
instance.save()
|
|
|
|
self.save_m2m()
|
|
|
|
|
|
|
|
return instance
|
2012-04-14 18:53:18 +02:00
|
|
|
|
2013-06-03 20:13:06 +02:00
|
|
|
def clean(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Raises a validation error if a non-superuser user removes himself
|
|
|
|
from the last group containing the permission to manage participants.
|
|
|
|
|
|
|
|
Raises also a validation error if a non-superuser removes his last
|
|
|
|
permission to manage participants from the (last) group.
|
|
|
|
"""
|
|
|
|
# TODO: Check this in clean_users or clean_permissions
|
|
|
|
if (self.request and
|
|
|
|
not self.request.user.is_superuser and
|
|
|
|
not self.request.user in self.cleaned_data['users'] and
|
|
|
|
not Group.objects.exclude(pk=self.instance.pk).filter(
|
|
|
|
permissions__in=[get_protected_perm()],
|
|
|
|
user__pk=self.request.user.pk).exists()):
|
|
|
|
error_msg = _('You can not remove yourself from the last group containing the permission to manage participants.')
|
|
|
|
raise forms.ValidationError(error_msg)
|
|
|
|
if (self.request and
|
|
|
|
not self.request.user.is_superuser and
|
|
|
|
not get_protected_perm() in self.cleaned_data['permissions'] and
|
|
|
|
not Group.objects.exclude(pk=self.instance.pk).filter(
|
|
|
|
permissions__in=[get_protected_perm()],
|
|
|
|
user__pk=self.request.user.pk).exists()):
|
2013-09-07 10:14:54 +02:00
|
|
|
error_msg = _('You can not remove the permission to manage participants from the last group you are in.')
|
2013-06-03 20:13:06 +02:00
|
|
|
raise forms.ValidationError(error_msg)
|
|
|
|
return super(GroupForm, self).clean(*args, **kwargs)
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-04-14 14:31:09 +02:00
|
|
|
|
2013-06-21 09:51:17 +02:00
|
|
|
class UsersettingsForm(CssClassMixin, forms.ModelForm):
|
|
|
|
user_name = forms.CharField()
|
|
|
|
"""
|
|
|
|
Field to save the username.
|
|
|
|
|
|
|
|
The field username (without the underscore) from the UserModel does not
|
|
|
|
allow whitespaces and umlauts.
|
|
|
|
"""
|
|
|
|
|
2012-12-16 17:26:53 +01:00
|
|
|
language = forms.ChoiceField(choices=settings.LANGUAGES)
|
|
|
|
|
2013-06-21 09:51:17 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
2013-09-24 23:27:30 +02:00
|
|
|
kwargs['initial'] = kwargs.get('initial', {})
|
2013-06-21 09:51:17 +02:00
|
|
|
kwargs['initial']['user_name'] = kwargs['instance'].username
|
|
|
|
return super(UsersettingsForm, self).__init__(*args, **kwargs)
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
class Meta:
|
2012-08-12 12:52:38 +02:00
|
|
|
model = User
|
2013-06-21 09:51:17 +02:00
|
|
|
fields = ('user_name', 'title', 'first_name', 'last_name', 'gender', 'email',
|
2013-04-09 11:21:55 +02:00
|
|
|
'committee', 'about_me')
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-08-15 10:58:29 +02:00
|
|
|
|
2013-06-21 09:51:17 +02:00
|
|
|
class UserImportForm(CssClassMixin, forms.Form):
|
2012-08-08 10:34:23 +02:00
|
|
|
csvfile = forms.FileField(widget=forms.FileInput(attrs={'size': '50'}),
|
2013-04-13 19:18:51 +02:00
|
|
|
label=ugettext_lazy('CSV File'))
|