OpenSlides/openslides/participant/forms.py

116 lines
3.7 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.
2012-04-25 22:29:19 +02:00
:copyright: 2011, 2012 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.auth.models import Permission
from django.utils.translation import ugettext_lazy as _
from openslides.utils.forms import (
CssClassMixin, LocalizedModelMultipleChoiceField)
from openslides.participant.models import User, 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=_("User groups"), required=False)
class Meta:
model = User
fields = ('first_name', 'last_name', 'is_active', 'groups', 'category',
2012-08-13 14:37:49 +02:00
'gender', 'type', 'committee', 'comment', 'default_password')
2011-09-03 17:17:29 +02:00
class UserUpdateForm(UserCreateForm):
2011-07-31 10:46:29 +02:00
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'is_active', 'groups',
'category', 'gender', 'type', 'committee', 'comment',
2012-08-13 14:37:49 +02:00
'default_password')
2011-07-31 10:46:29 +02:00
2011-09-03 17:17:29 +02:00
class GroupForm(forms.ModelForm, CssClassMixin):
permissions = LocalizedModelMultipleChoiceField(
queryset=Permission.objects.all(), label=_("Persmissions"),
required=False)
2012-08-11 11:36:55 +02:00
users = forms.ModelMultipleChoiceField(
queryset=User.objects.all(), label=_("Users"), 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
def save_m2m():
old_save_m2m()
instance.user_set.clear()
for user in self.cleaned_data['users']:
instance.user_set.add(user)
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() == 'anonymous':
# Editing the anonymous-user
if self.instance.name.lower() != data.lower():
raise forms.ValidationError(
_('You can not edit the name for the anonymous user'))
else:
if data.lower() == 'anonymous':
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):
2011-07-31 10:46:29 +02:00
class Meta:
model = User
2012-09-12 10:17:51 +02:00
fields = ('username', 'first_name', 'last_name', 'email', 'gender')
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=_("CSV File"))
class ConfigForm(forms.Form, CssClassMixin):
participant_pdf_system_url = forms.CharField(
widget=forms.TextInput(),
required=False,
label=_("System URL"),
help_text=_("Printed in PDF of first time passwords only."))
participant_pdf_welcometext = forms.CharField(
widget=forms.Textarea(),
required=False,
label=_("Welcome text"),
help_text=_("Printed in PDF of first time passwords only."))