|
1 | 1 | from typing import TYPE_CHECKING, Any |
2 | 2 |
|
3 | 3 | from django import forms |
| 4 | +from django.db.models import TextChoices |
| 5 | +from django.utils.translation import gettext_lazy as _ |
4 | 6 |
|
5 | 7 | from country_workspace.workspaces.admin.cleaners.base import BaseActionForm |
6 | 8 | from country_workspace.workspaces.validators import ValidatableFileValidator |
@@ -34,41 +36,54 @@ class BulkUpdateImportForm(forms.Form): |
34 | 36 | ) |
35 | 37 |
|
36 | 38 |
|
37 | | -class ImportFileForm(forms.Form): |
| 39 | +class ValidateMode(TextChoices): |
| 40 | + NONE = "none", _("Skip validation — import data as is") |
| 41 | + CHECK_BEFORE = "check_before", _("Prevent import if data is not valid against data checker.") |
| 42 | + CHECK_AND_FAIL_IF_ALIEN = ( |
| 43 | + "check_and_fail_if_alien", |
| 44 | + _("Prevent import if data is invalid AND fail if an alien field is found."), |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +class BaseImportForm(forms.Form): |
38 | 49 | batch_name = forms.CharField(required=False, help_text="Label for this batch") |
| 50 | + validate_mode = forms.TypedChoiceField( |
| 51 | + choices=ValidateMode.choices, |
| 52 | + coerce=ValidateMode, |
| 53 | + empty_value=ValidateMode.CHECK_AND_FAIL_IF_ALIEN, |
| 54 | + initial=ValidateMode.CHECK_AND_FAIL_IF_ALIEN, |
| 55 | + required=True, |
| 56 | + help_text=_("How to validate data before import"), |
| 57 | + ) |
| 58 | + |
39 | 59 |
|
| 60 | +class ImportFileForm(BaseImportForm): |
40 | 61 | pk_column_name = forms.CharField( |
41 | 62 | required=True, |
42 | 63 | initial="household_id", |
43 | | - help_text="Which column contains the unique identifier of the record.It is mandatory from Master/detail", |
| 64 | + help_text=_("Which column contains the unique identifier of the record. It is mandatory from Master/detail"), |
44 | 65 | ) |
45 | 66 |
|
46 | 67 | master_column_label = forms.CharField( |
47 | 68 | required=False, |
48 | 69 | initial="household_id", |
49 | | - help_text="Which column contains the 'link' to the household record.", |
| 70 | + help_text=_("Which column contains the 'link' to the household record."), |
50 | 71 | ) |
51 | 72 |
|
52 | 73 | detail_column_label = forms.CharField( |
53 | 74 | required=False, |
54 | 75 | initial="household_id", |
55 | | - help_text="Which column should be used as label for the household. It can use interpolation", |
| 76 | + help_text=_("Which column should be used as label for the household. It can use interpolation"), |
56 | 77 | ) |
57 | 78 |
|
58 | 79 | people_column_prefix = forms.CharField( |
59 | 80 | required=False, |
60 | 81 | initial="pp_", |
61 | | - help_text="People' column group prefix", |
| 82 | + help_text=_("People' column group prefix"), |
62 | 83 | ) |
63 | 84 |
|
64 | 85 | first_line = forms.IntegerField(required=True, initial=0, help_text="First line to process") |
65 | 86 |
|
66 | | - check_before = forms.BooleanField( |
67 | | - required=False, help_text="Prevent import if errors if data is not valid against data checker." |
68 | | - ) |
69 | | - fail_if_alien = forms.BooleanField( |
70 | | - required=False, help_text="Fails if it finds fields which do not exists in data checker." |
71 | | - ) |
72 | 87 | file = forms.FileField(validators=[ValidatableFileValidator()]) |
73 | 88 |
|
74 | 89 | def __init__(self, *args: Any, beneficiary_group: BeneficiaryGroup | None = None, **kwargs: Any) -> None: |
|
0 commit comments