rewrote the participant edit view as class based view

This commit is contained in:
Oskar Hahn 2012-08-10 13:22:09 +02:00
parent 1832a0c125
commit ddb5756868
5 changed files with 63 additions and 96 deletions

View File

@ -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):

View File

@ -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 %}
<h1>{% trans "Edit participant" %}</h1>
{% else %}
<h1>{% trans "New participant" %}</h1>
{% endif %}
<form action="" method="post">{% csrf_token %}
{{ userform.as_p }}
{{ profileform.as_p }}
{% if edituser %}
<p><a href="{% url user_reset_password edituser.id %}">{% trans 'Reset to First Password' %}</a></p>
{{ form.as_p }}
{% if edit_user %}
<p>
<a href="{% url user_reset_password edit_user.id %}">{% trans 'Reset to First Password' %}</a>
</p>
{% endif %}
<p>
<button class="button" type="submit">
<span class="icon ok">{% trans 'Save' %}</span>
</button>
<button class="button" type="submit" name="apply">
<span class="icon apply">{% trans 'Apply' %}</span>
</button>
<a href='{% url user_overview %}'>
<button class="button" type="button" onclick="window.location='{% url user_overview %}'">
<span class="icon cancel">{% trans 'Cancel' %}</span>
</button>
</a>
<button class="button" type="submit">
<span class="icon ok">{% trans 'Save' %}</span>
</button>
<button class="button" type="submit" name="apply">
<span class="icon apply">{% trans 'Apply' %}</span>
</button>
<a href='{% url user_overview %}'>
<button class="button" type="button" onclick="window.location='{% url user_overview %}'">
<span class="icon cancel">{% trans 'Cancel' %}</span>
</button>
</a>
</p>
<small>* {% trans "required" %}</small>
</form>

View File

@ -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<user_id>\d+)/edit/$',
'edit',
url(r'^(?P<pk>\d+)/edit/$',
UserUpdateView.as_view(),
name='user_edit',
),

View File

@ -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')

View File

@ -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):