diff --git a/events/models.py b/events/models.py index 1f2ab2cb0..017b919f3 100644 --- a/events/models.py +++ b/events/models.py @@ -181,6 +181,20 @@ def next_time(self): except IndexError: return None + def is_scheduled_to_start_this_year(self) -> bool: + if self.next_time: + current_year: int = timezone.now().year + if self.next_time.dt_start.year == current_year: + return True + return False + + def is_scheduled_to_end_this_year(self) -> bool: + if self.next_time: + current_year: int = timezone.now().year + if self.next_time.dt_end.year == current_year: + return True + return False + @property def previous_time(self): now = timezone.now() diff --git a/events/tests/test_models.py b/events/tests/test_models.py index 0f3bafe76..3d0938280 100644 --- a/events/tests/test_models.py +++ b/events/tests/test_models.py @@ -1,4 +1,6 @@ import datetime +from types import SimpleNamespace +from unittest.mock import patch from django.contrib.auth import get_user_model from django.test import TestCase @@ -62,7 +64,6 @@ def test_recurring_event(self): self.assertEqual(self.event.next_time.dt_start, recurring_time_dtstart) self.assertTrue(rt.valid_dt_end()) - rt.begin = now - datetime.timedelta(days=5) rt.finish = now - datetime.timedelta(days=3) rt.save() @@ -186,3 +187,61 @@ def test_event_previous_event(self): # 'Event.previous_event' can return None if there is no # OccurringRule or RecurringRule found. self.assertIsNone(self.event.previous_event) + + def test_scheduled_to_start_this_year_method(self): + test_datetime = SimpleNamespace( + now=lambda: timezone.datetime(timezone.now().year, + 6, 1, tzinfo=timezone.now().tzinfo) + ) + + with patch("django.utils.timezone", new=test_datetime) as mock_timezone: + with patch("events.models.timezone", new=test_datetime): + now = seconds_resolution(mock_timezone.now()) + + occurring_time_dtstart = now + datetime.timedelta(days=1) + OccurringRule.objects.create( + event=self.event, + dt_start=occurring_time_dtstart, + dt_end=occurring_time_dtstart + datetime.timedelta(days=3) + ) + self.assertTrue(self.event.is_scheduled_to_start_this_year()) + + OccurringRule.objects.get(event=self.event).delete() + + event_not_scheduled_to_start_this_year_occurring_time_dtstart = now + datetime.timedelta(days=365) + OccurringRule.objects.create( + event=self.event, + dt_start=event_not_scheduled_to_start_this_year_occurring_time_dtstart, + dt_end=event_not_scheduled_to_start_this_year_occurring_time_dtstart + datetime.timedelta(days=3) + ) + + self.assertFalse(self.event.is_scheduled_to_start_this_year()) + + def test_scheduled_to_end_this_year_method(self): + test_datetime = SimpleNamespace( + now=lambda: timezone.datetime(timezone.now().year, + 6, 1, tzinfo=timezone.now().tzinfo) + ) + + with patch("django.utils.timezone", new=test_datetime) as mock_timezone: + with patch("events.models.timezone", new=test_datetime): + now = seconds_resolution(mock_timezone.now()) + occurring_time_dtstart = now + datetime.timedelta(days=1) + + OccurringRule.objects.create( + event=self.event, + dt_start=occurring_time_dtstart, + dt_end=occurring_time_dtstart + ) + + self.assertTrue(self.event.is_scheduled_to_end_this_year()) + + OccurringRule.objects.get(event=self.event).delete() + + OccurringRule.objects.create( + event=self.event, + dt_start=now, + dt_end=now + datetime.timedelta(days=365) + ) + + self.assertFalse(self.event.is_scheduled_to_end_this_year()) diff --git a/events/tests/test_views.py b/events/tests/test_views.py index 1291252a5..34ca27831 100644 --- a/events/tests/test_views.py +++ b/events/tests/test_views.py @@ -6,7 +6,7 @@ from django.test import TestCase from django.utils import timezone -from ..models import Calendar, Event, EventCategory, EventLocation, RecurringRule +from ..models import Calendar, Event, EventCategory, EventLocation, RecurringRule, OccurringRule from ..templatetags.events import get_events_upcoming from users.factories import UserFactory @@ -18,6 +18,11 @@ def setUpTestData(cls): cls.calendar = Calendar.objects.create(creator=cls.user, slug="test-calendar") cls.event = Event.objects.create(creator=cls.user, calendar=cls.calendar) cls.event_past = Event.objects.create(title='Past Event', creator=cls.user, calendar=cls.calendar) + cls.event_single_day = Event.objects.create(title="Single Day Event", creator=cls.user, calendar=cls.calendar) + cls.event_starts_at_future_year = Event.objects.create(title='Event Starting Following Year', + creator=cls.user, calendar=cls.calendar) + cls.event_ends_at_future_year = Event.objects.create(title='Event Ending Following Year', + creator=cls.user, calendar=cls.calendar) cls.now = timezone.now() @@ -34,7 +39,6 @@ def setUpTestData(cls): begin=cls.now - datetime.timedelta(days=2), finish=cls.now - datetime.timedelta(days=1), ) - # Future event cls.future_event = Event.objects.create(title='Future Event', creator=cls.user, calendar=cls.calendar, featured=True) RecurringRule.objects.create( @@ -67,6 +71,22 @@ def setUpTestData(cls): finish=cls.now - datetime.timedelta(days=1), ) + cls.rule_single_day = OccurringRule.objects.create( + event=cls.event_single_day, + dt_start=recurring_time_dtstart, + dt_end=recurring_time_dtstart + ) + cls.rule_future_start_year = OccurringRule.objects.create( + event=cls.event_starts_at_future_year, + dt_start=recurring_time_dtstart + datetime.timedelta(weeks=52), + dt_end=recurring_time_dtstart + datetime.timedelta(weeks=53), + ) + cls.rule_future_end_year = OccurringRule.objects.create( + event=cls.event_ends_at_future_year, + dt_start=recurring_time_dtstart, + dt_end=recurring_time_dtend + datetime.timedelta(weeks=52) + ) + def test_events_homepage(self): url = reverse('events:events') response = self.client.get(url) @@ -74,11 +94,13 @@ def test_events_homepage(self): event_titles = [event.title for event in events] self.assertEqual(response.status_code, 200) - self.assertEqual(len(events), 6) + self.assertEqual(len(events), 9) self.assertIn('Future Event', event_titles) self.assertIn('Current Event', event_titles) self.assertIn('Past Event', event_titles) + self.assertIn('Event Starting Following Year', event_titles) + self.assertIn('Event Ending Following Year', event_titles) def test_calendar_list(self): calendars_count = Calendar.objects.count() @@ -93,7 +115,7 @@ def test_event_list(self): response = self.client.get(url) self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.context['object_list']), 3) + self.assertEqual(len(response.context['object_list']), 6) url = reverse('events:event_list_past', kwargs={"calendar_slug": 'unexisting'}) response = self.client.get(url) @@ -189,12 +211,21 @@ def test_event_detail(self): self.assertEqual(self.event, response.context['object']) def test_upcoming_tag(self): - self.assertEqual(len(get_events_upcoming()), 3) + self.assertEqual(len(get_events_upcoming()), 5) self.assertEqual(len(get_events_upcoming(only_featured=True)), 1) self.rule.begin = self.now - datetime.timedelta(days=3) self.rule.finish = self.now - datetime.timedelta(days=2) self.rule.save() - self.assertEqual(len(get_events_upcoming()), 2) + self.assertEqual(len(get_events_upcoming()), 5) + + def test_event_starting_future_year_displays_relevant_year(self): + event = self.event_starts_at_future_year + url = reverse('events:events') + response = self.client.get(url) + self.assertIn( + f'', + response.content.decode() + ) def test_context_data(self): url = reverse("events:events") @@ -204,6 +235,23 @@ def test_context_data(self): self.assertIn("upcoming_events", response.context) self.assertIn("events_now", response.context) + def test_event_ending_future_year_displays_relevant_year(self): + event = self.event_ends_at_future_year + url = reverse('events:events') + response = self.client.get(url) + self.assertIn( + f'', + response.content.decode() + ) + + def test_events_scheduled_current_year_does_not_display_current_year(self): + event = self.event_single_day + url = reverse('events:events') + response = self.client.get(url) + self.assertIn( # start date + f'', + response.content.decode() + ) class EventSubmitTests(TestCase): event_submit_url = reverse_lazy('events:event_submit') diff --git a/templates/events/event_list.html b/templates/events/event_list.html index 8d7b0d60f..c4d14321e 100644 --- a/templates/events/event_list.html +++ b/templates/events/event_list.html @@ -73,7 +73,11 @@

{{ object.title|

{% with object.next_time as next_time %} - {% include "events/includes/time_tag.html" %} + {% with object.is_scheduled_to_start_this_year as scheduled_start_this_year %} + {% with object.is_scheduled_to_end_this_year as scheduled_end_this_year %} + {% include "events/includes/time_tag.html" %} + {% endwith %} + {% endwith %} {% endwith %} {% if object.venue %} @@ -98,7 +102,11 @@

{{ object.title|striptags }}

{% with object.previous_time as next_time %} - {% include "events/includes/time_tag.html" %} + {% with object.is_scheduled_to_start_this_year as scheduled_start_this_year %} + {% with object.is_scheduled_to_end_this_year as scheduled_end_this_year %} + {% include "events/includes/time_tag.html" %} + {% endwith %} + {% endwith %} {% endwith %} {% if object.venue %} diff --git a/templates/events/includes/time_tag.html b/templates/events/includes/time_tag.html index 078fcf1dc..90bc4491b 100644 --- a/templates/events/includes/time_tag.html +++ b/templates/events/includes/time_tag.html @@ -1,5 +1,32 @@ {% if next_time.single_day %} - + {% else %} - + {% endif %} \ No newline at end of file