Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug that results in AttributeError - 'OccurrencePreview' object has no attribute 'object' #555

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion schedule/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


class Migration(migrations.Migration):

dependencies = [
("contenttypes", "0001_initial"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
Expand Down
1 change: 0 additions & 1 deletion schedule/migrations/0002_event_color_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class Migration(migrations.Migration):

dependencies = [("schedule", "0001_initial")]

operations = [
Expand Down
1 change: 0 additions & 1 deletion schedule/migrations/0003_auto_20160715_0028.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class Migration(migrations.Migration):

dependencies = [("schedule", "0002_event_color_event")]

operations = [
Expand Down
1 change: 0 additions & 1 deletion schedule/migrations/0004_text_fields_not_null.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class Migration(migrations.Migration):

dependencies = [("schedule", "0003_auto_20160715_0028")]

operations = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class Migration(migrations.Migration):

dependencies = [("schedule", "0004_text_fields_not_null")]

operations = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def reverse(apps, schema_editor):


class Migration(migrations.Migration):

dependencies = [("schedule", "0003_auto_20160715_0028")]

operations = [migrations.RunPython(forwards, reverse, elidable=True)]
1 change: 0 additions & 1 deletion schedule/migrations/0007_merge_text_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class Migration(migrations.Migration):

dependencies = [
("schedule", "0006_update_text_fields_empty_string"),
("schedule", "0005_verbose_name_plural_for_calendar"),
Expand Down
1 change: 0 additions & 1 deletion schedule/migrations/0008_gfk_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class Migration(migrations.Migration):

dependencies = [
("contenttypes", "0002_remove_content_type_name"),
("schedule", "0007_merge_text_fields"),
Expand Down
1 change: 0 additions & 1 deletion schedule/migrations/0009_merge_20180108_2303.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [("schedule", "0008_gfk_index")]

operations = []
1 change: 0 additions & 1 deletion schedule/migrations/0010_events_set_missing_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def forwards(apps, schema_editor):


class Migration(migrations.Migration):

dependencies = [("schedule", "0009_merge_20180108_2303")]

operations = [
Expand Down
1 change: 0 additions & 1 deletion schedule/migrations/0011_event_calendar_not_null.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


class Migration(migrations.Migration):

dependencies = [("schedule", "0010_events_set_missing_calendar")]

operations = [
Expand Down
1 change: 0 additions & 1 deletion schedule/migrations/0012_auto_20191025_1852.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class Migration(migrations.Migration):

dependencies = [("schedule", "0011_event_calendar_not_null")]

operations = [
Expand Down
1 change: 0 additions & 1 deletion schedule/migrations/0013_auto_20210502_2303.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("schedule", "0012_auto_20191025_1852"),
]
Expand Down
1 change: 0 additions & 1 deletion schedule/migrations/0014_use_autofields_for_pk.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class Migration(migrations.Migration):

dependencies = [
("schedule", "0013_auto_20210502_2303"),
]
Expand Down
2 changes: 0 additions & 2 deletions schedule/periods.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def __init__(
tzinfo=pytz.utc,
sorting_options=None,
):

self.utc_start = self._normalize_timezone_to_utc(start, tzinfo)

self.utc_end = self._normalize_timezone_to_utc(end, tzinfo)
Expand Down Expand Up @@ -442,7 +441,6 @@ def __init__(
)

def _get_day_range(self, date):

# localize the date before we typecast to naive dates
if self.tzinfo is not None and timezone.is_aware(date):
date = date.astimezone(self.tzinfo)
Expand Down
32 changes: 30 additions & 2 deletions 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 Period, weekday_names
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 Expand Up @@ -347,7 +376,6 @@ def api_occurrences(request):


def _api_occurrences(start, end, calendar_slug, timezone):

if not start or not end:
raise ValueError("Start and end parameters are required")
# version 2 of full calendar
Expand Down
5 changes: 0 additions & 5 deletions tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def test_edge_case_events(self):
self.assertEqual(0, len(occurrences_one))

def test_recurring_event_get_occurrences(self):

cal = Calendar.objects.create(name="MyCal")
rule = Rule.objects.create(frequency="WEEKLY")

Expand All @@ -76,7 +75,6 @@ def test_recurring_event_get_occurrences(self):
)

def test_event_get_occurrences_after(self):

cal = Calendar.objects.create(name="MyCal")
rule = Rule.objects.create(frequency="WEEKLY")

Expand Down Expand Up @@ -140,7 +138,6 @@ def test_recurring_event_get_occurrences_2(self):
)

def test_recurring_event_get_occurrences_after(self):

cal = Calendar.objects.create(name="MyCal")
rule = Rule.objects.create(frequency="WEEKLY")
recurring_event = self.__create_recurring_event(
Expand Down Expand Up @@ -188,7 +185,6 @@ def test_recurring_event_with_moved_get_occurrences_after(self):
self.assertEqual(occurrence, occurrence2)

def test_recurring_event_get_occurrence(self):

cal = Calendar.objects.create(name="MyCal")
rule = Rule.objects.create(frequency="WEEKLY")

Expand Down Expand Up @@ -442,7 +438,6 @@ def test_recurring_event_get_occurrence_different_end_timezone(self):
self.assertEqual(occurrences[-1].end, end_recurring)

def test_recurring_event_get_occurrence_across_dst(self):

pacific = pytz.timezone("US/Pacific")
e_start = pacific.localize(datetime.datetime(2015, 3, 4, 9, 0))
e_end = e_start
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)
)