summaryrefslogtreecommitdiff
path: root/ishtar_common/forms_common.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2020-06-10 18:47:17 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2021-02-28 12:15:21 +0100
commit8b7c617c7ac7f752646bd48f39da97dcfbaaaab2 (patch)
treec0a931af3e3be561d3779811e70eeaf2c995f332 /ishtar_common/forms_common.py
parent6d030c2e804b5c8f73a6e6aa9510ea23afaeeaa3 (diff)
downloadIshtar-8b7c617c7ac7f752646bd48f39da97dcfbaaaab2.tar.bz2
Ishtar-8b7c617c7ac7f752646bd48f39da97dcfbaaaab2.zip
Documents: better form - improve select2 widget to manage new
Diffstat (limited to 'ishtar_common/forms_common.py')
-rw-r--r--ishtar_common/forms_common.py73
1 files changed, 58 insertions, 15 deletions
diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py
index 20cf0b613..f389ab84d 100644
--- a/ishtar_common/forms_common.py
+++ b/ishtar_common/forms_common.py
@@ -36,6 +36,7 @@ from django.core.files import File
from django.forms.formsets import formset_factory
from django.forms.models import BaseModelFormSet, BaseFormSet
from django.shortcuts import reverse
+from django.utils.text import slugify
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _, pgettext
@@ -1229,12 +1230,38 @@ def get_image_help():
#######################
+class AddGenericForm(ManageOldType, NewItemForm):
+ model = None # to be defined
+ label = forms.CharField(label=_("Label"), max_length=200)
+
+ def clean_label(self):
+ value = self.cleaned_data.get('label', None).strip()
+ if self.model.objects.filter(label=value).count():
+ raise forms.ValidationError(_("This value already exist"))
+ return value
+
+ def save(self, user):
+ label = self.cleaned_data['label']
+ base_slug = slugify(label)
+ slug = base_slug
+ idx = 0
+ while self.model.objects.filter(txt_idx=slug).count():
+ idx += 1
+ slug = base_slug + "-" + str(idx)
+ return self.model.objects.create(label=label, txt_idx=slug)
+
+
+class AddDocumentTagForm(AddGenericForm):
+ model = models.DocumentTag
+ form_label = _("Document tag")
+
+
class DocumentForm(forms.ModelForm, CustomForm, ManageOldType):
form_label = _("Documentation")
form_admin_name = _("Document - General")
form_slug = "document-general"
file_upload = True
- extra_form_modals = ["author", "person", "organization"]
+ extra_form_modals = ["author", "person", "organization", "documenttag"]
associated_models = {'source_type': models.SourceType,
'support_type': models.SupportType,
'publisher': models.Organization,
@@ -1259,9 +1286,9 @@ class DocumentForm(forms.ModelForm, CustomForm, ManageOldType):
container_ref = widgets.ModelJQueryAutocompleteField(
label=_("Reference container"),
model=Container, required=False)
- authors = widgets.ModelJQueryAutocompleteField(
- model=models.Author, multiple=True, label=_("Authors"), new=True,
- long_widget=True, required=False)
+ authors = widgets.Select2MultipleField(
+ label=_("Authors"), required=False, model=models.Author,
+ remote="autocomplete-author")
publisher = forms.IntegerField(
label=_("Publisher"),
widget=widgets.JQueryAutoComplete(
@@ -1270,15 +1297,16 @@ class DocumentForm(forms.ModelForm, CustomForm, ManageOldType):
args=[models.organization_type_pks_lazy(
settings.ISHTAR_SLUGS["document-editor"])]),
limit={
- 'organization_type': models.organization_type_pks_lazy(
- ['responsible_planning_service'])
- },
+ 'organization_type': [models.organization_type_pks_lazy(
+ settings.ISHTAR_SLUGS["document-editor"])]},
tips=models.get_publisher_label,
- associated_model=models.Organization, new=True),
+ associated_model=models.Organization),
validators=[models.valid_id(models.Organization)], required=False)
- license = widgets.Select2MultipleField(
- label=_("Licences"), required=False)
- tag = widgets.Select2MultipleField(label=_("Tags"), required=False)
+ licenses = widgets.Select2MultipleField(
+ label=_("Licences"), required=False, model=models.LicenseType)
+ tags = widgets.Select2MultipleField(
+ label=_("Tags"), required=False, model=models.DocumentTag,
+ remote="autocomplete-documenttag")
language = widgets.ModelChoiceField(
model=models.Language, label=_("Language"), choices=[],
required=False)
@@ -1335,8 +1363,8 @@ class DocumentForm(forms.ModelForm, CustomForm, ManageOldType):
FieldType('support_type', models.SupportType),
FieldType('format_type', models.Format),
FieldType('language', models.Language),
- FieldType('licence', models.LicenseType, is_multiple=True),
- FieldType('tag', models.DocumentTag, is_multiple=True),
+ FieldType('licences', models.LicenseType, is_multiple=True),
+ FieldType('tags', models.DocumentTag, is_multiple=True),
]
class Meta:
@@ -1344,10 +1372,10 @@ class DocumentForm(forms.ModelForm, CustomForm, ManageOldType):
fields = [
'title', 'source_type', 'reference', 'internal_reference',
'format_type', 'support_type', 'scale',
- 'image', 'associated_file', 'associated_url', 'tag',
+ 'image', 'associated_file', 'associated_url', 'tags',
'authors', 'receipt_date',
'receipt_date_in_documentation', 'creation_date',
- 'publisher', 'language', 'isbn', 'issn', 'license',
+ 'publisher', 'language', 'isbn', 'issn', 'licenses',
'container', "container_ref",
'source', 'source_free_input',
'comment', 'description', 'additional_information', 'duplicate'
@@ -1364,6 +1392,12 @@ class DocumentForm(forms.ModelForm, CustomForm, ManageOldType):
'comment': FormHeader(_("Advanced"), collapse=True),
'finds': FormHeader(_("Related items")),
}
+ OPTIONS_PERMISSIONS = [
+ # field name, permission, options
+ ("tags", ("ishtar_common.add_documenttag",), {"new": True}),
+ ("authors", ("ishtar_common.add_author",), {"new": True}),
+ ("publisher", ("ishtar_common.add_organization",), {"new": True}),
+ ]
def __init__(self, *args, **kwargs):
main_items_fields = {}
@@ -1403,6 +1437,15 @@ class DocumentForm(forms.ModelForm, CustomForm, ManageOldType):
raise forms.ValidationError(_("A document has to be attached at least "
"to one item"))
+ def clean_publisher(self):
+ if not self.cleaned_data.get("publisher", None):
+ return
+ try:
+ return models.Organization.objects.get(
+ pk=self.cleaned_data["publisher"])
+ except models.Organization.DoesNotExist:
+ return
+
def save(self, commit=True):
if not self.cleaned_data.get('authors', None):
self.cleaned_data['authors'] = []