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")) first_name = forms.CharField(label=_("First name"))
last_name = forms.CharField(label=_("Last name")) last_name = forms.CharField(label=_("Last name"))
groups = forms.ModelMultipleChoiceField( groups = forms.ModelMultipleChoiceField(
@ -37,22 +37,17 @@ class UserNewForm(forms.ModelForm, CssClassMixin):
label=_("Active"), required=False, initial=True) label=_("Active"), required=False, initial=True)
class Meta: class Meta:
model = User model = OpenSlidesUser
exclude = ('username', 'password', 'is_staff', 'is_superuser', fields = ('first_name', 'last_name', 'is_active', 'groups', 'category',
'last_login', 'date_joined', 'user_permissions') 'gender', 'type', 'committee', 'comment', 'firstpassword')
class UserEditForm(forms.ModelForm, CssClassMixin): class UserUpdateForm(UserCreateForm):
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 Meta: class Meta:
model = User model = OpenSlidesUser
exclude = ('password', 'is_staff', 'is_superuser', 'last_login', fields = ('username', 'first_name', 'last_name', 'is_active', 'groups',
'date_joined', 'user_permissions') 'category', 'gender', 'type', 'committee', 'comment',
'firstpassword')
class UsernameForm(forms.ModelForm, CssClassMixin): class UsernameForm(forms.ModelForm, CssClassMixin):

View File

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

View File

@ -14,7 +14,7 @@ from django.conf.urls.defaults import url, patterns
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from openslides.participant.views import ( from openslides.participant.views import (
ParticipantsListPDF, ParticipantsPasswordsPDF, Overview) ParticipantsListPDF, ParticipantsPasswordsPDF, Overview, UserCreateView, UserUpdateView)
urlpatterns = patterns('openslides.participant.views', urlpatterns = patterns('openslides.participant.views',
url(r'^$', url(r'^$',
@ -23,12 +23,12 @@ urlpatterns = patterns('openslides.participant.views',
), ),
url(r'^new/$', url(r'^new/$',
'edit', UserCreateView.as_view(),
name='user_new', name='user_new',
), ),
url(r'^(?P<user_id>\d+)/edit/$', url(r'^(?P<pk>\d+)/edit/$',
'edit', UserUpdateView.as_view(),
name='user_edit', name='user_edit',
), ),

View File

@ -43,14 +43,15 @@ from openslides.utils.template import Tab
from openslides.utils.utils import ( from openslides.utils.utils import (
template, permission_required, gen_confirm_form, ajax_request, decodedict, template, permission_required, gen_confirm_form, ajax_request, decodedict,
encodedict, delete_default_permissions, html_strong) 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.config.models import config
from openslides.participant.models import OpenSlidesUser, OpenSlidesGroup from openslides.participant.models import OpenSlidesUser, OpenSlidesGroup
from openslides.participant.api import gen_username, gen_password from openslides.participant.api import gen_username, gen_password
from openslides.participant.forms import ( from openslides.participant.forms import (
UserNewForm, UserEditForm, OpenSlidesUserForm, UsersettingsForm, UserCreateForm, UserUpdateForm, OpenSlidesUserForm, UsersettingsForm,
UserImportForm, GroupForm, AdminPasswordChangeForm, ConfigForm) UserImportForm, GroupForm, AdminPasswordChangeForm, ConfigForm)
@ -139,69 +140,30 @@ class Overview(TemplateView):
return context return context
@permission_required('participant.can_manage_participant') class UserCreateView(CreateView):
@template('participant/edit.html')
def edit(request, user_id=None):
""" """
View to create and edit users. Create a new participant.
""" """
if user_id is not None: permission_required = 'participant.can_manage_participant'
user = User.objects.get(id=user_id) template_name = 'participant/edit.html'
else: model = OpenSlidesUser
user = None context_object_name = 'edit_user'
form_class = UserCreateForm
success_url = 'user_overview'
apply_url = 'participant_edit'
if request.method == 'POST': def manipulate_object(self, form):
if user_id is None: self.object.username = gen_username(form.cleaned_data['first_name'], form.cleaned_data['last_name'])
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")
if user_form.is_valid() and openslides_user_form.is_valid():
user = user_form.save(commit=False) class UserUpdateView(UpdateView):
if user_id is None: permission_required = 'participant.can_manage_participant'
# TODO: call first_name and last_name though openslides_user template_name = 'participant/edit.html'
user.username = gen_username(user.first_name, user.last_name) model = OpenSlidesUser
user.save() context_object_name = 'edit_user'
openslides_user = user.openslidesuser form_class = UserUpdateForm
openslides_user_form = OpenSlidesUserForm(request.POST, instance=openslides_user, prefix="openslidesuser") success_url = 'user_overview'
openslides_user_form.is_valid() apply_url = 'participant_edit'
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,
}
@permission_required('participant.can_manage_participant') @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.')) messages.error(self.request, _('Please check the form for errors.'))
return super(CreateView, self).form_invalid(form) 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): def get_success_message(self):
return _('%s was successfully created.') % html_strong(self.object) return _('%s was successfully created.') % html_strong(self.object)
def manipulate_object(self, form):
pass
class DeleteView(RedirectView, SingleObjectMixin): class DeleteView(RedirectView, SingleObjectMixin):
def get_confirm_question(self): def get_confirm_question(self):