Skip to content

Commit

Permalink
Fix bug that results in AttributeError - 'OccurrencePreview' object h…
Browse files Browse the repository at this point in the history
…as no attribute 'object'
  • Loading branch information
Dan Stringer committed Jun 28, 2023
1 parent 8aa6f87 commit 64a8cec
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
31 changes: 30 additions & 1 deletion schedule/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

from schedule.forms import EventForm, OccurrenceForm
from schedule.models import Calendar, Event, Occurrence
from schedule.periods import weekday_names
from schedule.periods import weekday_names, Period
from schedule.settings import (
CHECK_EVENT_PERM_FUNC,
CHECK_OCCURRENCE_PERM_FUNC,
Expand Down Expand Up @@ -157,6 +157,35 @@ class OccurrenceView(OccurrenceMixin, DetailView):
class OccurrencePreview(OccurrenceMixin, ModelFormMixin, ProcessFormView):
template_name = "schedule/occurrence.html"

@property
def date_from_url(self):
return datetime.datetime(
int(self.kwargs["year"]),
int(self.kwargs["month"]),
int(self.kwargs["day"]),
int(self.kwargs["hour"]),
int(self.kwargs["minute"]),
int(self.kwargs["second"]),
tzinfo=pytz.UTC
)

def get_object(self, queryset=None):
event = get_object_or_404(Event, pk=self.kwargs["event_id"])
period = Period(
[event],
start=self.date_from_url,
end=self.date_from_url,
)

try:
return period.get_occurrences()[0]
except IndexError:
raise Http404

def get(self, request, *args, **kwargs):
self.object = self.get_object()
return super().get(request, *args, **kwargs)

def get_context_data(self, **kwargs):
context = super().get_context_data()
context = {"event": self.object.event, "occurrence": self.object}
Expand Down
41 changes: 41 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,3 +663,44 @@ def test_calendar_view_home(self):
self.assertTrue(
'<a href="/feed/calendar/upcoming/1/">Feed</a>' in response.content.decode()
)


class TestOccurrencePreview(TestCase):
def setUp(self):
self.rule = Rule.objects.create(frequency="DAILY")
self.calendar = Calendar.objects.create(name="MyCal", slug="MyCalSlug")
self.event = Event.objects.create(
title="Recent Event",
start=datetime.datetime(2008, 1, 5, 8, 0, tzinfo=pytz.utc),
end=datetime.datetime(2008, 1, 5, 9, 0, tzinfo=pytz.utc),
end_recurring_period=datetime.datetime(2008, 5, 5, 0, 0, tzinfo=pytz.utc),
rule=self.rule,
calendar=self.calendar,
)

def test_generates_preview(self):
url = reverse(
"occurrence_by_date",
kwargs={
"event_id": self.event.pk,
"year": 2008,
"month": 4,
"day": 20,
"hour": 8,
"minute": 30,
"second": 0,
}
)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "schedule/occurrence.html")
self.assertEqual(response.context["event"], self.event)

occurrence = response.context["occurrence"]
self.assertEqual(occurrence.event, self.event)
self.assertEqual(
occurrence.start, datetime.datetime(2008, 4, 20, 8, 0, tzinfo=pytz.utc)
)
self.assertEqual(
occurrence.end, datetime.datetime(2008, 4, 20, 9, 0, tzinfo=pytz.utc)
)

0 comments on commit 64a8cec

Please sign in to comment.