From c410be9b3c3ba193ee8c233cc6a50d065d4090fd Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 21 Nov 2017 10:46:58 +0100 Subject: Custom forms: disable completly a form --- ishtar_common/wizards.py | 55 +++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 22 deletions(-) (limited to 'ishtar_common/wizards.py') diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py index f86e03df0..e82b32671 100644 --- a/ishtar_common/wizards.py +++ b/ishtar_common/wizards.py @@ -34,37 +34,19 @@ from django.db.models.fields.files import FileField, ImageFieldFile from django.db.models.fields.related import ManyToManyField from django.db.models.fields import NOT_PROVIDED -from django.http import HttpResponseRedirect +from django.http import HttpResponseRedirect, Http404 from django.forms import ValidationError from django.shortcuts import redirect, render from django.template import loader -from django.utils.datastructures import MultiValueDict as BaseMultiValueDict from django.utils.translation import ugettext_lazy as _ from ishtar_common import models -from ishtar_common.utils import get_all_field_names +from ishtar_common.forms import CustomForm +from ishtar_common.utils import get_all_field_names, MultiValueDict logger = logging.getLogger(__name__) -class MultiValueDict(BaseMultiValueDict): - def get(self, *args, **kwargs): - v = super(MultiValueDict, self).getlist(*args, **kwargs) - if callable(v): - v = v() - if type(v) in (list, tuple) and len(v) > 1: - v = ",".join(v) - elif type(v) not in (int, unicode): - v = super(MultiValueDict, self).get(*args, **kwargs) - return v - - def getlist(self, *args, **kwargs): - lst = super(MultiValueDict, self).getlist(*args, **kwargs) - if type(lst) not in (tuple, list): - lst = [lst] - return lst - - def check_rights(rights=[], redirect_url='/'): """ Decorator that checks the rights to access the view. @@ -125,6 +107,19 @@ def _check_right(step, condition=True): """ +def filter_no_fields_form(form, other_check=None): + def func(self): + if issubclass(form, CustomForm): + enabled, exc = form.check_availability_and_excluded_fields( + self.request.user.ishtaruser) + if not enabled: + return False + if other_check: + return other_check(self) + return True + return func + + class Wizard(NamedUrlWizardView): model = None label = '' @@ -155,6 +150,19 @@ class Wizard(NamedUrlWizardView): self.condition_dict[form_key] = cond ''' + @classmethod + def get_initkwargs(cls, *args, **kwargs): + kwargs = super(Wizard, cls).get_initkwargs(*args, **kwargs) + # remove + for form_key in kwargs['form_list']: + form = kwargs['form_list'][form_key] + other_check = None + if form_key in kwargs['condition_dict']: + other_check = kwargs['condition_dict'][form_key] + kwargs['condition_dict'][form_key] = filter_no_fields_form( + form, other_check) + return kwargs + def dispatch(self, request, *args, **kwargs): self.current_right = kwargs.get('current_right', None) @@ -813,7 +821,10 @@ class Wizard(NamedUrlWizardView): data = data.copy() if not step: step = self.steps.current - form = self.get_form_list()[step] + try: + form = self.get_form_list()[step] + except KeyError: + raise Http404() if hasattr(form, 'management_form'): # manage deletion to_delete, not_to_delete = self.get_deleted(data.keys()) -- cgit v1.2.3