diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-04-11 13:29:15 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-04-11 13:29:15 +0200 |
commit | 9ff54c45e09015606754a7fcce824bc78aae9201 (patch) | |
tree | d5af30619dfbf69cd1ec700b8d996109fb511640 /ishtar_common/forms.py | |
parent | 5a298f37a4f87396d5fdd275c23b6589db84c06e (diff) | |
download | Ishtar-9ff54c45e09015606754a7fcce824bc78aae9201.tar.bz2 Ishtar-9ff54c45e09015606754a7fcce824bc78aae9201.zip |
🐛 document form: fix json fields management
Diffstat (limited to 'ishtar_common/forms.py')
-rw-r--r-- | ishtar_common/forms.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index 07d1ac417..1416dd464 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -308,9 +308,21 @@ class CustomForm(BSForm): c_field.order_number = idx fields[key] = c_field # custom field after classic fields + instance = getattr(self, "instance", None) for k in sorted(new_fields.keys()): alt_key, alt_field = new_fields.pop(k) alt_field.order_number = k + # manage initial data + if instance: + data = instance.data + nok = False + for k_part in alt_key[len("data__"):].split("__"): + if k_part not in data: + nok = True + break + data = data[k_part] + if not nok: + alt_field.initial = data fields[alt_key] = alt_field self.fields = fields self._post_init() @@ -464,6 +476,22 @@ class CustomForm(BSForm): customs.append((key, field.label)) return sorted(customs, key=lambda x: x[1]) + def save_json_fields(self, item): + """ + Edit JSON fields of an item after form validation. + Do not save. Return if item has been changed. + """ + changed = False + for key in self.cleaned_data: + if not key.startswith("data__"): + continue + item.data = update_data( + item.data, + generate_dict_from_data_string(key, self.cleaned_data[key]) + ) + changed = True + return changed + class CustomFormSearch(FormPermissionForm, forms.Form): """ |