diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-02-28 17:20:57 +0100 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-02-28 17:20:57 +0100 | 
| commit | c2d60a414e788d3802c01883681480a4cfe7308c (patch) | |
| tree | 07f7aa82cd02f01e172aee1b8d95dfe4d01edeb9 | |
| parent | 763daa92f261ee5a32afe7b1db611f9e9717106f (diff) | |
| download | Ishtar-c2d60a414e788d3802c01883681480a4cfe7308c.tar.bz2 Ishtar-c2d60a414e788d3802c01883681480a4cfe7308c.zip | |
Forms: manage headers definition in code
| -rw-r--r-- | archaeological_finds/forms.py | 2 | ||||
| -rw-r--r-- | ishtar_common/forms.py | 13 | ||||
| -rw-r--r-- | ishtar_common/templates/blocks/bs_form_snippet.html | 9 | ||||
| -rw-r--r-- | 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"<h{level}>{label}</h{level}>".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 %}  <div class="alert alert-danger" role="alert">      {{form.non_field_errors}} @@ -66,8 +66,13 @@              </div>              <div class="modal-body form-row">  {% endif %} +{% if field.name in form.HEADERS %} +{% if forloop.counter0 %} +</div>{% endif %} +<h3>{{field.name|from_dict:form.HEADERS|call:'render'}}</h3> +<div class="form-row"> -{% 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 %}    </div>{% endif %}    <div class="form-row"> 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)() | 
