Skip to content
Merged
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
9 changes: 4 additions & 5 deletions events/importer.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from datetime import timedelta
from icalendar import Calendar as ICalendar
import pytz
import requests

from .models import EventLocation, Event, OccurringRule
from .utils import convert_dt_to_aware
from .utils import extract_date_or_datetime

DATE_RESOLUTION = timedelta(1)
TIME_RESOLUTION = timedelta(0, 0, 1)
Expand All @@ -29,8 +28,8 @@ def import_occurrence(self, event, event_data):
# Django will already convert to datetime by setting the time to 0:00,
# but won't add any timezone information. We will convert them to
# aware datetime objects manually.
dt_start = convert_dt_to_aware(event_data['DTSTART'].dt)
dt_end = convert_dt_to_aware(event_data['DTEND'].dt)
dt_start = extract_date_or_datetime(event_data['DTSTART'].dt)
dt_end = extract_date_or_datetime(event_data['DTEND'].dt)

# Let's mark those occurrences as 'all-day'.
all_day = (
Expand All @@ -40,7 +39,7 @@ def import_occurrence(self, event, event_data):

defaults = {
'dt_start': dt_start,
'dt_end': dt_end,
'dt_end': dt_end - timedelta(days=1) if all_day else dt_end,
'all_day': all_day
}

Expand Down
35 changes: 35 additions & 0 deletions events/migrations/0006_change_end_date_for_occurring_rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2017-10-29 15:24
from __future__ import unicode_literals

import datetime

from django.db import migrations
from django.db.models import F


def exclude_ending_day(apps, schema_editor):
OccurringRule = apps.get_model('events', 'OccurringRule')
db_alias = schema_editor.connection.alias
OccurringRule.objects.using(db_alias)\
.filter(all_day=True)\
.update(dt_end=F('dt_end') - datetime.timedelta(days=1))


def include_ending_day(apps, schema_editor):
OccurringRule = apps.get_model('events', 'OccurringRule')
db_alias = schema_editor.connection.alias
OccurringRule.objects.using(db_alias)\
.filter(all_day=True)\
.update(dt_end=F('dt_end') + datetime.timedelta(days=1))


class Migration(migrations.Migration):

dependencies = [
('events', '0005_auto_20170821_2000'),
]

operations = [
migrations.RunPython(exclude_ending_day, include_ending_day),
]
81 changes: 79 additions & 2 deletions events/tests/test_importer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import unittest

from django.test import TestCase
from django.conf import settings
from django.utils.timezone import datetime, make_aware

from events.importer import ICSImporter
from events.models import Calendar, Event
Expand Down Expand Up @@ -61,6 +61,15 @@ def test_modified_event(self):
e.description.rendered,
'<a href="https://www.barcamptools.eu/pycamp201604">PythonCamp Cologne 2016</a>'
)
self.assertTrue(e.next_or_previous_time.all_day)
self.assertEqual(
make_aware(datetime(year=2016, month=4, day=2)),
e.next_or_previous_time.dt_start
)
self.assertEqual(
make_aware(datetime(year=2016, month=4, day=3)),
e.next_or_previous_time.dt_end
)

