From 23d8777cf97c37f20438a31b7e155835474044a4 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Thu, 7 Dec 2023 10:33:23 +0100 Subject: [PATCH 1/2] Tender admin: add form validation on specific distance location rule --- lemarche/tenders/admin.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lemarche/tenders/admin.py b/lemarche/tenders/admin.py index 6e43cb37f..d953c6f8a 100644 --- a/lemarche/tenders/admin.py +++ b/lemarche/tenders/admin.py @@ -2,6 +2,7 @@ from django import forms from django.contrib import admin from django.contrib.contenttypes.admin import GenericTabularInline +from django.core.exceptions import ValidationError from django.db import models from django.http import HttpResponseRedirect from django.urls import reverse @@ -12,6 +13,7 @@ from lemarche.notes.models import Note from lemarche.perimeters.admin import PerimeterRegionFilter +from lemarche.perimeters.models import Perimeter from lemarche.tenders import constants as tender_constants from lemarche.tenders.forms import TenderAdminForm from lemarche.tenders.models import PartnerShareTender, Tender, TenderQuestion, TenderStepsData @@ -88,6 +90,26 @@ class TenderQuestionInline(admin.TabularInline): extra = 0 +class TenderForm(forms.ModelForm): + class Meta: + model = Tender + fields = "__all__" + + def clean(self): + """ + Add validation on form rules: + - if distance_location is set, then location must be filled + a city + """ + cleaned_data = super().clean() + distance_location = cleaned_data.get("distance_location") + if distance_location: + location = cleaned_data.get("location") + if not location: + raise ValidationError({"location": "Distance en km est spécifié, ce champ doit donc être rempli"}) + if location.kind != Perimeter.KIND_CITY: + raise ValidationError({"location": "Distance en km est spécifié, ce champ doit être une ville"}) + + @admin.register(Tender, site=admin_site) class TenderAdmin(FieldsetsInlineMixin, admin.ModelAdmin): list_display = [ @@ -135,6 +157,7 @@ class TenderAdmin(FieldsetsInlineMixin, admin.ModelAdmin): search_help_text = "Cherche sur les champs : ID, Titre, Slug, Auteur (ID, E-mail)" ordering = ["-created_at"] + form = TenderForm autocomplete_fields = ["sectors", "location", "perimeters", "author"] readonly_fields = [field for field in Tender.READONLY_FIELDS] + [ # slug From af1848fb95f11d9c95e94683300d3633ffa69a77 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Thu, 7 Dec 2023 13:46:38 +0100 Subject: [PATCH 2/2] Improve wording --- lemarche/tenders/admin.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lemarche/tenders/admin.py b/lemarche/tenders/admin.py index d953c6f8a..435f5de24 100644 --- a/lemarche/tenders/admin.py +++ b/lemarche/tenders/admin.py @@ -105,9 +105,13 @@ def clean(self): if distance_location: location = cleaned_data.get("location") if not location: - raise ValidationError({"location": "Distance en km est spécifié, ce champ doit donc être rempli"}) + raise ValidationError( + {"location": "Le champ 'Distance en km' est spécifié, ce champ doit donc être rempli"} + ) if location.kind != Perimeter.KIND_CITY: - raise ValidationError({"location": "Distance en km est spécifié, ce champ doit être une ville"}) + raise ValidationError( + {"location": "Le champ 'Distance en km' est spécifié, ce champ doit être une ville"} + ) @admin.register(Tender, site=admin_site)