2011-07-31 10:46:29 +02:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
openslides.participant.forms
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Forms for the participant app.
|
|
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
|
:copyright: 2011–2013 by OpenSlides team, see AUTHORS.
|
2011-07-31 10:46:29 +02:00
|
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
|
"""
|
|
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
|
from django import forms
|
2012-08-11 10:51:52 +02:00
|
|
|
|
from django.contrib.auth.models import Permission
|
2013-03-01 17:13:12 +01:00
|
|
|
|
from django.utils.translation import ugettext_lazy as _ # TODO: Change this in the code
|
2012-12-16 17:26:53 +01:00
|
|
|
|
from django.conf import settings
|
2011-11-14 20:45:04 +01:00
|
|
|
|
|
2012-08-08 10:34:23 +02:00
|
|
|
|
from openslides.utils.forms import (
|
|
|
|
|
CssClassMixin, LocalizedModelMultipleChoiceField)
|
2011-11-14 20:45:04 +01:00
|
|
|
|
|
2012-08-12 12:52:38 +02:00
|
|
|
|
from openslides.participant.models import User, Group
|
2013-03-12 20:58:22 +01:00
|
|
|
|
from openslides.participant.api import get_registered_group
|
2012-04-12 19:11:07 +02:00
|
|
|
|
|
2012-07-10 13:19:12 +02:00
|
|
|
|
|
2012-08-10 13:22:09 +02:00
|
|
|
|
class UserCreateForm(forms.ModelForm, CssClassMixin):
|
2012-08-08 10:34:23 +02:00
|
|
|
|
groups = forms.ModelMultipleChoiceField(
|
2012-08-12 12:52:38 +02:00
|
|
|
|
queryset=Group.objects.exclude(name__iexact='anonymous'),
|
2012-11-07 22:16:11 +01:00
|
|
|
|
label=_("Groups"), required=False)
|
2011-09-04 09:57:23 +02:00
|
|
|
|
|
2012-11-22 18:39:54 +01:00
|
|
|
|
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()
|
2012-11-22 18:39:54 +01:00
|
|
|
|
initial['groups'] = [registered.pk]
|
|
|
|
|
super(UserCreateForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
2011-09-04 09:57:23 +02:00
|
|
|
|
class Meta:
|
2012-08-12 12:52:38 +02:00
|
|
|
|
model = User
|
2012-11-19 23:30:09 +01:00
|
|
|
|
fields = ('first_name', 'last_name', 'is_active', 'groups', 'structure_level',
|
2012-11-03 09:34:42 +01:00
|
|
|
|
'gender', 'type', 'committee', 'about_me', 'comment', 'default_password')
|
2011-09-04 09:57:23 +02:00
|
|
|
|
|
2011-09-03 17:17:29 +02:00
|
|
|
|
|
2012-08-10 13:22:09 +02:00
|
|
|
|
class UserUpdateForm(UserCreateForm):
|
2011-07-31 10:46:29 +02:00
|
|
|
|
class Meta:
|
2012-08-12 12:52:38 +02:00
|
|
|
|
model = User
|
2012-08-10 13:22:09 +02:00
|
|
|
|
fields = ('username', 'first_name', 'last_name', 'is_active', 'groups',
|
2012-11-19 23:30:09 +01:00
|
|
|
|
'structure_level', 'gender', 'type', 'committee', 'about_me', '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
|
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
|
class GroupForm(forms.ModelForm, CssClassMixin):
|
|
|
|
|
permissions = LocalizedModelMultipleChoiceField(
|
2012-10-30 22:16:43 +01:00
|
|
|
|
queryset=Permission.objects.all(), label=_("Permissions"),
|
2012-08-11 10:09:54 +02:00
|
|
|
|
required=False)
|
2012-08-11 11:36:55 +02:00
|
|
|
|
users = forms.ModelMultipleChoiceField(
|
2012-10-30 22:16:43 +01:00
|
|
|
|
queryset=User.objects.all(), label=_("Participants"), required=False)
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
2012-04-14 18:53:18 +02:00
|
|
|
|
def __init__(self, *args, **kwargs):
|
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)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-08-11 10:09:54 +02:00
|
|
|
|
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
|
2012-08-11 10:09:54 +02:00
|
|
|
|
data = self.cleaned_data['name']
|
2012-11-22 18:39:54 +01:00
|
|
|
|
if self.instance.name.lower() in ['anonymous', 'registered']:
|
2012-08-11 10:09:54 +02:00
|
|
|
|
# Editing the anonymous-user
|
|
|
|
|
if self.instance.name.lower() != data.lower():
|
|
|
|
|
raise forms.ValidationError(
|
2012-11-22 18:39:54 +01:00
|
|
|
|
_('You can not edit the name for this group.'))
|
2012-08-11 10:09:54 +02:00
|
|
|
|
else:
|
2012-11-22 18:39:54 +01:00
|
|
|
|
if data.lower() in ['anonymous', 'registered']:
|
2012-08-11 10:09:54 +02:00
|
|
|
|
raise forms.ValidationError(
|
|
|
|
|
_('Group name "%s" is reserved for internal use.') % data)
|
|
|
|
|
return data
|
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
|
class Meta:
|
2012-08-12 12:52:38 +02:00
|
|
|
|
model = Group
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
2012-04-14 14:31:09 +02:00
|
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
|
class UsersettingsForm(forms.ModelForm, CssClassMixin):
|
2012-12-16 17:26:53 +01:00
|
|
|
|
language = forms.ChoiceField(choices=settings.LANGUAGES)
|
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
|
class Meta:
|
2012-08-12 12:52:38 +02:00
|
|
|
|
model = User
|
2012-11-24 14:01:21 +01:00
|
|
|
|
fields = ('username', '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
|
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
|
class UserImportForm(forms.Form, CssClassMixin):
|
2012-08-08 10:34:23 +02:00
|
|
|
|
csvfile = forms.FileField(widget=forms.FileInput(attrs={'size': '50'}),
|
|
|
|
|
label=_("CSV File"))
|