From ddb5756868f78c97d505374973f3512dc893c786 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Fri, 10 Aug 2012 13:22:09 +0200 Subject: [PATCH] rewrote the participant edit view as class based view --- openslides/participant/forms.py | 23 ++--- .../templates/participant/edit.html | 35 ++++---- openslides/participant/urls.py | 8 +- openslides/participant/views.py | 84 +++++-------------- openslides/utils/views.py | 9 ++ 5 files changed, 63 insertions(+), 96 deletions(-) diff --git a/openslides/participant/forms.py b/openslides/participant/forms.py index f39db40fb..ebd267bc0 100644 --- a/openslides/participant/forms.py +++ b/openslides/participant/forms.py @@ -28,7 +28,7 @@ USER_APPLICATION_IMPORT_OPTIONS = [ ] -class UserNewForm(forms.ModelForm, CssClassMixin): +class UserCreateForm(forms.ModelForm, CssClassMixin): first_name = forms.CharField(label=_("First name")) last_name = forms.CharField(label=_("Last name")) groups = forms.ModelMultipleChoiceField( @@ -37,22 +37,17 @@ class UserNewForm(forms.ModelForm, CssClassMixin): label=_("Active"), required=False, initial=True) class Meta: - model = User - exclude = ('username', 'password', 'is_staff', 'is_superuser', - 'last_login', 'date_joined', 'user_permissions') + model = OpenSlidesUser + fields = ('first_name', 'last_name', 'is_active', 'groups', 'category', + 'gender', 'type', 'committee', 'comment', 'firstpassword') -class UserEditForm(forms.ModelForm, CssClassMixin): - first_name = forms.CharField(label=_("First name")) - last_name = forms.CharField(label=_("Last name")) - groups = forms.ModelMultipleChoiceField( - queryset=Group.objects.all(), label=_("User groups"), required=False) - is_active = forms.BooleanField(label=_("Active"), required=False) - +class UserUpdateForm(UserCreateForm): class Meta: - model = User - exclude = ('password', 'is_staff', 'is_superuser', 'last_login', - 'date_joined', 'user_permissions') + model = OpenSlidesUser + fields = ('username', 'first_name', 'last_name', 'is_active', 'groups', + 'category', 'gender', 'type', 'committee', 'comment', + 'firstpassword') class UsernameForm(forms.ModelForm, CssClassMixin): diff --git a/openslides/participant/templates/participant/edit.html b/openslides/participant/templates/participant/edit.html index ca7c2b649..da910b19e 100644 --- a/openslides/participant/templates/participant/edit.html +++ b/openslides/participant/templates/participant/edit.html @@ -4,7 +4,7 @@ {% block title %} {{ block.super }} – - {% if edituser %} + {% if edit_user %} {% trans "Edit participant" %} {% else %} {% trans "New participant" %} @@ -13,30 +13,31 @@ {% block content %} - {% if edituser %} + {% if edit_user %}

{% trans "Edit participant" %}

{% else %}

{% trans "New participant" %}

{% endif %}
{% csrf_token %} - {{ userform.as_p }} - {{ profileform.as_p }} - {% if edituser %} -

{% trans 'Reset to First Password' %}

+ {{ form.as_p }} + {% if edit_user %} +

+ {% trans 'Reset to First Password' %} +

{% endif %}

- - - - - + + + + +

