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': if self.get_answer() == 'all':
self.object.delete(with_children=True) self.object.delete(with_children=True)
messages.success(request, messages.success(request,
_("Item %s and his children were successfully deleted.") \ _("Item %s and his children were successfully deleted.")
% html_strong(self.object)) % html_strong(self.object))
elif self.get_answer() == 'yes': elif self.get_answer() == 'yes':
self.object.delete(with_children=False) self.object.delete(with_children=False)
messages.success(request, messages.success(request,
_("Item %s was successfully deleted.") \ _("Item %s was successfully deleted.")
% html_strong(self.object)) % html_strong(self.object))

View File

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

View File

@ -320,7 +320,7 @@ class UserImportView(FormView):
return super(UserImportView, self).form_valid(form) 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. 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): def get_redirect_url(self, **kwargs):
return reverse('user_edit', args=[self.object.id]) return reverse('user_edit', args=[self.object.id])
def pre_redirect(self, request, *args, **kwargs): def case_yes(self):
self.confirm_form()
def pre_post_redirect(self, request, *args, **kwargs):
if self.get_answer().lower() == 'yes':
self.object.reset_password() self.object.reset_password()
messages.success(request,
_('The Password for %s was successfully reset.') % html_strong(self.object))
def get_answer_url(self): def get_success_message(self):
return reverse('user_reset_password', args=[self.object.id]) return _('The Password for %s was successfully reset.') % html_strong(self.object)
class GroupOverviewView(ListView): class GroupOverviewView(ListView):

View File

@ -110,20 +110,21 @@ class QuestionMixin(object):
success_message = ugettext_lazy('Thank you for your answer') 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 pre_redirect(self, request, *args, **kwargs):
return self.answer_options # Prints the question in a GET request
self.confirm_form()
def get_question(self): def get_question(self):
return unicode(self.question) return unicode(self.question)
def get_answer(self): def get_answer_options(self):
for option in self.get_answer_options(): return self.answer_options
if option[0] in self.request.POST:
return option[0]
return None
def get_answer_url(self): def get_answer_url(self):
try:
return self.answer_url return self.answer_url
except AttributeError:
return self.request.path
def confirm_form(self): def confirm_form(self):
option_fields = "\n".join([ option_fields = "\n".join([
@ -142,11 +143,25 @@ class QuestionMixin(object):
'csrf': csrf(self.request)['csrf_token'], 'csrf': csrf(self.request)['csrf_token'],
'option_fields': option_fields}) 'option_fields': option_fields})
def pre_redirect(self, request, *args, **kwargs):
self.confirm_form()
def pre_post_redirect(self, request, *args, **kwargs): 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): class TemplateView(PermissionMixin, _TemplateView):
@ -266,27 +281,19 @@ class CreateView(PermissionMixin, _CreateView):
pass pass
class DeleteView(RedirectView, SingleObjectMixin, QuestionMixin): class DeleteView(SingleObjectMixin, QuestionMixin, RedirectView):
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())
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
self.object = self.get_object() self.object = self.get_object()
return super(DeleteView, self).get(request, *args, **kwargs) return super(DeleteView, self).get(request, *args, **kwargs)
def get_answer_url(self): def get_question(self):
return self.object.get_absolute_url('delete') 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): class DetailView(TemplateView, SingleObjectMixin):