|
12 | 12 | from geotrek.common.forms import CommonForm |
13 | 13 | from geotrek.core.widgets import PointTopologyWidget |
14 | 14 | from geotrek.infrastructure.forms import BaseInfrastructureForm |
15 | | -from geotrek.signage.models import Blade, Line, LinePictogram, Signage |
| 15 | +from geotrek.signage.models import Blade, BladeCondition, Line, LinePictogram, Signage |
| 16 | + |
| 17 | + |
| 18 | +class CleanDuplicatedNumbersMixin: |
| 19 | + duplication_error_message = None |
| 20 | + |
| 21 | + def clean(self): |
| 22 | + """Checks that no objects have the same number.""" |
| 23 | + if any(self.errors): |
| 24 | + # Don't bother validating the formset unless each form is valid on its own |
| 25 | + return |
| 26 | + |
| 27 | + numbers = {} |
| 28 | + |
| 29 | + for form in self.forms: |
| 30 | + if self.can_delete and self._should_delete_form(form): |
| 31 | + continue |
| 32 | + number = form.cleaned_data.get("number") |
| 33 | + numbers.setdefault(number, []).append(form) |
| 34 | + |
| 35 | + duplicates = {n: forms for n, forms in numbers.items() if len(forms) > 1} |
| 36 | + |
| 37 | + if duplicates: |
| 38 | + for forms in duplicates.values(): |
| 39 | + for form in forms: |
| 40 | + form.add_error("number", self.duplication_error_message) |
| 41 | + raise ValidationError(self.duplication_error_message) |
16 | 42 |
|
17 | 43 |
|
18 | 44 | class LineForm(forms.ModelForm): |
@@ -54,29 +80,8 @@ class Meta: |
54 | 80 | ) |
55 | 81 |
|
56 | 82 |
|
57 | | -class BaseLineFormSet(BaseInlineFormSet): |
58 | | - def clean(self): |
59 | | - """Checks that no two lines have the same number.""" |
60 | | - if any(self.errors): |
61 | | - # Don't bother validating the formset unless each form is valid on its own |
62 | | - return |
63 | | - |
64 | | - msg = _("This order number is already used by another line.") |
65 | | - numbers = {} |
66 | | - |
67 | | - for form in self.forms: |
68 | | - if self.can_delete and self._should_delete_form(form): |
69 | | - continue |
70 | | - number = form.cleaned_data.get("number") |
71 | | - numbers.setdefault(number, []).append(form) |
72 | | - |
73 | | - duplicates = {n: forms for n, forms in numbers.items() if len(forms) > 1} |
74 | | - |
75 | | - if duplicates: |
76 | | - for forms in duplicates.values(): |
77 | | - for form in forms: |
78 | | - form.add_error("number", msg) |
79 | | - raise ValidationError(msg) |
| 83 | +class BaseLineFormSet(CleanDuplicatedNumbersMixin, BaseInlineFormSet): |
| 84 | + duplication_error_message = _("This order number is already used by another line.") |
80 | 85 |
|
81 | 86 |
|
82 | 87 | LineFormset = inlineformset_factory( |
@@ -172,6 +177,35 @@ class Meta: |
172 | 177 | fields = ["id", "number", "direction", "type", "conditions", "color"] |
173 | 178 |
|
174 | 179 |
|
| 180 | +class BladeFormsetForm(forms.ModelForm): |
| 181 | + def __init__(self, *args, **kwargs): |
| 182 | + super().__init__(*args, **kwargs) |
| 183 | + |
| 184 | + fields_for_layout = Div( |
| 185 | + "id", "number", "direction", "type", "conditions", "color" |
| 186 | + ) |
| 187 | + |
| 188 | + self.helper = FormHelper() |
| 189 | + self.helper.form_tag = False |
| 190 | + self.helper.layout = Layout(*fields_for_layout) |
| 191 | + self.fields["conditions"].widget = autocomplete.Select2Multiple( |
| 192 | + attrs={"data-theme": "bootstrap4"}, |
| 193 | + ) |
| 194 | + self.fields["conditions"].queryset = BladeCondition.objects.all() |
| 195 | + |
| 196 | + class Meta: |
| 197 | + fields = ["id", "number", "direction", "type", "conditions", "color"] |
| 198 | + |
| 199 | + |
| 200 | +class BaseBladeFormSet(CleanDuplicatedNumbersMixin, BaseInlineFormSet): |
| 201 | + duplication_error_message = _("This order number is already used by another blade.") |
| 202 | + |
| 203 | + |
| 204 | +BladeFormset = inlineformset_factory( |
| 205 | + Signage, Blade, form=BladeFormsetForm, formset=BaseBladeFormSet, extra=1 |
| 206 | +) |
| 207 | + |
| 208 | + |
175 | 209 | if settings.TREKKING_TOPOLOGY_ENABLED: |
176 | 210 |
|
177 | 211 | class BaseSignageForm(BaseInfrastructureForm): |
@@ -207,6 +241,7 @@ class SignageForm(BaseSignageForm): |
207 | 241 | "manager", |
208 | 242 | "sealing", |
209 | 243 | "access", |
| 244 | + Fieldset(_("Blades")), |
210 | 245 | ) |
211 | 246 | ] |
212 | 247 |
|
|
0 commit comments