diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-05-23 17:13:35 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-06-12 08:49:06 +0200 | 
| commit | d807e2b7b96935174135bc76b6a22f3a0100bd95 (patch) | |
| tree | 427ba1a5c67ec7d578549f64dbd3496d72ec37ca /ishtar_common/wizards.py | |
| parent | 88c9aeaeafd9bc77decbf0982be0fd9d20fd3993 (diff) | |
| download | Ishtar-d807e2b7b96935174135bc76b6a22f3a0100bd95.tar.bz2 Ishtar-d807e2b7b96935174135bc76b6a22f3a0100bd95.zip | |
Wizards - JSON fields: Manage field display in forms - management in wizards save and form init (refs #4089)
Diffstat (limited to 'ishtar_common/wizards.py')
| -rw-r--r-- | ishtar_common/wizards.py | 45 | 
1 files changed, 43 insertions, 2 deletions
| diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py index a6844b674..40044f100 100644 --- a/ishtar_common/wizards.py +++ b/ishtar_common/wizards.py @@ -120,8 +120,12 @@ def filter_no_fields_form(form, other_check=None):          if not hasattr(self.request.user, 'ishtaruser'):              return False          if issubclass(form, CustomForm): -            enabled, exc = form.check_availability_and_excluded_fields( +            enabled, excluded, json_fields = form.check_custom_form(                  self.request.user.ishtaruser) +            if not hasattr(self, 'json_fields'): +                self.json_fields = {} +            self.json_fields[form.form_slug] = [ +                key for order, key, field in json_fields]              if not enabled:                  return False          if other_check: @@ -595,11 +599,27 @@ class Wizard(NamedUrlWizardView):                     return_object):          dct = self.get_extra_model(dct, form_list)          obj = self.get_current_saved_object() -        # manage dependant items +        data = {} +        if obj: +            data = obj.data + +        # manage dependant items and json fields          other_objs = {}          for k in dct.keys():              if '__' not in k:                  continue +            # manage json field +            if k.startswith('data__'): +                data_keys = k[len('data__'):].split('__') +                # tree +                current_data = data +                for data_key in data_keys[:-1]: +                    if data_key not in current_data: +                        current_data[data_key] = {} +                    current_data = current_data[data_key] +                current_data[data_keys[-1]] = dct.pop(k) +                continue +              vals = k.split('__')              assert len(vals) == 2, \                  "Only one level of dependant item is managed" @@ -627,6 +647,7 @@ class Wizard(NamedUrlWizardView):                      elif type(dct[k]) not in (list, tuple):                          dct[k] = [dct[k]]                  setattr(obj, k, dct[k]) +            obj.data = data              if hasattr(obj, 'pre_save'):                  obj.pre_save()              try: @@ -711,6 +732,7 @@ class Wizard(NamedUrlWizardView):              except ValidationError as e:                  logger.warning(unicode(e))                  return self.render(form_list[-1]) +            obj.data = data              obj.save(**saved_args)              for k in adds:                  getattr(obj, k).add(adds[k]) @@ -1243,6 +1265,25 @@ class Wizard(NamedUrlWizardView):          Get initial data from an object: simple form          """          initial = MultiValueDict() + +        # manage json field +        if hasattr(self, 'json_fields') \ +                and getattr(c_form, 'form_slug', None) \ +                and c_form.form_slug in self.json_fields \ +                and obj.data: +            for key in self.json_fields[c_form.form_slug]: +                if not key.startswith('data__'): +                    continue +                json_keys = key[len('data__'):].split('__') +                value = obj.data +                for json_key in json_keys: +                    if json_key not in value: +                        value = None +                        break +                    value = value[json_key] +                if value: +                    initial[key] = value +          for base_field in c_form.base_fields.keys():              value = obj              base_model = None | 
