This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
tabellio.icalimport/tabellio/icalimport/icalimport.py

66 lines
2.7 KiB
Python

import datetime
import urllib2
import vobject
from zope import component
from Products.CMFCore.WorkflowCore import WorkflowException
from Products.CMFCore.utils import getToolByName
from Products.Five.browser import BrowserView
from plone.registry.interfaces import IRegistry
from tabellio.config.interfaces import ITabellioSettings
class IcalImport(BrowserView):
def __call__(self):
self.settings = component.getUtility(IRegistry).forInterface(ITabellioSettings, False)
self.plone_utils = getToolByName(self.context, 'plone_utils')
self.portal_workflow = getToolByName(self.context, 'portal_workflow')
url = self.request.form.get('url')
if self.settings.ical_username:
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url,
self.settings.ical_username, self.settings.ical_password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
urlopen = opener.open
else:
urlopen = urllib2.urlopen
content = urlopen(url).read()
parsed_cal = vobject.readOne(content)
for vevent in parsed_cal.vevent_list:
title = vevent.summary.value
date_start = vevent.dtstart.value
if isinstance(date_start, datetime.date):
date_start = datetime.datetime.fromordinal(date_start.toordinal())
try:
date_end = vevent.dtend.value
if isinstance(date_end, datetime.date):
date_end = datetime.datetime.fromordinal(date_end.toordinal())
except:
date_end = None
event_id = self.plone_utils.normalizeString(
'%04d-%02d-%02d-%s' % (date_start.year,
date_start.month,
date_start.day,
title))
if not hasattr(self.context, event_id):
self.context.invokeFactory('tabellio.agenda.event', event_id, title=title)
event = getattr(self.context, event_id)
event.title = title
event.start = date_start
event.end = date_end
try:
event.description = vevent.description.value
except:
pass
try:
event.location = vevent.location.value
except:
pass
try:
self.portal_workflow.doActionFor(getattr(self.context, event_id), 'publish')
except WorkflowException:
pass