def import_from_hcal(self, url): if not self.has_ical_support(): return "Calendaring product not installed." import os import Globals # lxml.etree introduces a new class, lxml.etree.XSLT. # The class can be given an ElementTree object to construct an # XSLT transformer: from lxml import etree f = os.path.join(Globals.package_home(globals()), 'xhtml2vcal.xsl') xslt_doc = etree.parse(f) transform = etree.XSLT(xslt_doc) # You can then run the transformation on an ElementTree # document by simply calling it, and this results in another # ElementTree object: remote_page = urllib2.urlopen(url) parsed_page = etree.parse(remote_page) result = transform.apply(parsed_page) ical = StringIO(transform.tostring(result)) ct = getToolByName(self.context, 'portal_calendar') items = ct.importCalendar(ical, dest=self.context, do_action=True) return _(u"%s items imported") % len(items)
def importFormHandler(self): if self.request.get('file') is not None: ct = getToolByName(self.context, 'portal_calendar') items = ct.importCalendar(self.request.get('file'), dest=self.context, do_action=True) self.request.portal_status_message = _(u"%s items imported") \ % len(items) if self.request.get('url') is not None: self.request.portal_status_message = \ self.import_from_url(self.request.get('url'))
def import_from_url(self, url): if not self.has_ical_support(): return "Calendaring product not installed." res = urllib2.urlopen(url) text = '\n'.join(res.readlines()) # Make sure it really is UTF8, to avoid failure later: try: text.decode('utf8') except UnicodeDecodeError: try: # Maybe it's Latin-1? That's a break of specs, # but a common one. text = text.decode('latin1') # Yup, sure is. Re-encode as utf8: text = text.encode('utf8', 'replace') except UnicodeDecodeError: # We have no idea, what this is, so lets just reencode it # as UTF8 and replace everything weird with <?>. text = text.encode('utf8', 'replace').encode('utf8', 'replace') ical = StringIO(text) ct = getToolByName(self.context, 'portal_calendar') items = ct.importCalendar(ical, dest=self.context, do_action=True) return _(u"%s items imported") % len(items)