ical = """\
BEGIN:VCALENDAR
Expand Down Expand Up @@ -94,3 +103,71 @@ def test_modified_event(self):
self.assertEqual(e.pk, e2.pk)
self.assertEqual(e2.calendar.url, EVENTS_CALENDAR_URL)
self.assertEqual(e2.description.rendered, 'Python Istanbul')
self.assertTrue(e.next_or_previous_time.all_day)
self.assertEqual(
make_aware(datetime(year=2016, month=4, day=2)),
e.next_or_previous_time.dt_start
)
self.assertEqual(
make_aware(datetime(year=2016, month=4, day=3)),
e.next_or_previous_time.dt_end
)

def test_import_event_excludes_ending_day_when_all_day_is_true(self):
ical = """\
BEGIN:VCALENDAR
BEGIN:VEVENT
DTSTART;VALUE=DATE:20150328
DTEND;VALUE=DATE:20150330
DTSTAMP:20150202T092425Z
UID:pythoncalendartest@python.org
SUMMARY:PythonCamp 2015 - Python Bar Camp in Cologne
DESCRIPTION:Python PythonCamp 2015 - Python Bar Camp in Cologne
LOCATION:GFU Cyrus AG\, Am Grauen Stein 27\, 51105 Cologne\, Germany
END:VEVENT
END:VCALENDAR
"""
importer = ICSImporter(self.calendar)
importer.import_events_from_text(ical)

all_day_event = Event.objects.get(uid='pythoncalendartest@python.org')
self.assertTrue(all_day_event.next_or_previous_time.all_day)
self.assertFalse(all_day_event.next_or_previous_time.single_day)
self.assertEqual(
make_aware(datetime(year=2015, month=3, day=28)),
all_day_event.next_or_previous_time.dt_start
)
self.assertEqual(
make_aware(datetime(year=2015, month=3, day=29)),
all_day_event.next_or_previous_time.dt_end
)

def test_import_event_does_not_exclude_ending_day_when_all_day_is_false(self):
ical = """\
BEGIN:VCALENDAR
BEGIN:VEVENT
DTSTART:20130802T200000Z
DTEND:20130802T203000Z
DTSTAMP:20150202T092425Z
UID:pythoncalendartestsingleday@python.org
SUMMARY:PythonCamp 2015 - Python Bar Camp in Cologne
DESCRIPTION:Python PythonCamp 2015 - Python Bar Camp in Cologne
LOCATION:GFU Cyrus AG\, Am Grauen Stein 27\, 51105 Cologne\, Germany
END:VEVENT
END:VCALENDAR
"""

importer = ICSImporter(self.calendar)
importer.import_events_from_text(ical)

single_day_event = Event.objects.get(uid='pythoncalendartestsingleday@python.org')
self.assertFalse(single_day_event.next_or_previous_time.all_day)
self.assertTrue(single_day_event.next_or_previous_time.single_day)
self.assertEqual(
make_aware(datetime(year=2013, month=8, day=2, hour=20)),
single_day_event.next_or_previous_time.dt_start
)
self.assertEqual(
make_aware(datetime(year=2013, month=8, day=2, hour=20, minute=30)),
single_day_event.next_or_previous_time.dt_end
)
6 changes: 6 additions & 0 deletions events/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ def date_to_datetime(date, tzinfo=None):
return datetime.datetime(*date.timetuple()[:6], tzinfo=tzinfo)


def extract_date_or_datetime(dt):
if isinstance(dt, datetime.datetime):
return convert_dt_to_aware(dt)
return dt


def convert_dt_to_aware(dt):
if not isinstance(dt, datetime.datetime):
dt = date_to_datetime(dt)
Expand Down
2 changes: 1 addition & 1 deletion templates/events/includes/time_tag.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% if next_time.single_day %}
<time datetime="{{ next_time.dt_start|date:'c' }}">{{ next_time.dt_start|date:"d N" }}<span class="say-no-more"> {{ next_time.dt_start|date:"Y" }}</span>{% if next_time.all_day %} {{ next_time.dt_start|date:"fA"|lower }} {{ next_time.dt_start|date:"e" }}{% if next_time.valid_dt_end %} – {{ next_time.dt_end|date:"fA"|lower }} {{ next_time.dt_end|date:"e" }}{% endif %}{% endif %}</time>
<time datetime="{{ next_time.dt_start|date:'c' }}">{{ next_time.dt_start|date:"d N" }}<span class="say-no-more"> {{ next_time.dt_start|date:"Y" }}</span>{% if not next_time.all_day %} {{ next_time.dt_start|date:"fA"|lower }} {{ next_time.dt_start|date:"e" }}{% if next_time.valid_dt_end %} – {{ next_time.dt_end|date:"fA"|lower }} {{ next_time.dt_end|date:"e" }}{% endif %}{% endif %}</time>
{% else %}
<time datetime="{{ next_time.dt_start|date:'c' }}">{{ next_time.dt_start|date:"d N" }}{% if next_time.valid_dt_end %} &ndash; {{ next_time.dt_end|date:"d N" }}{% endif %} <span class="say-no-more"> {{ next_time.dt_end|date:"Y" }}</span></time>
{% endif %}