Skip to content

Commit

Permalink
add unique constraint on brand and name fields
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienReuiller committed Nov 28, 2024
1 parent feacc72 commit 47d5d04
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.contrib.gis.db.models.functions import Distance
from django.contrib.gis.measure import D
from django.contrib.postgres.search import TrigramSimilarity # SearchVector
from django.core.exceptions import ValidationError
from django.db import IntegrityError, models, transaction
from django.db.models import (
BooleanField,
Expand Down Expand Up @@ -1279,6 +1280,18 @@ def set_super_badge(self):

self.save(update_fields=update_fields_list)

def clean(self):
"""
Validate that brand is not used as a brand or name by another Siae
Does not use a unique constraint on model because it allows blank values and checks two fields simultaneously.
"""
super().clean()
if self.brand:
# Check if brand is used as name by another Siae
name_exists = Siae.objects.exclude(id=self.id).filter(Q(name=self.brand) | Q(brand=self.brand)).exists()
if name_exists:
raise ValidationError({"brand": "Ce nom commercial est déjà utilisé par une autre structure."})


@receiver(post_save, sender=Siae)
def siae_post_save(sender, instance, **kwargs):
Expand Down
13 changes: 13 additions & 0 deletions lemarche/www/dashboard_siaes/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ def test_siae_edit_info_form(self):
self.assertEqual(self.siae_with_user.brand, "Nouveau nom commercial")
self.assertEqual(self.siae_with_user.name_display, "Nouveau nom commercial")

def test_siae_edit_info_form_brand_unique(self):
SiaeFactory(brand="Nouveau nom commercial")

self.client.force_login(self.user_siae)
url = reverse("dashboard_siaes:siae_edit_info", args=[self.siae_with_user.slug])

data = {
"brand": "Nouveau nom commercial",
}
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Ce nom commercial est déjà utilisé par une autre structure.")


class DashboardSiaeUserViewTest(TestCase):
@classmethod
Expand Down

0 comments on commit 47d5d04

Please sign in to comment.