Skip to content

Commit

Permalink
Make Calendar.slug field unique
Browse files Browse the repository at this point in the history
As the calendar slug is used in URLs to query a single calendar, it
should be unique to avoid ambiguous URLs and queries. To avoid the
ambiguities, make the Calendar.slug field unique.

Fixes #270
  • Loading branch information
kimarakov committed Jan 3, 2018
1 parent 251e1c9 commit 1b203b5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
16 changes: 16 additions & 0 deletions schedule/migrations/0008_calendar_slug_unique.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('schedule', '0007_merge_text_fields'),
]

operations = [
migrations.AlterField(
model_name='calendar',
name='slug',
field=models.SlugField(verbose_name='slug', max_length=200, unique=True),
),
]
2 changes: 1 addition & 1 deletion schedule/models/calendars.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class Calendar(with_metaclass(ModelBase, *get_model_bases('Calendar'))):
'''

name = models.CharField(_("name"), max_length=200)
slug = models.SlugField(_("slug"), max_length=200)
slug = models.SlugField(_("slug"), max_length=200, unique=True)
objects = CalendarManager()

class Meta(object):
Expand Down
9 changes: 4 additions & 5 deletions tests/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ class ProxyCalendar(Calendar):
class Meta:
proxy = True

base_object = Calendar.objects.create()

rule = Rule.objects.create()
self.assertIsInstance(
ProxyCalendar.objects.get_or_create_calendar_for_object(base_object),
ProxyCalendar.objects.get_or_create_calendar_for_object(rule),
ProxyCalendar
)

Expand Down Expand Up @@ -78,8 +77,8 @@ def test_get_calendar_for_object_without_calendars(self):
Calendar.objects.get_calendar_for_object(rule)

def test_get_calendar_for_object_with_more_than_one_calendar(self):
calendar_1 = Calendar.objects.create(name='My Cal 1')
calendar_2 = Calendar.objects.create(name='My Cal 2')
calendar_1 = Calendar.objects.create(name='My Cal 1', slug='my-cal-1')
calendar_2 = Calendar.objects.create(name='My Cal 2', slug='my-cal-2')
rule = Rule.objects.create()
calendar_1.create_relation(rule)
calendar_2.create_relation(rule)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_occurrence.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_create_occurrence_without_event(self):

def test_get_occurrences_non_intersection_returns_empty_occ(self):
rule = Rule.objects.create(frequency="DAILY")
cal = Calendar.objects.create(name="MyCal")
cal = Calendar.objects.create(name="MyCal", slug='mycal')
recurring_event = Event.objects.create(
title='Recent Event',
start=datetime.datetime(2016, 1, 5, 8, 0, tzinfo=pytz.utc),
Expand All @@ -94,7 +94,7 @@ def test_get_occurrences_non_intersection_returns_empty_occ(self):

def test_get_occurrences_is_sorted(self):
rule = Rule.objects.create(frequency="DAILY")
cal = Calendar.objects.create(name="MyCal")
cal = Calendar.objects.create(name="MyCal", slug='mycal')
recurring_event = Event.objects.create(
title='Recent Event',
start=datetime.datetime(2016, 1, 5, 8, 0, tzinfo=pytz.utc),
Expand Down

0 comments on commit 1b203b5

Please sign in to comment.