Redesigned the QuestioMixin

This commit is contained in:
Oskar Hahn 2012-10-28 19:59:41 +01:00
parent 76534cf287
commit 79c5e93422
4 changed files with 63 additions and 63 deletions

View File

@ -177,12 +177,12 @@ class ItemDelete(DeleteView):
if self.get_answer() == 'all':
self.object.delete(with_children=True)
messages.success(request,
_("Item %s and his children were successfully deleted.") \
_("Item %s and his children were successfully deleted.")
% html_strong(self.object))
elif self.get_answer() == 'yes':
self.object.delete(with_children=False)
messages.success(request,
_("Item %s was successfully deleted.") \
_("Item %s was successfully deleted.")
% html_strong(self.object))

View File

@ -374,18 +374,16 @@ class SupportView(SingleObjectMixin, QuestionMixin, RedirectView):
pk_url_kwarg = 'motion_id'
support = True
def get_question(self):
if self.support:
return _('Do you really want to support this motion?')
else:
return _('Do you really want to unsupport this motion?')
def get(self, request, *args, **kwargs):
self.object = self.get_object()
return super(SupportView, self).get(request, *args, **kwargs)
def check_allowed_actions(self, request):
"""
Checks whether request.user can support or unsupport the motion.
Returns True or False.
"""
allowed_actions = self.get_object().get_allowed_actions(request.user)
allowed_actions = self.object.get_allowed_actions(request.user)
if self.support and not 'support' in allowed_actions:
messages.error(request, _('You can not support this motion.'))
return False
@ -399,27 +397,28 @@ class SupportView(SingleObjectMixin, QuestionMixin, RedirectView):
if self.check_allowed_actions(request):
super(SupportView, self).pre_redirect(request, *args, **kwargs)
def pre_post_redirect(self, request, *args, **kwargs):
motion = self.get_object()
if self.get_answer().lower() == 'yes' and self.check_allowed_actions(request):
def get_question(self):
if self.support:
motion.support(person=request.user)
self.success_message = _("You have supported this motion successfully.")
return _('Do you really want to support this motion?')
else:
motion.unsupport(person=request.user)
self.success_message = _("You have unsupported this motion successfully.")
messages.success(request, self.success_message)
return _('Do you really want to unsupport this motion?')
def case_yes(self):
if self.check_allowed_actions(self.request):
if self.support:
self.object.support(person=self.request.user)
else:
self.object.unsupport(person=self.request.user)
def get_success_message(self):
if self.support:
return _("You have supported this motion successfully.")
else:
return _("You have unsupported this motion successfully.")
def get_redirect_url(self, **kwargs):
return reverse('motion_view', args=[kwargs[self.pk_url_kwarg]])
def get_answer_url(self):
if self.support:
answer_url = 'motion_support'
else:
answer_url = 'motion_unsupport'
return reverse(answer_url, args=[self.kwargs[self.pk_url_kwarg]])
@permission_required('motion.can_manage_motion')
@template('motion/view.html')

View File

@ -320,7 +320,7 @@ class UserImportView(FormView):
return super(UserImportView, self).form_valid(form)
class ResetPasswordView(RedirectView, SingleObjectMixin, QuestionMixin):
class ResetPasswordView(SingleObjectMixin, QuestionMixin, RedirectView):
"""
Set the Passwort for a user to his default password.
"""
@ -336,17 +336,11 @@ class ResetPasswordView(RedirectView, SingleObjectMixin, QuestionMixin):
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':
def case_yes(self):
self.object.reset_password()
messages.success(request,
_('The Password for %s was successfully reset.') % html_strong(self.object))
def get_answer_url(self):
return reverse('user_reset_password', args=[self.object.id])
def get_success_message(self):
return _('The Password for %s was successfully reset.') % html_strong(self.object)
class GroupOverviewView(ListView):

View File

@ -110,20 +110,21 @@ class QuestionMixin(object):
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 pre_redirect(self, request, *args, **kwargs):
# Prints the question in a GET request
self.confirm_form()
def get_question(self):
return unicode(self.question)
def get_answer(self):
for option in self.get_answer_options():
if option[0] in self.request.POST:
return option[0]
return None
def get_answer_options(self):
return self.answer_options
def get_answer_url(self):
try:
return self.answer_url
except AttributeError:
return self.request.path
def confirm_form(self):
option_fields = "\n".join([
@ -142,11 +143,25 @@ class QuestionMixin(object):
'csrf': csrf(self.request)['csrf_token'],
'option_fields': option_fields})
def pre_redirect(self, request, *args, **kwargs):
self.confirm_form()
def pre_post_redirect(self, request, *args, **kwargs):
messages.success(request)
# Reacts on the response of the user in a POST-request.
# TODO: call the methodes for all possible answers.
if self.get_answer() == 'yes':
self.case_yes()
messages.success(request, self.get_success_message())
def get_answer(self):
for option in self.get_answer_options():
if option[0] in self.request.POST:
return option[0]
return None
def case_yes(self):
# TODO: raise a warning
pass
def get_success_message(self):
return self.success_message
class TemplateView(PermissionMixin, _TemplateView):
@ -266,27 +281,19 @@ class CreateView(PermissionMixin, _CreateView):
pass
class DeleteView(RedirectView, SingleObjectMixin, QuestionMixin):
def get_question(self):
return _('Do you really want to delete %s?') % html_strong(self.object)
def get_success_message(self):
return _('%s was successfully deleted.') % html_strong(self.object)
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.delete()
messages.success(request, self.get_success_message())
class DeleteView(SingleObjectMixin, QuestionMixin, RedirectView):
def get(self, request, *args, **kwargs):
self.object = self.get_object()
return super(DeleteView, self).get(request, *args, **kwargs)
def get_answer_url(self):
return self.object.get_absolute_url('delete')
def get_question(self):
return _('Do you really want to delete %s?') % html_strong(self.object)
def case_yes(self):
self.object.delete()
def get_success_message(self):
return _('%s was successfully deleted.') % html_strong(self.object)
class DetailView(TemplateView, SingleObjectMixin):