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( comment = models.TextField(
null=True, blank=True, verbose_name=_('Comment'), null=True, blank=True, verbose_name=_('Comment'),
help_text=_('Only for notes.')) help_text=_('Only for notes.'))
# TODO: Rename this fild to default_password
firstpassword = models.CharField( firstpassword = models.CharField(
max_length=100, null=True, blank=True, max_length=100, null=True, blank=True,
verbose_name=_("First Password")) verbose_name=_("First Password"))

View File

@ -15,7 +15,8 @@ from django.core.urlresolvers import reverse
from openslides.participant.views import ( from openslides.participant.views import (
ParticipantsListPDF, ParticipantsPasswordsPDF, Overview, UserCreateView, ParticipantsListPDF, ParticipantsPasswordsPDF, Overview, UserCreateView,
UserUpdateView, UserDeleteView, SetUserStatusView, UserImportView) UserUpdateView, UserDeleteView, SetUserStatusView, UserImportView,
ResetPasswordView)
urlpatterns = patterns('openslides.participant.views', urlpatterns = patterns('openslides.participant.views',
url(r'^$', url(r'^$',
@ -33,16 +34,21 @@ urlpatterns = patterns('openslides.participant.views',
name='user_edit', name='user_edit',
), ),
url(r'^print/$',
ParticipantsListPDF.as_view(),
name='user_print',
),
url(r'^(?P<pk>\d+)/del/$', url(r'^(?P<pk>\d+)/del/$',
UserDeleteView.as_view(), UserDeleteView.as_view(),
name='user_delete', 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/$', url(r'^(?P<pk>\d+)/status/toggle/$',
SetUserStatusView.as_view(), SetUserStatusView.as_view(),
{'action': 'toggle'}, {'action': 'toggle'},
@ -86,11 +92,6 @@ urlpatterns = patterns('openslides.participant.views',
name='user_group_delete', name='user_group_delete',
), ),
url(r'^resetpassword/(?P<user_id>\d+)/$',
'reset_password',
name='user_reset_password',
),
url(r'^passwords/print/$', url(r'^passwords/print/$',
ParticipantsPasswordsPDF.as_view(), ParticipantsPasswordsPDF.as_view(),
name='print_passwords', name='print_passwords',

View File

@ -45,7 +45,7 @@ from openslides.utils.utils import (
encodedict, delete_default_permissions, html_strong) encodedict, delete_default_permissions, html_strong)
from openslides.utils.views import ( from openslides.utils.views import (
FormView, PDFView, TemplateView, CreateView, UpdateView, DeleteView, FormView, PDFView, TemplateView, CreateView, UpdateView, DeleteView,
RedirectView, SingleObjectMixin, ListView) RedirectView, SingleObjectMixin, ListView, QuestionMixin)
from openslides.config.models import config from openslides.config.models import config
@ -318,21 +318,33 @@ class UserImportView(FormView):
return super(UserImportView, self).form_valid(form) return super(UserImportView, self).form_valid(form)
@permission_required('participant.can_manage_participant') class ResetPasswordView(RedirectView, SingleObjectMixin, QuestionMixin):
def reset_password(request, user_id):
""" """
Reset the Password. Set the Passwort for a user to his firstpassword.
""" """
user = User.objects.get(pk=user_id) permission_required = 'participant.can_manage_participant'
if request.method == 'POST': model = OpenSlidesUser
user.profile.reset_password() allow_ajax = True
messages.success(request, question = ugettext_lazy('Do you really want to reset the password?')
_('The Password for <b>%s</b> was successfully reset.') % user)
else: def get(self, request, *args, **kwargs):
gen_confirm_form(request, self.object = self.get_object()
_('Do you really want to reset the password for <b>%s</b>?') % user, return super(ResetPasswordView, self).get(request, *args, **kwargs)
reverse('user_reset_password', args=[user_id]))
return redirect(reverse('user_edit', args=[user_id])) 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 @login_required

View File

@ -107,18 +107,14 @@ class AjaxMixin(object):
class QuestionMixin(object): class QuestionMixin(object):
question = ugettext_lazy('Are you sure?') 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"))] answer_options = [('yes', ugettext_lazy("Yes")), ('no', ugettext_lazy("No"))]
def get_answer_options(self): def get_answer_options(self):
return self.answer_options return self.answer_options
def get_confirm_question(self): def get_question(self):
return self.questions return unicode(self.question)
def get_success_message(self, option=None):
if option is None:
return _('Invalid answer')
return _('You choose %s') % option
def get_answer(self): def get_answer(self):
for option in self.get_answer_options(): for option in self.get_answer_options():
@ -141,7 +137,7 @@ class QuestionMixin(object):
%(option_fields)s %(option_fields)s
</form> </form>
""" % { """ % {
'message': self.get_confirm_question(), 'message': self.get_question(),
'url': self.get_answer_url(), 'url': self.get_answer_url(),
'csrf': csrf(self.request)['csrf_token'], 'csrf': csrf(self.request)['csrf_token'],
'option_fields': option_fields}) 'option_fields': option_fields})
@ -150,8 +146,7 @@ class QuestionMixin(object):
self.confirm_form(request, self.object) self.confirm_form(request, self.object)
def pre_post_redirect(self, request, *args, **kwargs): def pre_post_redirect(self, request, *args, **kwargs):
option = self.get_answer() messages.success(request)
messages.success(request, self.get_success_message(option))
class TemplateView(PermissionMixin, _TemplateView): class TemplateView(PermissionMixin, _TemplateView):
@ -271,7 +266,7 @@ class CreateView(PermissionMixin, _CreateView):
class DeleteView(RedirectView, SingleObjectMixin, QuestionMixin): 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) return _('Do you really want to delete %s?') % html_strong(self.object)
def get_success_message(self): def get_success_message(self):