From 1b203b5d08a4ed4b5d37ea1036ae866593b17f87 Mon Sep 17 00:00:00 2001 From: kimarakov Date: Tue, 2 Jan 2018 04:48:21 -0800 Subject: [PATCH] Make Calendar.slug field unique 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 --- schedule/migrations/0008_calendar_slug_unique.py | 16 ++++++++++++++++ schedule/models/calendars.py | 2 +- tests/test_calendar.py | 9 ++++----- tests/test_occurrence.py | 4 ++-- 4 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 schedule/migrations/0008_calendar_slug_unique.py diff --git a/schedule/migrations/0008_calendar_slug_unique.py b/schedule/migrations/0008_calendar_slug_unique.py new file mode 100644 index 0000000..13221c4 --- /dev/null +++ b/schedule/migrations/0008_calendar_slug_unique.py @@ -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), + ), + ] diff --git a/schedule/models/calendars.py b/schedule/models/calendars.py index a59e424..bd16a81 100644 --- a/schedule/models/calendars.py +++ b/schedule/models/calendars.py @@ -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): diff --git a/tests/test_calendar.py b/tests/test_calendar.py index 0fae67a..ad541f5 100644 --- a/tests/test_calendar.py +++ b/tests/test_calendar.py @@ -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 ) @@ -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) diff --git a/tests/test_occurrence.py b/tests/test_occurrence.py index da617c3..8953cd0 100644 --- a/tests/test_occurrence.py +++ b/tests/test_occurrence.py @@ -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), @@ -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),