From 9b3bec69d1bf91cd2aa838a611d79e64d1379f0b Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Fri, 10 Aug 2012 21:24:26 +0200 Subject: [PATCH] rewrote the passwort reset view as class base view --- openslides/participant/models.py | 1 + openslides/participant/urls.py | 23 +++++++++--------- openslides/participant/views.py | 40 +++++++++++++++++++++----------- openslides/utils/views.py | 17 +++++--------- 4 files changed, 45 insertions(+), 36 deletions(-) diff --git a/openslides/participant/models.py b/openslides/participant/models.py index 850b23806..09d2adbdf 100644 --- a/openslides/participant/models.py +++ b/openslides/participant/models.py @@ -51,6 +51,7 @@ class OpenSlidesUser(User, PersonMixin): comment = models.TextField( null=True, blank=True, verbose_name=_('Comment'), help_text=_('Only for notes.')) + # TODO: Rename this fild to default_password firstpassword = models.CharField( max_length=100, null=True, blank=True, verbose_name=_("First Password")) diff --git a/openslides/participant/urls.py b/openslides/participant/urls.py index 51ae2e301..584f880e7 100644 --- a/openslides/participant/urls.py +++ b/openslides/participant/urls.py @@ -15,7 +15,8 @@ from django.core.urlresolvers import reverse from openslides.participant.views import ( ParticipantsListPDF, ParticipantsPasswordsPDF, Overview, UserCreateView, - UserUpdateView, UserDeleteView, SetUserStatusView, UserImportView) + UserUpdateView, UserDeleteView, SetUserStatusView, UserImportView, + ResetPasswordView) urlpatterns = patterns('openslides.participant.views', url(r'^$', @@ -33,16 +34,21 @@ urlpatterns = patterns('openslides.participant.views', name='user_edit', ), - url(r'^print/$', - ParticipantsListPDF.as_view(), - name='user_print', - ), - url(r'^(?P\d+)/del/$', UserDeleteView.as_view(), name='user_delete', ), + url(r'^(?P\d+)/reset_password/$', + ResetPasswordView.as_view(), + name='user_reset_password', + ), + + url(r'^print/$', + ParticipantsListPDF.as_view(), + name='user_print', + ), + url(r'^(?P\d+)/status/toggle/$', SetUserStatusView.as_view(), {'action': 'toggle'}, @@ -86,11 +92,6 @@ urlpatterns = patterns('openslides.participant.views', name='user_group_delete', ), - url(r'^resetpassword/(?P\d+)/$', - 'reset_password', - name='user_reset_password', - ), - url(r'^passwords/print/$', ParticipantsPasswordsPDF.as_view(), name='print_passwords', diff --git a/openslides/participant/views.py b/openslides/participant/views.py index a0879c002..15e4e9eba 100644 --- a/openslides/participant/views.py +++ b/openslides/participant/views.py @@ -45,7 +45,7 @@ from openslides.utils.utils import ( encodedict, delete_default_permissions, html_strong) from openslides.utils.views import ( FormView, PDFView, TemplateView, CreateView, UpdateView, DeleteView, - RedirectView, SingleObjectMixin, ListView) + RedirectView, SingleObjectMixin, ListView, QuestionMixin) from openslides.config.models import config @@ -318,21 +318,33 @@ class UserImportView(FormView): return super(UserImportView, self).form_valid(form) -@permission_required('participant.can_manage_participant') -def reset_password(request, user_id): +class ResetPasswordView(RedirectView, SingleObjectMixin, QuestionMixin): """ - Reset the Password. + Set the Passwort for a user to his firstpassword. """ - user = User.objects.get(pk=user_id) - if request.method == 'POST': - user.profile.reset_password() - messages.success(request, - _('The Password for %s was successfully reset.') % user) - else: - gen_confirm_form(request, - _('Do you really want to reset the password for %s?') % user, - reverse('user_reset_password', args=[user_id])) - return redirect(reverse('user_edit', args=[user_id])) + permission_required = 'participant.can_manage_participant' + model = OpenSlidesUser + allow_ajax = True + question = ugettext_lazy('Do you really want to reset the password?') + + def get(self, request, *args, **kwargs): + self.object = self.get_object() + return super(ResetPasswordView, self).get(request, *args, **kwargs) + + def get_redirect_url(self, **kwargs): + return reverse('user_edit', args=[self.object.id]) + + def pre_redirect(self, request, *args, **kwargs): + self.confirm_form() + + def pre_post_redirect(self, request, *args, **kwargs): + if self.get_answer().lower() == 'yes': + self.object.reset_password() + messages.success(request, + _('The Password for %s was successfully reset.') % self.object) + + def get_answer_url(self): + return reverse('user_reset_password', args=[self.object.id]) @login_required diff --git a/openslides/utils/views.py b/openslides/utils/views.py index c7e398481..45a2b0e9d 100644 --- a/openslides/utils/views.py +++ b/openslides/utils/views.py @@ -107,18 +107,14 @@ class AjaxMixin(object): class QuestionMixin(object): question = ugettext_lazy('Are you sure?') + success_message = ugettext_lazy('Thank you for your answer') answer_options = [('yes', ugettext_lazy("Yes")), ('no', ugettext_lazy("No"))] def get_answer_options(self): return self.answer_options - def get_confirm_question(self): - return self.questions - - def get_success_message(self, option=None): - if option is None: - return _('Invalid answer') - return _('You choose %s') % option + def get_question(self): + return unicode(self.question) def get_answer(self): for option in self.get_answer_options(): @@ -141,7 +137,7 @@ class QuestionMixin(object): %(option_fields)s """ % { - 'message': self.get_confirm_question(), + 'message': self.get_question(), 'url': self.get_answer_url(), 'csrf': csrf(self.request)['csrf_token'], 'option_fields': option_fields}) @@ -150,8 +146,7 @@ class QuestionMixin(object): self.confirm_form(request, self.object) def pre_post_redirect(self, request, *args, **kwargs): - option = self.get_answer() - messages.success(request, self.get_success_message(option)) + messages.success(request) class TemplateView(PermissionMixin, _TemplateView): @@ -271,7 +266,7 @@ class CreateView(PermissionMixin, _CreateView): class DeleteView(RedirectView, SingleObjectMixin, QuestionMixin): - def get_confirm_question(self): + def get_question(self): return _('Do you really want to delete %s?') % html_strong(self.object) def get_success_message(self):