diff options
Diffstat (limited to 'chimere/utils.py')
-rw-r--r-- | chimere/utils.py | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/chimere/utils.py b/chimere/utils.py index 96c41b4..1240914 100644 --- a/chimere/utils.py +++ b/chimere/utils.py @@ -1222,3 +1222,70 @@ class HtmlXsltManager(ImportManager): class XMLXsltManager(HtmlXsltManager): PARSER = 'XMLParser' + +import icalendar + + +class IcalManager(ImportManager): + def get(self): + u""" + Get data from an icalendar source + """ + from models import Marker + new_item, updated_item, msg = 0, 0, '' + source, msg = self.get_source_file([]) + if msg: + return (0, 0, msg) + + data = source.read() + try: + cal = icalendar.Calendar.from_ical(data) + except ValueError as e: + return (new_item, updated_item, + _(u"Error on icalendar parsing: " + e.message)) + + default_dct = {'origin': self.importer_instance.origin, + 'license': self.importer_instance.license} + if self.importer_instance.default_localisation: + default_dct['point'] = self.importer_instance.default_localisation + + for event in cal.walk('VEVENT'): + dct = default_dct.copy() + dct['name'] = event.get('SUMMARY', '') + if dct['name']: + dct['name'] = unicode(dct['name']) + dct['description'] = event.get('DESCRIPTION', '') + if dct['description']: + dct['description'] = unicode(dct['description']) + loc = event.get('LOCATION', None) + if loc: + dct['description'] += u"<br/>{}".format(unicode(loc)) + url = event.get('URL', None) + if url: + dct['description'] += u"<br/><a href='{}'>{}</a>".format( + unicode(url), unicode(_(u'Link'))) + dct['start_date'] = event.get('DTSTART', None) + if dct['start_date']: + dct['start_date'] = event.decoded('DTSTART') + dct['end_date'] = event.get('DTEND', None) + if dct['end_date']: + dct['end_date'] = event.decoded('DTEND') + point = event.get('GEO', None) + if point: + dct['point'] = 'SRID=4326;POINT(%s %s)' % (point.longitude, + point.latitude) + + if not dct.get('point', None): + continue + + cls = Marker + pl_id = event.get('UID', None) + if not pl_id: + pl_id = dct['name'] + "-" + unicode(self.importer_instance.pk) + pl_id += "-" + unicode(self.importer_instance.pk) + it, updated, created = self.create_or_update_item(cls, dct, pl_id) + if updated: + updated_item += 1 + if created: + new_item += 1 + return (new_item, updated_item, msg) |