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

fixes to display recurring events and whole-day events correctly #22

Open
wants to merge 1 commit into
base: master
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
41 changes: 34 additions & 7 deletions ftw/calendar/browser/calendarupdateview.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
from zope.component import getMultiAdapter
from zope.interface import implements
import simplejson as json
from plone.app.event.base import get_events
from plone.app.event.base import RET_MODE_ACCESSORS
from email.utils import formatdate
from datetime import timedelta
import time


def rfc822_dt(dt):
return formatdate(time.mktime(dt.timetuple()))


class CalendarJSONSource(object):
Expand All @@ -19,12 +28,7 @@ def __init__(self, context, request):
self.context.portal_membership.getAuthenticatedMember().id

def generate_json_calendar_source(self):
result = []

for brain in self.get_event_brains():
result.append(self.generate_source_dict_from_brain(brain))

return json.dumps(result, sort_keys=True)
return json.dumps(list(self.all_events()), sort_keys=True)

def get_event_brains(self):
args = {
Expand All @@ -48,6 +52,27 @@ def get_event_brains(self):
'query': '/'.join(self.context.getPhysicalPath())}
)

def all_events(self):
events = get_events(self.context,
ret_mode=RET_MODE_ACCESSORS,
expand=True,
start=DateTime(self.request.get('start')),
end=DateTime(self.request.get('end')))

for event in events:
duration = event.end - event.start
yield {"id": "UID_%s" % (event.uid),
"title": event.title,
"location": event.location,
"start": rfc822_dt(event.start),
"end": rfc822_dt(event.end),
"url": event.url,
"editable": False,
"allDay": (event.whole_day or duration >
timedelta(seconds=86390)),
"className": '',
"description": event.description}

def generate_source_dict_from_brain(self, brain):
# plone 4-5 compat
creator = brain.Creator
Expand All @@ -68,7 +93,9 @@ def generate_source_dict_from_brain(self, brain):
if isinstance(brain.end - brain.start, float):
delta = 1.0
else:
delta = datetime.timedelta(days=1)
# delta set to slightly (10 seconds) shorter than a full day so
# whole day events are handled correctly
delta = datetime.timedelta(seconds=86390)

if self.memberid in creator:
editable = True
Expand Down
5 changes: 2 additions & 3 deletions ftw/calendar/tests/test_calendar_json_source.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ftw.builder import Builder
from ftw.builder.dexterity import DexterityBuilder
from ftw.builder import create
from ftw.calendar.browser.calendarupdateview import CalendarJSONSource
from ftw.calendar.browser.interfaces import IFtwCalendarJSONSourceProvider
Expand All @@ -20,8 +20,7 @@ def setUp(self):
self.portal = self.layer['portal']
setRoles(self.portal, TEST_USER_ID, ['Manager'])
login(self.portal, TEST_USER_NAME)

self.event = create(Builder('event'))
self.event = create(DexterityBuilder('event'))

def test_adapter_provides_methods(self):
verifyObject(IFtwCalendarJSONSourceProvider, CalendarJSONSource(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages
import os

version = '2.1.2.dev0'
version = '2.1.3.dev0'

tests_require = ['ftw.testing [splinter]',
'ftw.builder',
Expand Down