From ea8a5ec520c9f2f6321a754072902889742821f0 Mon Sep 17 00:00:00 2001 From: Anna Makarudze Date: Tue, 16 May 2023 04:04:21 +0200 Subject: [PATCH 1/2] Add unique_together for name and twitter_handle for coach model --- .../0003_alter_coach_unique_together.py | 16 ++++++++++++++++ coach/models.py | 13 ++++++++++++- djangogirls/settings.py | 2 +- tests/coach/test_models.py | 12 ++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 coach/migrations/0003_alter_coach_unique_together.py diff --git a/coach/migrations/0003_alter_coach_unique_together.py b/coach/migrations/0003_alter_coach_unique_together.py new file mode 100644 index 000000000..5792d1b07 --- /dev/null +++ b/coach/migrations/0003_alter_coach_unique_together.py @@ -0,0 +1,16 @@ +# Generated by Django 3.2.19 on 2023-05-16 01:35 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("coach", "0002_alter_coach_id"), + ] + + operations = [ + migrations.AlterUniqueTogether( + name="coach", + unique_together={("name", "twitter_handle")}, + ), + ] diff --git a/coach/models.py b/coach/models.py index c037eec17..25f704e20 100644 --- a/coach/models.py +++ b/coach/models.py @@ -1,4 +1,5 @@ -from django.db import models +from django.core.exceptions import ValidationError +from django.db import IntegrityError, models from django.templatetags.static import static from django.urls import reverse from django.utils.translation import gettext_lazy as _ @@ -21,6 +22,7 @@ class Coach(models.Model): class Meta: ordering = ("name",) verbose_name_plural = _("Coaches") + unique_together = ["name", "twitter_handle"] def __str__(self): return self.name @@ -43,3 +45,12 @@ def photo_url(self): return DEFAULT_COACH_PHOTO return DEFAULT_COACH_PHOTO + + def save(self, *args, **kwargs): + try: + super().save(*args, **kwargs) + except IntegrityError: + raise ValidationError( + {"name": _(f"Coach with name {self.name} and twitter_handle {self.twitter_handle} " "already exists.")} + ) + return self diff --git a/djangogirls/settings.py b/djangogirls/settings.py index be9e4a9e1..0674cdd60 100644 --- a/djangogirls/settings.py +++ b/djangogirls/settings.py @@ -97,7 +97,7 @@ def gettext(s): default=f"postgres://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}:{POSTGRES_PORT}/{POSTGRES_DB}" ) -DEFAULT_AUTO_FIELD = "django.db.models.AutoField" +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" # Internationalization # https://docs.djangoproject.com/en/1.6/topics/i18n/ diff --git a/tests/coach/test_models.py b/tests/coach/test_models.py index ce4562a28..2dfd15340 100644 --- a/tests/coach/test_models.py +++ b/tests/coach/test_models.py @@ -1,3 +1,6 @@ +import pytest +from django.core.exceptions import ValidationError + from coach.models import DEFAULT_COACH_PHOTO, Coach @@ -6,3 +9,12 @@ def test_default_photo(): coach = Coach.objects.create(name="Test Test", twitter_handle="@test") assert Coach.objects.count() == 1 assert coach.photo_url == DEFAULT_COACH_PHOTO + + +def test_name_twitter_handle_unique_together(): + assert Coach.objects.count() == 0 + Coach.objects.create(name="Test Coach", twitter_handle="@testcoach") + assert Coach.objects.count() == 1 + with pytest.raises(ValidationError): + Coach.objects.create(name="Test Coach", twitter_handle="@testcoach") + assert Coach.objects.count() == 1 From 1173508ee01164daed85c4ab82cfb9d445f8e412 Mon Sep 17 00:00:00 2001 From: Anna Makarudze Date: Fri, 14 Jul 2023 17:59:17 +0200 Subject: [PATCH 2/2] Fix typo in query statement --- applications/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/views.py b/applications/views.py index 2c3562506..b72df6079 100644 --- a/applications/views.py +++ b/applications/views.py @@ -142,7 +142,7 @@ def application_detail(request, page_url, app_number): """ Display the details of a single application. """ - application = Application.object.filter(number=app_number).order_by("-created").first() + application = Application.objects.filter(number=app_number).order_by("-created").first() try: score = Score.objects.get(user=request.user, application=application) except Score.DoesNotExist: