From 875462381bc57b13a8ca6b68a52ad1ca065ef95e Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 7 Jun 2018 16:52:02 +0200 Subject: Document form: add related fields (refs #4107) --- ishtar_common/forms.py | 47 +++++++++++++++++-- ishtar_common/forms_common.py | 53 +++++++++++++++++++--- ishtar_common/models.py | 5 ++ .../templates/blocks/bs_form_snippet.html | 15 +++++- ishtar_common/templates/ishtar/forms/document.html | 38 ++++++++++++++++ ishtar_common/templatetags/from_dict.py | 5 +- ishtar_common/utils.py | 21 ++++++++- ishtar_common/views.py | 2 +- ishtar_common/views_item.py | 19 ++++++++ 9 files changed, 191 insertions(+), 14 deletions(-) create mode 100644 ishtar_common/templates/ishtar/forms/document.html (limited to 'ishtar_common') diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index 59f3e141a..b0f1920df 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -33,6 +33,7 @@ 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.text import slugify from django.utils.translation import ugettext_lazy as _ from bootstrap_datepicker.widgets import DatePicker, DATE_FORMAT, DateField @@ -356,14 +357,46 @@ class FieldType(object): class FormHeader(object): - def __init__(self, label, level=4): + def __init__(self, label, level=4, collapse=False): self.label = label + self.collapse = collapse + if collapse: + level = 5 self.level = level def render(self): - return mark_safe(u"{label}".format( - label=self.label, level=self.level - )) + if not self.collapse: + return mark_safe(u"{label}".format( + label=self.label, level=self.level + )) + html = u"""
+
+
+ + + +
+ +
+
+""".format(label=self.label, slug=slugify(self.label), level=self.level) + return mark_safe(html) + + def render_end(self): + if not self.collapse: + return "" + return mark_safe(u""" +
+
+
+
""") class IshtarForm(forms.Form): @@ -404,6 +437,12 @@ class IshtarForm(forms.Form): self.fields[field.key].choices = field.get_choices() self.fields[field.key].help_text = field.get_help() + def headers(self, key): + if key not in self.HEADERS: + return + self.current_header = self.HEADERS[key] + return self.current_header + class TableSelect(IshtarForm): def __init__(self, *args, **kwargs): diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index 9abc6551a..0cfc2a609 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -41,7 +41,7 @@ import widgets from bootstrap_datepicker.widgets import DatePicker from ishtar_common.templatetags.link_to_window import link_to_window from forms import FinalForm, FormSet, reverse_lazy, name_validator, \ - TableSelect, ManageOldType, CustomForm, FieldType, \ + TableSelect, ManageOldType, CustomForm, FieldType, FormHeader, \ FormSetWithDeleteSwitches, IshtarForm, get_data_from_formset from ishtar_common.utils import is_downloadable, clean_session_cache @@ -1079,7 +1079,7 @@ class DocumentForm(forms.ModelForm, CustomForm, ManageOldType): label=_(u"Image"), help_text=mark_safe(get_image_help()), max_length=255, required=False, widget=widgets.ImageFileInput()) associated_file = forms.FileField( - label=pgettext(u"File", u"Not directory"), max_length=255, + label=pgettext(u"Not directory", u"File"), max_length=255, required=False) reference = forms.CharField( label=_(u"Reference"), @@ -1111,12 +1111,31 @@ class DocumentForm(forms.ModelForm, CustomForm, ManageOldType): class Meta: model = models.Document fields = [ - 'title', 'source_type', 'authors', 'associated_url', 'image', - 'associated_file', 'reference', 'internal_reference', - 'receipt_date', 'creation_date', 'receipt_date_in_documentation', + 'title', 'source_type', 'reference', 'internal_reference', + 'image', 'associated_file', 'associated_url', + 'authors', 'receipt_date', + 'receipt_date_in_documentation', 'creation_date', 'comment', 'description', 'additional_information', 'duplicate' ] + HEADERS = { + 'title': FormHeader(_(u"Identification")), + 'image': FormHeader(_(u"Content")), + 'authors': FormHeader(_(u"Authors")), + 'receipt_date': FormHeader(_(u"Dates")), + 'comment': FormHeader(_(u"Advanced"), collapse=True), + 'finds': FormHeader(_(u"Related items")), + } + + def __init__(self, *args, **kwargs): + super(DocumentForm, self).__init__(*args, **kwargs) + for related_key in models.Document.RELATED_MODELS_ALT: + model = models.Document._meta.get_field(related_key).related_model + self.fields[related_key] = widgets.Select2MultipleField( + model=model, remote=True, label=model._meta.verbose_name_plural, + required=False, long_widget=True + ) + def clean(self): cleaned_data = self.cleaned_data if not cleaned_data.get('title', None) and \ @@ -1126,7 +1145,29 @@ class DocumentForm(forms.ModelForm, CustomForm, ManageOldType): raise forms.ValidationError(_(u"You should at least fill one of " u"this field: title, url, image or " u"file.")) - return cleaned_data + for rel in models.Document.RELATED_MODELS: + if cleaned_data.get(rel, None): + return cleaned_data + raise forms.ValidationError(_(u"A document have to attached at least " + u"to one item")) + + def save(self, commit=True): + item = super(DocumentForm, self).save(commit=commit) + for related_key in models.Document.RELATED_MODELS: + related = getattr(item, related_key) + initial = dict([(item.pk, item) + for item in related.all()]) + new = [int(pk) + for pk in sorted(self.cleaned_data.get(related_key, []))] + for pk in initial.keys(): + if pk in new: + continue + related.remove(initial[pk]) + for new_pk in new: + if new_pk in initial.keys(): + continue + related.add(related.model.objects.get(pk=new_pk)) + return item class DocumentSelect(TableSelect): diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 758596279..3715c0326 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -3060,6 +3060,11 @@ class Document(OwnPerms, ImageModel, FullSearch): 'treatment_files', 'treatments', 'finds', 'context_records', 'operations', 'sites', 'warehouses', ] + # same fields but in order for forms + RELATED_MODELS_ALT = [ + 'finds', 'context_records', 'operations', 'sites', 'warehouses', + 'treatments', 'treatment_files', + ] SLUG = 'document' LINK_SPLIT = u"<||>" diff --git a/ishtar_common/templates/blocks/bs_form_snippet.html b/ishtar_common/templates/blocks/bs_form_snippet.html index 47779cdc1..74dbadf5c 100644 --- a/ishtar_common/templates/blocks/bs_form_snippet.html +++ b/ishtar_common/templates/blocks/bs_form_snippet.html @@ -63,13 +63,26 @@ {% if field.name in form.HEADERS %} {% if forloop.counter0 %} {% endif %} -

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

+ +{% if form.current_header %} + {{form.current_header|call:'render_end'}} +{% endif %} + +{% with "headers,"|add:field.name as call_arg %} +{{form|call:call_arg|call:"render"}} +{% endwith %} +
{% elif not search and not forloop.counter0 or search and forloop.counter0 == 1 %}
{% endif %} {% include "blocks/bs_field_snippet.html" %} {% if forloop.last %} + +{% if form.current_header %} + {{form.current_header|call:'render_end'}} +{% endif %} + {% if search and forloop.counter0 >= 1 %}