diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2024-02-19 15:58:49 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2024-04-16 16:42:32 +0200 |
commit | 9268bb26e86b9b44ce6520bb1a9120796cfc32d6 (patch) | |
tree | 22a7f9f892477fdf46d2064f7d2daa666bd9808f /ishtar_common | |
parent | f3ad0273186a61f5ca73a7845467f52c732f8d23 (diff) | |
download | Ishtar-9268bb26e86b9b44ce6520bb1a9120796cfc32d6.tar.bz2 Ishtar-9268bb26e86b9b44ce6520bb1a9120796cfc32d6.zip |
✨ Quick edit form for biographical notes
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/forms_common.py | 71 | ||||
-rw-r--r-- | ishtar_common/urls.py | 5 | ||||
-rw-r--r-- | ishtar_common/utils.py | 6 | ||||
-rw-r--r-- | ishtar_common/views.py | 3 |
4 files changed, 79 insertions, 6 deletions
diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index 872571aad..81097ff2f 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -43,7 +43,7 @@ from django.contrib.auth.forms import ( from django.core import validators from django.core.exceptions import ObjectDoesNotExist from django.core.files import File -from django.core.validators import MaxValueValidator, MinValueValidator +from django.core.validators import MinValueValidator from django.forms.formsets import formset_factory from django.forms.models import BaseModelFormSet, BaseFormSet from django.shortcuts import reverse @@ -80,6 +80,7 @@ from ishtar_common.utils import ( is_downloadable, clean_session_cache, max_size_help, + max_value_current_year, reverse_coordinates, ) @@ -1388,6 +1389,70 @@ class PersonTypeForm(ManageOldType, forms.Form): self.fields["person_type"].help_text = models.PersonType.get_help() +class BiographicalNoteForm(ManageOldType, NewItemForm): + extra_form_modals = ["organization", "person"] + form_label = _("Identity") + associated_models = { + "organization": models.Organization, + "person": models.Person, + } + denomination = forms.CharField(label=_("Denomination"), max_length=300) + biography = forms.CharField(label=_("Biography"), max_length=300, required=False, widget=forms.Textarea) + last_name = forms.CharField(label=_("Last name"), max_length=300, required=False) + first_name = forms.CharField(label=_("First name"), max_length=300, required=False) + birth_year = forms.IntegerField( + label=_("Birth year"), + validators=[MinValueValidator(100), max_value_current_year], + required=False, + ) + death_year = forms.IntegerField( + label=_("Death year"), + validators=[MinValueValidator(100), max_value_current_year], + required=False, + ) + person = forms.IntegerField( + label=_("Person"), + widget=widgets.JQueryAutoComplete( + reverse_lazy("autocomplete-person"), + associated_model=models.Person, + new=True, + ), + validators=[models.valid_id(models.Person)], + required=False, + ) + organization = forms.IntegerField( + label=_("Organization"), + widget=widgets.JQueryAutoComplete( + reverse_lazy("autocomplete-organization"), + associated_model=models.Organization, + new=True, + ), + validators=[models.valid_id(models.Organization)], + required=False, + ) + + def save(self, user, item=None): + dct = self.cleaned_data + dct["history_modifier"] = user + for key in self.associated_models.keys(): + if key in dct: + if not dct[key]: + dct.pop(key) + else: + model = self.associated_models[key] + try: + dct[key] = model.objects.get(pk=dct[key]) + except model.DoesNotExist: + dct.pop(key) + if not item: + item = models.BiographicalNote.objects.create(**dct) + else: + for k in dct: + setattr(item, k, dct[k]) + item.save() + return item + + class AccountForm(IshtarForm): form_label = _("Account") associated_models = {"pk": models.Person} @@ -1901,10 +1966,6 @@ class AddDocumentTagForm(AddGenericForm): form_label = _("Document tag") -def max_value_current_year(value): - return MaxValueValidator(datetime.date.today().year)(value) - - class DocumentForm(forms.ModelForm, CustomForm, ManageOldType): form_label = _("Documentation") form_admin_name = _("Document - 010 - General") diff --git a/ishtar_common/urls.py b/ishtar_common/urls.py index 5ce9861d1..955563875 100644 --- a/ishtar_common/urls.py +++ b/ishtar_common/urls.py @@ -410,6 +410,11 @@ urlpatterns += [ name="show-biographicalnote", ), url( + r"new-biographicalnote/(?:(?P<parent_name>[^/]+)/)?(?:(?P<limits>[^/]+)/)?$", + views.new_biographical_note, + name="new-biographicalnote", + ), + url( r"department-by-state/(?P<state_id>.+)?$", views.department_by_state, name="department-by-state", diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 4043e48cf..d41e07e97 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -62,7 +62,7 @@ from django.core.exceptions import SuspiciousOperation, ObjectDoesNotExist, \ ValidationError from django.core.files import File from django.core.files.storage import FileSystemStorage -from django.core.validators import EMPTY_VALUES +from django.core.validators import EMPTY_VALUES, MaxValueValidator from django.db import models from django.db.models import Q from django.http import HttpResponseRedirect @@ -1268,6 +1268,10 @@ def format_int_float(values): return new_values +def max_value_current_year(value): + return MaxValueValidator(datetime.date.today().year)(value) + + def create_slug(model, name, slug_attr="slug", max_length=100): base_slug = slugify(name) slug = base_slug[:max_length] diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 5ad14695f..84d038440 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -1137,6 +1137,9 @@ get_person = get_item(models.Person, "get_person", "person", callback=get_person_gdpr_log) show_biographical_note = show_item(models.BiographicalNote, "biographicalnote") +new_biographical_note = new_qa_item( + models.BiographicalNote, forms.BiographicalNoteForm, page_name=_("New biographical note") +) get_person_for_account = get_item( models.Person, |