summaryrefslogtreecommitdiff
path: root/ishtar_common/forms.py
diff options
context:
space:
mode:
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
commit35b80da2036e2fcd24ffad3ce5844c2a5db266b2 (patch)
tree348545788363e80b70d18856d28182242b82e9aa /ishtar_common/forms.py
parent315500888834385e35bf06220c1cbf7812a858ce (diff)
downloadIshtar-35b80da2036e2fcd24ffad3ce5844c2a5db266b2.tar.bz2
Ishtar-35b80da2036e2fcd24ffad3ce5844c2a5db266b2.zip
Cache dynamic choices
Diffstat (limited to 'ishtar_common/forms.py')
-rw-r--r--ishtar_common/forms.py34
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