diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-03-20 11:16:50 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-03-20 11:16:50 +0100 |
commit | 0ea89f12deae7c59b0f5901662c6a519a8f1e017 (patch) | |
tree | 348545788363e80b70d18856d28182242b82e9aa /ishtar_common/forms.py | |
parent | ac80884a42b5fd6032dff409346387616287abd5 (diff) | |
download | Ishtar-0ea89f12deae7c59b0f5901662c6a519a8f1e017.tar.bz2 Ishtar-0ea89f12deae7c59b0f5901662c6a519a8f1e017.zip |
Cache dynamic choices
Diffstat (limited to 'ishtar_common/forms.py')
-rw-r--r-- | ishtar_common/forms.py | 34 |
1 files changed, 13 insertions, 21 deletions
diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index 83b0b2b69..8d0d80497 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -26,9 +26,9 @@ import re import types from django import forms +from django.apps import apps from django.conf import settings from django.contrib.auth.models import User -from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ValidationError from django.core.urlresolvers import reverse from django.core import validators @@ -258,18 +258,8 @@ class CustomForm(BSForm): if app_name == "archaeological_files_pdl": app_name = "archaeological_files" model_name = cls.form_slug.split("-")[0].replace('_', "") - ct = ContentType.objects.get(app_label=app_name, model=model_name) - ct_class = ct.model_class() - choices = set() - splitted_key = key[len('data__'):].split('__') - for obj in ct_class.objects.filter( - data__has_key=key[len('data__'):]).all(): - value = obj.data - for k in splitted_key: - value = value[k] - choices.add(value) - choices = [('', '')] + [(v, v) for v in sorted(list(choices))] - return choices + ct_class = apps.get_model(app_name, model_name) + return ct_class._get_dynamic_choices(key) @classmethod def _get_json_fields(cls, custom_form): @@ -279,16 +269,18 @@ class CustomForm(BSForm): :return: ((order1, key1, field1), ...) """ fields = [] - for field in custom_form.json_fields.order_by('order').all(): - key = "data__" + field.json_field.key + for field in custom_form.json_fields.values( + "order", "label", "json_field__name", "json_field__value_type", + "help_text", "json_field__key").order_by('order').all(): + key = "data__" + field["json_field__key"] field_cls, widget = forms.CharField, None - if field.json_field.value_type in JSON_VALUE_TYPES_FIELDS: + if field["json_field__value_type"] in JSON_VALUE_TYPES_FIELDS: field_cls, widget = JSON_VALUE_TYPES_FIELDS[ - field.json_field.value_type] - attrs = {'label': field.label or field.json_field.name, + field["json_field__value_type"]] + attrs = {'label': field["label"] or field["json_field__name"], 'required': False} - if field.help_text: - attrs['help_text'] = field.help_text + if field["help_text"]: + attrs['help_text'] = field["help_text"] if widget: attrs['widget'] = widget() if field_cls == widgets.Select2DynamicField: @@ -299,7 +291,7 @@ class CustomForm(BSForm): kls = f.widget.attrs['class'] + " " + kls f.widget.attrs['class'] = kls f.alt_name = slugify(attrs['label']) - fields.append((field.order or 1, key, f)) + fields.append((field["order"] or 1, key, f)) return fields @classmethod |