Skip to content

Commit

Permalink
Rework ads form, and add ADS.ads_in_use
Browse files Browse the repository at this point in the history
  • Loading branch information
brmzkw committed Feb 23, 2024
1 parent 96d4022 commit a3638be
Show file tree
Hide file tree
Showing 13 changed files with 507 additions and 185 deletions.
12 changes: 9 additions & 3 deletions mesads/api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,15 @@ def test_get(self):
client.force_authenticate(user=self.admin_user)

# Create 3 ADS in departement 35
ADS.objects.create(number="1", ads_manager=self.ads_manager_city35)
ADS.objects.create(number="2", ads_manager=self.ads_manager_city35)
ADS.objects.create(number="3", ads_manager=self.ads_manager_city35)
ADS.objects.create(
number="1", ads_manager=self.ads_manager_city35, ads_in_use=True
)
ADS.objects.create(
number="2", ads_manager=self.ads_manager_city35, ads_in_use=True
)
ADS.objects.create(
number="3", ads_manager=self.ads_manager_city35, ads_in_use=True
)

resp = client.get("/api/stats/geojson/per-prefecture/")
self.assertEqual(resp.status_code, 200)
Expand Down
38 changes: 38 additions & 0 deletions mesads/app/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from django.core.exceptions import ValidationError
from django.forms.fields import BooleanField


class NullBooleanField(BooleanField):
"""NullBooleanField allows the following:
>>> from mesads.app.widgets import BooleanSelect
>>> class MyModel(models.Model):
... myfield = models.BooleanField(blank=False, null=False)
>>> class MyForm(forms.ModelForm):
... class Meta:
... model = MyModel
... fields = ['myfield']
... myfield = NullBooleanField(widget=BooleanSelect)
If BooleanField was used instead of NullBooleanField, and the the HTML form
submits an empty value (because the first default option submitted is
`<option disabled selected value>Select an option</option>`), the form would
return an error because the field is required (because `blank=False`), but
also when the "false" option is selected.
NullBooleanField makes the distinction between the "false" option and the
empty value.
"""

def to_python(self, value):
"""Override the behavior of the base class which converts None to False."""
if value is None:
return None
return super().to_python(value)

def validate(self, value):
"""Override the behavior of the base class which triggers an error when value is False."""
if value is None and self.required:
raise ValidationError(self.error_messages["required"], code="required")
9 changes: 9 additions & 0 deletions mesads/app/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from mesads.fradm.forms import FrenchAdministrationForm
from mesads.fradm.models import Commune, EPCI, Prefecture

from .fields import NullBooleanField
from .models import (
ADS,
ADSLegalFile,
Expand Down Expand Up @@ -89,6 +90,7 @@ class Meta:
"epci_commune",
"number",
"ads_creation_date",
"ads_in_use",
"ads_renew_date",
"attribution_date",
"attribution_type",
Expand Down Expand Up @@ -132,6 +134,13 @@ def __init__(self, epci=None, *args, **kwargs):
required=False,
)

ads_in_use = NullBooleanField(
widget=BooleanSelect(),
label=ADS.ads_in_use.field.verbose_name,
help_text=ADS.ads_in_use.field.help_text,
required=True,
)


class AutoDeleteADSUserFormSet(BaseInlineFormSet):
"""By default, to remove an entry from a formset, you need to render a
Expand Down
20 changes: 20 additions & 0 deletions mesads/app/migrations/0070_ads_ads_in_use.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.1.9 on 2024-02-15 15:45

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("app", "0069_adsmanageradministrator_referent_emails"),
]

operations = [
migrations.AddField(
model_name="ads",
name="ads_in_use",
field=models.BooleanField(
default=True, verbose_name="L'ADS est-elle actuellement exploitée ?"
),
preserve_default=False,
),
]
13 changes: 11 additions & 2 deletions mesads/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,18 @@ def unique_error_message(self, model_class, unique_check):
ads_creation_date = models.DateField(
blank=True,
null=True,
verbose_name="Date de création de l'ADS",
help_text="Indiquer la date à laquelle l’ADS a été attribuée à un titulaire pour la première fois.",
verbose_name="Date de création initiale de l'ADS",
help_text=mark_safe(
"Indiquer la date à laquelle l'ADS a été attribuée à un titulaire pour la première fois."
"<br />"
"Pour les « anciennes ADS » <strong>entrez la date de création initiale de l'ADS</strong>, et pas la date éventuelle de cession à un nouveau titulaire."
),
)

ads_in_use = models.BooleanField(
blank=False, null=False, verbose_name="L'ADS est-elle actuellement exploitée ?"
)

ads_renew_date = models.DateField(
blank=True,
null=True,
Expand Down
10 changes: 6 additions & 4 deletions mesads/app/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_validate_no_ads_declared(self):
self.assertIsNone(validate_no_ads_declared(self.ads_manager_city35, True))
self.assertIsNone(validate_no_ads_declared(self.ads_manager_city35, False))

ADS(number="12345", ads_manager=self.ads_manager_city35).save()
ADS(number="12345", ads_manager=self.ads_manager_city35, ads_in_use=True).save()

self.assertIsNone(validate_no_ads_declared(self.ads_manager_city35, False))
self.assertRaises(
Expand Down Expand Up @@ -156,7 +156,7 @@ class TestADS(ClientTestCase):
def setUp(self):
super().setUp()
self.ads = ADS.objects.create(
number="12346", ads_manager=self.ads_manager_city35
number="12346", ads_manager=self.ads_manager_city35, ads_in_use=True
)

def test_str(self):
Expand All @@ -177,7 +177,7 @@ class TestADSLegalFile(ClientTestCase):
def setUp(self):
super().setUp()
self.ads = ADS.objects.create(
number="12346", ads_manager=self.ads_manager_city35
number="12346", ads_manager=self.ads_manager_city35, ads_in_use=True
)

def test_exists_in_storage(self):
Expand All @@ -198,7 +198,9 @@ class TestADSUser(ClientTestCase):
def setUp(self):
super().setUp()
self.ads = ADS.objects.create(
number="12346", ads_manager=self.ads_manager_city35
number="12346",
ads_manager=self.ads_manager_city35,
ads_in_use=True,
)

def test_str(self):
Expand Down
8 changes: 6 additions & 2 deletions mesads/app/test_reversion_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ def setUp(self):
content_type=ContentType.objects.get_for_model(self.commune),
object_id=self.commune.id,
)
self.ads = ADS.objects.create(number=1, ads_manager=self.ads_manager)
self.ads2 = ADS.objects.create(number=2, ads_manager=self.ads_manager)
self.ads = ADS.objects.create(
number=1, ads_manager=self.ads_manager, ads_in_use=True
)
self.ads2 = ADS.objects.create(
number=2, ads_manager=self.ads_manager, ads_in_use=True
)

def test_no_revision(self):
revisions = ModelHistory(self.ads).get_revisions()
Expand Down
Loading

0 comments on commit a3638be

Please sign in to comment.