From c2d60a414e788d3802c01883681480a4cfe7308c Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Wed, 28 Feb 2018 17:20:57 +0100 Subject: Forms: manage headers definition in code --- archaeological_finds/forms.py | 2 +- ishtar_common/forms.py | 13 +++++++++++++ ishtar_common/templates/blocks/bs_form_snippet.html | 9 +++++++-- ishtar_common/templatetags/from_dict.py | 5 +++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/archaeological_finds/forms.py b/archaeological_finds/forms.py index 191c8722a..8ecd10ff5 100644 --- a/archaeological_finds/forms.py +++ b/archaeological_finds/forms.py @@ -41,7 +41,7 @@ import models from ishtar_common.forms import FormSet, FloatField, \ get_form_selection, reverse_lazy, TableSelect, get_now, FinalForm, \ - ManageOldType, FieldType, IshtarForm + ManageOldType, FieldType, IshtarForm, FormHeader from ishtar_common.forms_common import get_town_field, \ get_image_help, SourceSelect, CustomForm diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index 0c8427d9f..c4d6f2026 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -30,6 +30,7 @@ from django.core import validators from django.forms.formsets import BaseFormSet, DELETION_FIELD_NAME from django.utils import formats, translation from django.utils.functional import lazy +from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ from bootstrap_datepicker.widgets import DatePicker, DATE_FORMAT, DateField @@ -265,8 +266,20 @@ class FieldType(object): return self.model.get_help(**args) +class FormHeader(object): + def __init__(self, label, level=4): + self.label = label + self.level = level + + def render(self): + return mark_safe(u"{label}".format( + label=self.label, level=self.level + )) + + class IshtarForm(forms.Form): TYPES = [] # FieldType list + HEADERS = {} # field key associated to FormHeader instance def __init__(self, *args, **kwargs): super(IshtarForm, self).__init__(*args, **kwargs) diff --git a/ishtar_common/templates/blocks/bs_form_snippet.html b/ishtar_common/templates/blocks/bs_form_snippet.html index a4d772236..cc17b45eb 100644 --- a/ishtar_common/templates/blocks/bs_form_snippet.html +++ b/ishtar_common/templates/blocks/bs_form_snippet.html @@ -1,4 +1,4 @@ -{% load i18n %} +{% load i18n from_dict %} {% if form.non_field_errors %} {% endif %} +

{{field.name|from_dict:form.HEADERS|call:'render'}}

+
-{% if not search and forloop.counter0|divisibleby:2 or search and forloop.counter1|divisibleby:2 %} +{% elif not search and forloop.counter0|divisibleby:2 or search and forloop.counter1|divisibleby:2 %} {% if forloop.counter0 %}
{% endif %}
diff --git a/ishtar_common/templatetags/from_dict.py b/ishtar_common/templatetags/from_dict.py index 927a79cf1..c64190ab1 100644 --- a/ishtar_common/templatetags/from_dict.py +++ b/ishtar_common/templatetags/from_dict.py @@ -11,3 +11,8 @@ def from_dict(value, dct): if not dct or value not in dct: return '' return dct[value] + + +@register.filter +def call(value, call): + return getattr(value, call)() -- cgit v1.2.3