rewrote the passwort reset view as class base view

This commit is contained in:
Oskar Hahn 2012-08-10 21:24:26 +02:00
parent 87af568eeb
commit 9b3bec69d1
4 changed files with 45 additions and 36 deletions

View File

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

View File

@ -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<pk>\d+)/del/$',
UserDeleteView.as_view(),
name='user_delete',
),
url(r'^(?P<pk>\d+)/reset_password/$',
ResetPasswordView.as_view(),
name='user_reset_password',
),
url(r'^print/$',
ParticipantsListPDF.as_view(),
name='user_print',
),
url(r'^(?P<pk>\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<user_id>\d+)/$',
'reset_password',
name='user_reset_password',
),
url(r'^passwords/print/$',
ParticipantsPasswordsPDF.as_view(),
name='print_passwords',

View File

@ -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 <b>%s</b> was successfully reset.') % user)
else:
gen_confirm_form(request,
_('Do you really want to reset the password for <b>%s</b>?') % 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 <b>%s</b> was successfully reset.') % self.object)
def get_answer_url(self):
return reverse('user_reset_password', args=[self.object.id])
@login_required

View File

@ -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
</form>
""" % {
'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):