OpenSlides/openslides/participant/forms.py

143 lines
5.3 KiB
Python
Raw Normal View History

2011-07-31 10:46:29 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
openslides.participant.forms
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Forms for the participant app.
:copyright: 20112013 by OpenSlides team, see AUTHORS.
2011-07-31 10:46:29 +02:00
:license: GNU GPL, see LICENSE for more details.
"""
from django import forms
from django.contrib import messages
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext as _, ugettext_lazy
from django.conf import settings
from openslides.utils.forms import CssClassMixin, LocalizedModelMultipleChoiceField
from openslides.participant.models import User, Group
2013-03-12 20:58:22 +01:00
from openslides.participant.api import get_registered_group
2012-07-10 13:19:12 +02:00
class UserCreateForm(forms.ModelForm, CssClassMixin):
groups = forms.ModelMultipleChoiceField(
queryset=Group.objects.exclude(name__iexact='anonymous'),
label=ugettext_lazy('Groups'), required=False)
def __init__(self, *args, **kwargs):
if kwargs.get('instance', None) is None:
initial = kwargs.setdefault('initial', {})
2013-03-12 20:58:22 +01:00
registered = get_registered_group()
initial['groups'] = [registered.pk]
super(UserCreateForm, self).__init__(*args, **kwargs)
class Meta:
model = User
fields = ('title', 'first_name', 'last_name', 'gender', 'email',
'groups', 'structure_level', 'committee', 'about_me', 'comment',
'is_active', 'default_password')
2011-09-03 17:17:29 +02:00
class UserUpdateForm(UserCreateForm):
"""
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.
"""
2011-07-31 10:46:29 +02:00
class Meta:
model = User
fields = ('username', 'title', 'first_name', 'last_name', 'gender', 'email',
'groups', 'structure_level', 'committee', 'about_me', 'comment',
'is_active', 'default_password')
2011-07-31 10:46:29 +02:00
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request')
return super(UserUpdateForm, self).__init__(*args, **kwargs)
def clean(self, *args, **kwargs):
"""
Raises a validation error, if a non-superuser user edits himself
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
if self.request.user == self.instance and not self.instance.is_superuser:
2013-04-19 16:45:18 +02:00
protected_perm = Permission.objects.get(
content_type=ContentType.objects.get(app_label='participant',
model='user'),
codename='can_manage_participant')
if not self.cleaned_data['groups'].filter(permissions__in=[protected_perm]).exists():
error_msg = _('You can not remove the last group containing the permission to manage participants.')
messages.error(self.request, error_msg)
raise forms.ValidationError(error_msg)
return super(UserUpdateForm, self).clean(*args, **kwargs)
2011-09-03 17:17:29 +02:00
class GroupForm(forms.ModelForm, CssClassMixin):
permissions = LocalizedModelMultipleChoiceField(
queryset=Permission.objects.all(), label=ugettext_lazy('Permissions'),
required=False)
2012-08-11 11:36:55 +02:00
users = forms.ModelMultipleChoiceField(
queryset=User.objects.all(), label=ugettext_lazy('Participants'), required=False)
2011-07-31 10:46:29 +02:00
def __init__(self, *args, **kwargs):
2012-08-11 11:36:55 +02:00
# Initial users
if kwargs.get('instance', None) is not None:
2012-08-11 11:36:55 +02:00
initial = kwargs.setdefault('initial', {})
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)
def save(self, commit=True):
instance = forms.ModelForm.save(self, False)
old_save_m2m = self.save_m2m
2012-08-11 11:36:55 +02:00
def save_m2m():
old_save_m2m()
2012-08-11 11:36:55 +02: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
def clean_name(self):
2012-08-11 11:36:55 +02:00
# Do not allow to change the name "anonymous" or give another group
# this name
data = self.cleaned_data['name']
if self.instance.name.lower() in ['anonymous', 'registered']:
# Editing the anonymous-user
if self.instance.name.lower() != data.lower():
raise forms.ValidationError(
_('You can not edit the name for this group.'))
else:
if data.lower() in ['anonymous', 'registered']:
raise forms.ValidationError(
_('Group name "%s" is reserved for internal use.') % data)
return data
2011-07-31 10:46:29 +02:00
class Meta:
model = Group
2011-07-31 10:46:29 +02:00
2012-04-14 14:31:09 +02:00
class UsersettingsForm(forms.ModelForm, CssClassMixin):
language = forms.ChoiceField(choices=settings.LANGUAGES)
2011-07-31 10:46:29 +02:00
class Meta:
model = User
fields = ('username', 'title', 'first_name', 'last_name', 'gender', 'email',
'committee', 'about_me')
2011-07-31 10:46:29 +02:00
2012-08-15 10:58:29 +02:00
class UserImportForm(forms.Form, CssClassMixin):
csvfile = forms.FileField(widget=forms.FileInput(attrs={'size': '50'}),
label=ugettext_lazy('CSV File'))