def marshall(self, instance, **kw):
     events = kw.get('events', None)
     if events is None:
         event_types = getattr(instance, 'event_types',
                               ('ATEvent', 'Event'))
         events = instance.contentValues(
             filter={'portal_type': event_types})
     events.sort()
     vevents = [marshall_event_to_vevent(event) for event in events]
     if not vevents:
         # There were no events.  iCalendar spec (RFC 2445) requires
         # VCALENDAR to have at least one subcomponent.  Let's create
         # a fake event.
         # NB Mozilla Calendar produces a 0-length file when publishing
         # empty calendars.  Sadly it does not then accept them
         # (http://bugzilla.mozilla.org/show_bug.cgi?id=229266).
         dtstamp = datetime.utcnow().strftime("%Y%m%dT%H%M%SZ")
         event = VEvent()
         vevents.append(event)
         event.add('uid', "placeholder-%s" % dtstamp)
         event.add('summary', 'Empty calendar')
         dtstart = vDatetime.from_ical(dtstamp[:-1])
         event.add('dtstart', dtstart)
         dtstamp = vDatetime.from_ical(dtstamp)
         event.add('dtstamp', dtstamp)
     vcal = wrap_into_vcalendar(*vevents,
                                **{'title': instance.context.Title()})
     return ('text/calendar', len(vcal), vcal)
 def marshall(self, instance, **kw):
     events = kw.get('events', None)
     if events is None:
         event_types = getattr(instance, 'event_types', ('ATEvent', 'Event'))
         events = instance.contentValues(
             filter={'portal_type':event_types})
     events.sort()
     vevents = [marshall_event_to_vevent(event) for event in events]
     if not vevents:
         # There were no events.  iCalendar spec (RFC 2445) requires
         # VCALENDAR to have at least one subcomponent.  Let's create
         # a fake event.
         # NB Mozilla Calendar produces a 0-length file when publishing
         # empty calendars.  Sadly it does not then accept them
         # (http://bugzilla.mozilla.org/show_bug.cgi?id=229266).
         dtstamp = datetime.utcnow().strftime("%Y%m%dT%H%M%SZ")
         event = VEvent()
         vevents.append(event)
         event.add('uid', "placeholder-%s" % dtstamp)
         event.add('summary', 'Empty calendar')
         dtstart = vDatetime.from_ical(dtstamp[:-1])
         event.add('dtstart', dtstart)
         dtstamp = vDatetime.from_ical(dtstamp)
         event.add('dtstamp', dtstamp)	
     vcal = wrap_into_vcalendar(*vevents, **{'title': instance.context.Title()})
     return ('text/calendar', len(vcal), vcal)
def marshall_event_to_vevent(instance):
    wf = getToolByName(instance, 'portal_workflow')
    dtstamp = utc_strftime(datetime.utcnow(), "%Y%m%dT%H%M%SZ")
    uid_hash = abs(hash(instance))
    state = wf.getInfoFor(instance, 'review_state')
    status = re_status_mapping.get(state, 'TENTATIVE')
    event = VEvent()
    event.add('uid', generateUID(instance))
    event.add('summary', instance.Title(), encode=False)
    event.add('description', instance.Description(), encode=False)
    if hasattr(aq_base(instance), 'getLocation'):
        location = instance.getLocation()
        if location:
            event.add('location', location, encode=False)
    if hasattr(aq_base(instance), 'Subject'):
        subject = instance.Subject()
        if subject:
            for category in subject:
                event.add('categories', category, encode=False)
    if hasattr(aq_base(instance), 'event_url'):
        url = instance.event_url
        if callable(url):
            # Ugh, it's callable in ATContentTypes.
            url = url()
        if url:
            event.add('url', url)
            event['url'].params['value'] = 'URI'
    if hasattr(aq_base(instance), 'getAttendees'):
        for att in instance.getAttendees():
            name, email = parseaddr(att)
            if not name:
                name = att
                email = ''
            if email:
                email = 'mailto:%s' % email
            else:
                email = 'invalid:nomail'
            address = vCalAddress(email)
            address.params['cn'] = name
            event.add('attendee', address, encode=False)
    if (hasattr(aq_base(instance), 'duration')
            and instance.duration == date.resolution):
        # All day event.
        dtstart = vDate.from_ical(utc_strftime(instance.start_date, '%Y%m%d'))
        event.add('dtstart', dtstart)
        dtend = vDate.from_ical(utc_strftime(instance.end_date, '%Y%m%d'))
        event.add('dtend', dtend)
    else:
        # Normal event
        dtstart = vDatetime.from_ical(
            utc_strftime(instance.start_date, '%Y%m%dT%H%M%SZ'))
        event.add('dtstart', dtstart)
        dtend = vDatetime.from_ical(
            utc_strftime(instance.end_date, '%Y%m%dT%H%M%SZ'))
        event.add('dtend', dtend)
    dtstamp = vDatetime.from_ical(dtstamp)
    event.add('dtstamp', dtstamp)
    #event.add('status', status)
    return event
def marshall_event_to_vevent(instance):
    wf = getToolByName(instance, 'portal_workflow')
    dtstamp = utc_strftime(datetime.utcnow(), "%Y%m%dT%H%M%SZ")
    uid_hash = abs(hash(instance))
    state = wf.getInfoFor(instance, 'review_state')
    status = re_status_mapping.get(state, 'TENTATIVE')
    event = VEvent()
    event.add('uid', generateUID(instance))
    event.add('summary', instance.Title(), encode=False)
    event.add('description', instance.Description(), encode=False)
    if hasattr(aq_base(instance), 'getLocation'):
        location = instance.getLocation()
        if location:
            event.add('location', location, encode=False)
    if hasattr(aq_base(instance), 'Subject'):
        subject = instance.Subject()
        if subject:
            for category in subject:
                event.add('categories', category, encode=False)
    if hasattr(aq_base(instance), 'event_url'):
        url = instance.event_url
        if callable(url):
            # Ugh, it's callable in ATContentTypes.
            url = url()
        if url:
            event.add('url', url)
            event['url'].params['value'] = 'URI'
    if hasattr(aq_base(instance), 'getAttendees'):
        for att in instance.getAttendees():
            name, email = parseaddr(att)
            if not name:
                name = att
                email = ''
            if email:
                email = 'mailto:%s' % email
            else:
                email = 'invalid:nomail'
            address = vCalAddress(email)
            address.params['cn'] = name
            event.add('attendee', address, encode=False)
    if (hasattr(aq_base(instance), 'duration') and
        instance.duration == date.resolution):
        # All day event.
        dtstart = vDate.from_ical(
            utc_strftime(instance.start_date, '%Y%m%d'))
        event.add('dtstart', dtstart)
        dtend = vDate.from_ical(
            utc_strftime(instance.end_date, '%Y%m%d'))
        event.add('dtend', dtend)
    else:
        # Normal event
        dtstart = vDatetime.from_ical(
            utc_strftime(instance.start_date, '%Y%m%dT%H%M%SZ'))
        event.add('dtstart', dtstart)
        dtend = vDatetime.from_ical(
            utc_strftime(instance.end_date, '%Y%m%dT%H%M%SZ'))
        event.add('dtend', dtend)
    dtstamp = vDatetime.from_ical(dtstamp)
    event.add('dtstamp', dtstamp)
    #event.add('status', status)
    return event