From 57e35ce0c26ee36f0345e6adf00ea3f63e7235f0 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 16 Apr 2024 16:38:25 +0200 Subject: ✨ Imports groups - form: new import group MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ishtar_common/forms_common.py | 73 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 15 deletions(-) (limited to 'ishtar_common/forms_common.py') diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index 197b5a03c..1b62dcf49 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -217,12 +217,12 @@ class BaseImportForm(IshtarForm, forms.ModelForm): "name", "importer_type", "imported_file", + "associated_group", "encoding", "csv_sep", + "skip_lines", "imported_images", "imported_images_link", - "associated_group", - "skip_lines", ) widgets = { "imported_file": widgets.BSClearableFileInput, @@ -231,11 +231,28 @@ class BaseImportForm(IshtarForm, forms.ModelForm): HEADERS = { "name": FormHeader(_("Import (table)")), + "encoding": FormHeader(_("CSV options")), } def __init__(self, *args, **kwargs): user = kwargs.pop("user") super(BaseImportForm, self).__init__(*args, **kwargs) + self._filter_group(user) + self._filter_importer_type() + if "imported_images" in self.fields: + self.fields["imported_images"].validators = [file_size_validator] + + self.fields["imported_file"].validators = [file_size_validator] + self._post_init() + + def _filter_importer_type(self): + self.fields["importer_type"].choices = [("", "--")] + [ + (imp.pk, imp.name) + for imp in models.ImporterType.objects.filter(available=True, + type=self.importer_type) + ] + + def _filter_group(self, user): groups = models.TargetKeyGroup.objects.filter(available=True) if not user.is_superuser: groups = groups.filter(all_user_can_use=True) @@ -245,16 +262,6 @@ class BaseImportForm(IshtarForm, forms.ModelForm): self.fields["associated_group"].choices = [(None, "--")] + [ (g.pk, str(g)) for g in groups.all() ] - self.fields["importer_type"].choices = [("", "--")] + [ - (imp.pk, imp.name) - for imp in models.ImporterType.objects.filter(available=True, - type=self.importer_type) - ] - if "imported_images" in self.fields: - self.fields["imported_images"].validators = [file_size_validator] - - self.fields["imported_file"].validators = [file_size_validator] - self._post_init() BAD_CHARS = ["é", "³", "ô", "Ã\xa0", "é"] @@ -312,16 +319,18 @@ class NewImportForm(BaseImportForm): "name", "importer_type", "imported_file", + "associated_group", "encoding", "csv_sep", + "skip_lines", "imported_images", "imported_images_link", - "associated_group", - "skip_lines", ) HEADERS = { "name": FormHeader(_("Import (table)")), + "encoding": FormHeader(_("CSV options")), + "imported_images": FormHeader(_("Documents/Images")), } def clean(self): @@ -389,14 +398,15 @@ class NewImportGISForm(BaseImportForm): "name", "importer_type", "imported_file", + "associated_group", "encoding", "csv_sep", - "associated_group", "skip_lines", ) HEADERS = { "name": FormHeader(_("Import (GIS)")), + "encoding": FormHeader(_("CSV options")), } def __init__(self, *args, **kwargs): @@ -439,6 +449,39 @@ class NewImportGISForm(BaseImportForm): return item +class NewImportGroupForm(NewImportForm): + error_css_class = "error" + required_css_class = "required" + + class Meta: + model = models.ImportGroup + fields = ( + "name", + "importer_type", + "imported_file", + "encoding", + "csv_sep", + "skip_lines", + "imported_images", + "imported_images_link", + ) + + HEADERS = { + "name": FormHeader(_("Import (group)")), + "encoding": FormHeader(_("CSV options")), + "imported_images": FormHeader(_("Documents/Images")), + } + + def _filter_importer_type(self): + self.fields["importer_type"].choices = [("", "--")] + [ + (imp.pk, imp.name) + for imp in models.ImporterGroup.objects.filter(available=True) + ] + + def _filter_group(self, user): + pass + + class TargetKeyForm(forms.ModelForm): class Meta: model = models.TargetKey -- cgit v1.2.3