diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-09-17 17:07:04 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-09-22 16:45:03 +0200 |
commit | c981656169ed68154798cbd10492be674ba46ed6 (patch) | |
tree | d2182da3c7b978ebbfd57fe6334eca74e15bc74a | |
parent | fa46a3e8b9006892f8b7fa559c61d41b91c430dc (diff) | |
download | Ishtar-c981656169ed68154798cbd10492be674ba46ed6.tar.bz2 Ishtar-c981656169ed68154798cbd10492be674ba46ed6.zip |
✨ document type field - use select2 widget
-rw-r--r-- | archaeological_operations/forms.py | 6 | ||||
-rw-r--r-- | ishtar_common/forms.py | 7 | ||||
-rw-r--r-- | ishtar_common/forms_common.py | 21 | ||||
-rw-r--r-- | ishtar_common/static/js/ishtar.js | 6 | ||||
-rw-r--r-- | ishtar_common/widgets.py | 18 | ||||
-rw-r--r-- | scss/custom.scss | 4 |
6 files changed, 50 insertions, 12 deletions
diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index 94251c39b..ca84d697e 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -1357,8 +1357,10 @@ class SiteForm(CustomForm, ManageOldType): label=_("Cultural attributions"), choices=[], widget=widgets.Select2Multiple, required=False) - discoverer = widgets.Select2SimpleField( - model=Person, label=_("Discoverer"), required=False, remote=True) + discoverer = forms.IntegerField( + widget=widgets.JQueryAutoComplete( + reverse_lazy('autocomplete-person-permissive'), associated_model=Person), + label=_("Discoverer"), required=False) HEADERS['town'] = FormHeader(_("Localization")) town = widgets.Select2MultipleField( model=Town, label=_("Towns"), required=False, remote=True diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index 1804e9c7b..f16a3eeb9 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -684,7 +684,7 @@ class FieldType: """ def __init__(self, key, model, is_multiple=False, extra_args=None, - empty_first=True): + empty_first=True, help_text=True): self.key = key self.model = model self.is_multiple = is_multiple @@ -692,6 +692,7 @@ class FieldType: if self.is_multiple: empty_first = False self.empty_first = empty_first + self.help_text = help_text def get_choices(self, initial=None): args = {"empty_first": self.empty_first, "initial": initial} @@ -700,6 +701,8 @@ class FieldType: return self.model.get_types(**args) def get_help(self): + if not self.help_text: + return args = {} if self.extra_args: args.update(self.extra_args) @@ -837,6 +840,8 @@ class IshtarForm(BSForm, forms.Form): if field.key not in self.fields: return self.fields[field.key].choices = field.get_choices() + if not getattr(field, "help_text", True): + return self.fields[field.key].help_text = field.get_help() def _clean_model_field(self, key, model): diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index 9066e2390..f9b79bf1b 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -2065,8 +2065,10 @@ class DocumentForm(forms.ModelForm, CustomForm, ManageOldType): required=False, validators=[validators.MaxLengthValidator(200)], ) - source_type = widgets.ModelChoiceField( - model=models.SourceType, label=_("Type"), choices=[], required=False + source_type = widgets.Select2SimpleField( + label=_("Type"), + model=models.SourceType, + required=False, ) support_type = widgets.ModelChoiceField( model=models.SupportType, label=_("Medium"), choices=[], required=False @@ -2424,6 +2426,12 @@ class DocumentForm(forms.ModelForm, CustomForm, ManageOldType): ) return cleaned_data + def clean_source_type(self): + value = self.cleaned_data.get("source_type", None) + if not value: + return + return value + def clean_publisher(self): if not self.cleaned_data.get("publisher", None): return @@ -2496,7 +2504,10 @@ class DocumentSelect(HistorySelect): ) title = forms.CharField(label=_("Title")) - source_type = forms.ChoiceField(label=_("Type"), choices=[]) + source_type = forms.ChoiceField( + label=_("Type"), choices=[], + widget=widgets.Select2Multiple(), + ) reference = forms.CharField(label=_("Reference")) internal_reference = forms.CharField(label=_("Internal reference")) description = forms.CharField(label=_("Description")) @@ -2626,7 +2637,7 @@ class DocumentSelect(HistorySelect): receipt_date_in_documentation = DateField(label=_("Receipt date")) TYPES = [ - FieldType("source_type", models.SourceType), + FieldType("source_type", models.SourceType, help_text=False), FieldType("format", models.Format), FieldType("support", models.SupportType), FieldType("tag", models.DocumentTag), @@ -2701,7 +2712,7 @@ class QADocumentFormMulti(QAForm): "qa_copyright", "qa_shooting_angle", ] - qa_source_type = forms.ChoiceField(label=_("Source type"), required=False) + qa_source_type = forms.ChoiceField(label=_("Type"), required=False) qa_authors = widgets.ModelJQueryAutocompleteField( model=models.Author, label=_("Author"), new=True, required=False ) diff --git a/ishtar_common/static/js/ishtar.js b/ishtar_common/static/js/ishtar.js index bcc2b7940..cf7009ef9 100644 --- a/ishtar_common/static/js/ishtar.js +++ b/ishtar_common/static/js/ishtar.js @@ -850,6 +850,12 @@ function clear_search_field(){ function _clear_search_criteria_fields(query){ var datatables_length = $(".dataTables_length select").val(); document.getElementById('wizard-form').reset(); + // select2 + $("#modal-advanced-search .select2-hidden-accessible").each( + function(){ + $(this).val('false-data').trigger('change'); + } + ); // hidden input are not cleared $("#modal-advanced-search input[type='hidden']").each( function(){ diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py index d0ade6fea..2e6dc36af 100644 --- a/ishtar_common/widgets.py +++ b/ishtar_common/widgets.py @@ -276,8 +276,8 @@ class Select2Base(Select2Media): else: attrs["style"] = "width: 370px" - if value: - if type(value) not in (list, tuple): + if value and getattr(self, "multiple", False) and \ + not isinstance(value, (list, tuple)): value = value.split(",") options = "" @@ -352,7 +352,7 @@ class Select2Simple(Select2Base, forms.Select): class Select2Multiple(Select2Base, forms.SelectMultiple): - pass + multiple = True class CheckboxSelectMultiple(CheckboxSelectMultipleBase): @@ -423,6 +423,8 @@ class Select2BaseField(object): def valid_value(self, value): if not self.model: return super(Select2BaseField, self).valid_value(value) + if isinstance(value, self.model): + return True return bool(self.get_q().filter(pk=value).count()) @@ -439,7 +441,15 @@ class Select2MultipleField(Select2BaseField, forms.MultipleChoiceField): class Select2SimpleField(Select2BaseField, forms.ChoiceField): - pass + multiple = False + + def to_python(self, value): + value = super().to_python(value) + if not value or not getattr(self, "model", None) or getattr(self, "remote", False): + return value + if not self.valid_value(value): + return + return self.get_q().filter(pk=value).all()[0] class DeleteWidget(forms.CheckboxInput): diff --git a/scss/custom.scss b/scss/custom.scss index 5b1aba8f8..88cff1548 100644 --- a/scss/custom.scss +++ b/scss/custom.scss @@ -222,6 +222,10 @@ pre { width: 470px; } +#modal-advanced-search .modal-dialog .select2-selection{ + width: 100%; +} + .modal-dialog #qa-action{ .select2-selection{ width: auto; |