Skip to content

Commit

Permalink
Merge branch 'main' into fix-applications
Browse files Browse the repository at this point in the history
  • Loading branch information
amakarudze committed Jul 26, 2023
2 parents 8a2394f + 6c90ac2 commit f4a7d9c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
16 changes: 16 additions & 0 deletions coach/migrations/0003_alter_coach_unique_together.py
Original file line number Diff line number Diff line change
@@ -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")},
),
]
13 changes: 12 additions & 1 deletion coach/models.py
Original file line number Diff line number Diff line change
@@ -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 _
Expand All @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion djangogirls/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
12 changes: 12 additions & 0 deletions tests/coach/test_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest
from django.core.exceptions import ValidationError

from coach.models import DEFAULT_COACH_PHOTO, Coach


Expand All @@ -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

0 comments on commit f4a7d9c

Please sign in to comment.