* {% trans "required" %}
diff --git a/openslides/participant/urls.py b/openslides/participant/urls.py index 2786161bb..c6e57e98c 100644 --- a/openslides/participant/urls.py +++ b/openslides/participant/urls.py @@ -14,7 +14,7 @@ from django.conf.urls.defaults import url, patterns from django.core.urlresolvers import reverse from openslides.participant.views import ( - ParticipantsListPDF, ParticipantsPasswordsPDF, Overview) + ParticipantsListPDF, ParticipantsPasswordsPDF, Overview, UserCreateView, UserUpdateView) urlpatterns = patterns('openslides.participant.views', url(r'^$', @@ -23,12 +23,12 @@ urlpatterns = patterns('openslides.participant.views', ), url(r'^new/$', - 'edit', + UserCreateView.as_view(), name='user_new', ), - url(r'^(?P\d+)/edit/$', - 'edit', + url(r'^(?P\d+)/edit/$', + UserUpdateView.as_view(), name='user_edit', ), diff --git a/openslides/participant/views.py b/openslides/participant/views.py index 7299398c2..e62d6c248 100644 --- a/openslides/participant/views.py +++ b/openslides/participant/views.py @@ -43,14 +43,15 @@ from openslides.utils.template import Tab from openslides.utils.utils import ( template, permission_required, gen_confirm_form, ajax_request, decodedict, encodedict, delete_default_permissions, html_strong) -from openslides.utils.views import FormView, PDFView, TemplateView +from openslides.utils.views import ( + FormView, PDFView, TemplateView, CreateView, UpdateView) from openslides.config.models import config from openslides.participant.models import OpenSlidesUser, OpenSlidesGroup from openslides.participant.api import gen_username, gen_password from openslides.participant.forms import ( - UserNewForm, UserEditForm, OpenSlidesUserForm, UsersettingsForm, + UserCreateForm, UserUpdateForm, OpenSlidesUserForm, UsersettingsForm, UserImportForm, GroupForm, AdminPasswordChangeForm, ConfigForm) @@ -139,69 +140,30 @@ class Overview(TemplateView): return context -@permission_required('participant.can_manage_participant') -@template('participant/edit.html') -def edit(request, user_id=None): +class UserCreateView(CreateView): """ - View to create and edit users. + Create a new participant. """ - if user_id is not None: - user = User.objects.get(id=user_id) - else: - user = None + permission_required = 'participant.can_manage_participant' + template_name = 'participant/edit.html' + model = OpenSlidesUser + context_object_name = 'edit_user' + form_class = UserCreateForm + success_url = 'user_overview' + apply_url = 'participant_edit' - if request.method == 'POST': - if user_id is None: - user_form = UserNewForm(request.POST, prefix="user") - openslides_user_form = OpenSlidesUserForm(request.POST, prefix="openslidesuser") - else: - user_form = UserEditForm(request.POST, instance=user, prefix="user") - openslides_user_form = OpenSlidesUserForm(request.POST, instance=user.openslidesuser, - prefix="openslidesuser") + def manipulate_object(self, form): + self.object.username = gen_username(form.cleaned_data['first_name'], form.cleaned_data['last_name']) - if user_form.is_valid() and openslides_user_form.is_valid(): - user = user_form.save(commit=False) - if user_id is None: - # TODO: call first_name and last_name though openslides_user - user.username = gen_username(user.first_name, user.last_name) - user.save() - openslides_user = user.openslidesuser - openslides_user_form = OpenSlidesUserForm(request.POST, instance=openslides_user, prefix="openslidesuser") - openslides_user_form.is_valid() - openslides_user = openslides_user_form.save(commit=False) - openslides_user.user = user - if user_id is None: - if not openslides_user.firstpassword: - openslides_user.firstpassword = gen_password() - openslides_user.user.set_password(openslides_user.firstpassword) - # TODO: Try not to save the user object - openslides_user.user.save() - openslides_user.save() - if user_id is None: - messages.success(request, - _('New participant was successfully created.')) - else: - messages.success(request, - _('Participant was successfully modified.')) - if not 'apply' in request.POST: - return redirect(reverse('user_overview')) - if user_id is None: - return redirect(reverse('user_edit', args=[user.id])) - else: - messages.error(request, _('Please check the form for errors.')) - else: - if user_id is None: - user_form = UserNewForm(prefix="user") - openslides_user_form = OpenSlidesUserForm(prefix="openslidesuser") - else: - user_form = UserEditForm(instance=user, prefix="user") - openslides_user_form = OpenSlidesUserForm(instance=user.openslidesuser, prefix="openslidesuser") - # TODO: rename template vars - return { - 'userform': user_form, - 'profileform': openslides_user_form, - 'edituser': user, - } + +class UserUpdateView(UpdateView): + permission_required = 'participant.can_manage_participant' + template_name = 'participant/edit.html' + model = OpenSlidesUser + context_object_name = 'edit_user' + form_class = UserUpdateForm + success_url = 'user_overview' + apply_url = 'participant_edit' @permission_required('participant.can_manage_participant') diff --git a/openslides/utils/views.py b/openslides/utils/views.py index 0c3667025..5dab27f3c 100644 --- a/openslides/utils/views.py +++ b/openslides/utils/views.py @@ -208,9 +208,18 @@ class CreateView(PermissionMixin, _CreateView): messages.error(self.request, _('Please check the form for errors.')) return super(CreateView, self).form_invalid(form) + def form_valid(self, form): + self.object = form.save(commit=False) + self.manipulate_object(form) + self.object.save() + return HttpResponseRedirect(self.get_success_url()) + def get_success_message(self): return _('%s was successfully created.') % html_strong(self.object) + def manipulate_object(self, form): + pass + class DeleteView(RedirectView, SingleObjectMixin): def get_confirm_question(self):