Esempio n. 1
0
def generate_ical_event(event, now):
    cal_event = Event()
    cal_event.add('DTSTAMP', vDatetime(now))
    cal_event.add('SEQUENCE', 0)
    cal_event.add('UID', 'event_{}@foss.events'.format(event['id']))

    cal_event.add('summary', event['label'])
    cal_event.add('dtstart', vDate(event['start_date']))

    cal_end_date = event['end_date'] + timedelta(days=1)
    cal_event.add('dtend', vDate(cal_end_date))

    cal_event.add('location', event['readable_location'])
    cal_event.add('url', vUri(event['abs_details_url']))

    description = event['description'] or ''
    description += '\n\n'

    if event['has_details']:
        description += 'Read everything about ' + event['label'] + ' in a nutshell on // foss.events: ' + event['abs_details_url'] + '\n\n'

    description += 'Official Homepage: ' + event['homepage'] + '\n\n'

    if event['osm_link']:
        description += 'Find your way: ' + event['osm_link'] + '\n'

    cal_event['description'] = remove_tags(description)

    if event['lat'] and event['lon']:
        cal_event['geo'] = vGeo([event['lat'], event['lon']])

    return cal_event
Esempio n. 2
0
    def get(self, request, *args, **kwargs):
        events = (Event.objects.filter(is_online=True).filter(
            begins_at__gte=datetime.now() - timedelta(days=90)))

        cal = icalendar.Calendar()
        cal.add('version', '2.0')
        cal.add('prodid', '-//PortailVA events calendar//mxm.dk//')
        tz = pytz.timezone('Europe/Paris')

        for event in events:
            ev = icalendar.Event()
            if event.association.acronym:
                ev.add('summary',
                       event.name + ' (' + event.association.acronym + ')')
            else:
                ev.add('summary',
                       event.name + ' (' + event.association.name + ')')
            ev.add('description', event.description)
            # Use vText to prevent iCalendar to handle the string as a dictionary and not as a text
            # See https://icalendar.readthedocs.io/en/latest/usage.html#example
            ev.add('categories', vText(event.type.name))
            ev.add('dtstart', event.begins_at.replace(tzinfo=tz))
            ev.add('dtend', event.ends_at.replace(tzinfo=tz))
            if event.place:
                ev.add('location', event.place.name)
                ev.add('geo', vGeo([event.place.lat, event.place.long]))

            ev.add('uid', event.id)
            cal.add_component(ev)

        response = HttpResponse(content_type='text/calendar')
        response.write(cal.to_ical().replace(b'\r\n', b'\n').strip())
        return response
Esempio n. 3
0
def build_reservations_ical_file(reservations):
    """
    Return iCalendar file containing given reservations
    """

    cal = Calendar()
    cal['X-WR-CALNAME'] = vText('RESPA')
    cal['name'] = vText('RESPA')
    for reservation in reservations:
        event = Event()
        event['uid'] = 'respa_reservation_{}'.format(reservation.id)
        event['dtstart'] = vDatetime(reservation.begin)
        event['dtend'] = vDatetime(reservation.end)
        unit = reservation.resource.unit
        event['location'] = vText('{} {} {}'.format(unit.name, unit.street_address, unit.address_zip))
        event['geo'] = vGeo(unit.location)
        event['summary'] = vText('{} {}'.format(unit.name, reservation.resource.name))
        cal.add_component(event)
    return cal.to_ical()
Esempio n. 4
0
def build_reservations_ical_file(reservations):
    """
    Return iCalendar file containing given reservations
    """

    cal = Calendar()
    for reservation in reservations:
        event = Event()
        begin_utc = timezone.localtime(reservation.begin, timezone.utc)
        end_utc = timezone.localtime(reservation.end, timezone.utc)
        event['uid'] = 'respa_reservation_{}'.format(reservation.id)
        event['dtstart'] = vDatetime(begin_utc)
        event['dtend'] = vDatetime(end_utc)
        unit = reservation.resource.unit
        event['location'] = vText('{} {} {}'.format(unit.name, unit.street_address, unit.address_zip))
        if unit.location:
            event['geo'] = vGeo(unit.location)
        event['summary'] = vText('{} {}'.format(unit.name, reservation.resource.name))
        cal.add_component(event)
    return cal.to_ical()
Esempio n. 5
0
def generate_event_ical_files(events):
    for event in events:

        cal = Calendar()
        cal.add('prodid', '-//foss.events//foss.events//')
        cal.add('version', '1.3.3.7')

        cal_event = Event()
        cal_event.add('summary', event['label'])
        cal_event.add('dtstart', vDate(event['start_date']))

        cal_end_date = event['end_date'] + timedelta(days=1)
        cal_event.add('dtend', vDate(cal_end_date))

        cal_event.add('location', event['readable_location'])
        cal_event.add('url', vUri(event['abs_details_url']))

        description = event['description'] or ''
        description += '\n\n'

        if event['has_details']:
            description += 'Read everything about ' + event[
                'label'] + ' in a nutshell on // foss.events: ' + event[
                    'abs_details_url'] + '\n\n'

        description += 'Official Homepage: ' + event['homepage'] + '\n\n'

        if event['osm_link']:
            description += 'Find your way: ' + event['osm_link'] + '\n'

        cal_event['description'] = remove_tags(description)

        cal.add_component(cal_event)

        if event['lat'] and event['lon']:
            cal_event['geo'] = vGeo([event['lat'], event['lon']])

        filepath = generate_event_ical_path(event)
        with open('build/' + filepath, 'wb') as f:
            f.write(cal.to_ical())
Esempio n. 6
0
def create_ical_entry (e, request):
        event = icalendar.Event()

        # TODO should we generate an UUID when creating the event?
        uid = u'*****@*****.**' % (str(e.id))
        event['uid'] = icalendar.vText(uid)
        event['dtstamp'] = icalendar.vDatetime(datetime.utcnow())

        # The sequence field must be incremented each time the event is modifed.
        # The trick here is to subtract the create TS from the modify TS and
        # use the difference as sequence.
        sequence = 0
        if e.date_time_created and e.date_time_modified:
            createTimestamp = time.mktime(e.get_date_time_created_utc().timetuple())
            modifyTimestamp = time.mktime(e.get_date_time_modified_utc().timetuple())
            sequence = modifyTimestamp - createTimestamp
        event['sequence'] = icalendar.vInt(sequence) + 1

        # created and last-modified
        if e.date_time_created:
            event['created'] = icalendar.vDatetime(e.get_date_time_created_utc())
        if e.date_time_modified:
            event['last-modified'] = icalendar.vDatetime(e.get_date_time_modified_utc())

        # TENTATIVE, CONFIRMED, CANCELLED
        if e.canceled:
            event['status'] = icalendar.vText(u'CANCELLED')
        else:
            event['status'] = icalendar.vText(u'CONFIRMED')

        relative_url = e.get_absolute_url()
        absolute_url = request.build_absolute_uri(relative_url)
        
        event['url'] = icalendar.vUri(absolute_url)

        if e.title:
            event['summary'] = icalendar.vText(e.title)

        description = u''
        if e.description:
            description += e.description
        if e.url:
            if len(description) > 0:
                description += u'\n\n'
            description += u'Event Webseite: ' + e.url
        if len(description) > 0:
            description += u'\n\n'
        description += u'Event bei Techism: ' + absolute_url
        event['description'] = icalendar.vText(description)

        if e.date_time_begin:
            event['dtstart'] = icalendar.vDatetime(e.get_date_time_begin_utc())
        if e.date_time_end:
            event['dtend'] = icalendar.vDatetime(e.get_date_time_end_utc())

        # geo value isn't used by iCal readers :-(
        # maybe a trick is to add the geo coordinates to the location field using the following format:
        # $latitude, $longitude ($name, $street, $city)
        if e.location:
            location = u'%s, %s, %s' % (e.location.name, e.location.street, e.location.city)
            event['location'] = icalendar.vText(location)
        if e.location and e.location.latitude and e.location.longitude:
            event['geo'] = icalendar.vGeo((e.location.latitude, e.location.longitude))

        return event
Esempio n. 7
0
tz = pytz.timezone('America/Denver')

event = Event()
event.add('dtstart', tz.localize(datetime.datetime(2018, 1, 13, 10, 00, 00)))
event.add('dtend', tz.localize(datetime.datetime(2018, 1, 13, 11, 15, 00)))
event.add('dtstamp', tz.localize(datetime.datetime(2018, 1, 9, 6, 27, 00)))
event.add('created', tz.localize(datetime.datetime(2018, 1, 9, 6, 27, 00)))
event.add('description', 'Description is really cool!')
event.add('comment', 'Commentary about this event is that it\'s the bomb!')
event.add('summary', 'In summary, come!')

imgPath = "/Users/Bowen/Documents/calendar/propose.PNG"
AttachPNG(event, imgPath)
event.add('url', 'www.google.com')

geo = icalendar.vGeo([39.743476, -105.003218])
event.add('geo', geo)
event.add('location', '1900 16th Street\nDenver, CO 80220')

alarm = Alarm()
alarm.add('action', 'DISPLAY')
alarm.add('description', 'Event starting soon')
dur = icalendar.vDuration(datetime.timedelta(0, 0, 0, 0, -23))
alarm.add('trigger', dur)
event.add_component(alarm)

cal.add_component(event)

calPath = "/Users/Bowen/Documents/calendar/testCal.ics"
with open(calPath, 'wb') as f:
    f.write(cal.to_ical())
Esempio n. 8
0
def ical(request):
    ninety_days = datetime.utcnow() + timedelta(days=90)
    event_list = service.get_event_query_set().filter(date_time_begin__lte=ninety_days).order_by('date_time_begin')
    
    cal = icalendar.Calendar()
    cal['prodid'] = icalendar.vText(u'-//Techism//Techism//DE')
    cal['version'] = icalendar.vText(u'2.0')
    cal['x-wr-calname'] = icalendar.vText(u'Techism')
    cal['x-wr-caldesc'] = icalendar.vText(u'Techism - IT-Events in München')
    
    for e in event_list:
        event = icalendar.Event()
        
        # TODO should we generate an UUID when creating the event?
        uid = u'*****@*****.**' % (str(e.id))
        event['uid'] = icalendar.vText(uid)
        event['dtstamp'] = icalendar.vDatetime(datetime.utcnow())
        
        # The sequence field must be incremented each time the event is modifed.
        # The trick here is to subtract the create TS from the modify TS and 
        # use the difference as sequence.
        sequence = 0
        if e.date_time_created and e.date_time_modified:
            createTimestamp = time.mktime(e.get_date_time_created_utc().timetuple())
            modifyTimestamp = time.mktime(e.get_date_time_modified_utc().timetuple())
            sequence = modifyTimestamp - createTimestamp
        event['sequence'] = icalendar.vInt(sequence)
        
        # created and last-modified
        if e.date_time_created:
            event['created'] = icalendar.vDatetime(e.get_date_time_created_utc())
        if e.date_time_modified:
            event['last-modified'] = icalendar.vDatetime(e.get_date_time_modified_utc())
        
        # TENTATIVE, CONFIRMED, CANCELLED
        event['status'] = icalendar.vText(u'CONFIRMED')
        
        if e.title:
            event['summary'] = icalendar.vText(e.title)
        if e.description:
            event['description'] = icalendar.vText(e.description)
        if e.date_time_begin:
            event['dtstart'] = icalendar.vDatetime(e.get_date_time_begin_utc())
        if e.date_time_end:
            event['dtend'] = icalendar.vDatetime(e.get_date_time_end_utc())
        if e.url:
            relative_url = reverse('event-show', args=[e.id])
            absolute_url = request.build_absolute_uri(relative_url)
            event['url'] = icalendar.vUri(absolute_url)
        
        # geo value isn't used by iCal readers :-(
        # maybe a trick is to add the geo coordinates to the location field using the following format:
        # $latitude, $longitude ($name, $street, $city)
        if e.location:
            location = u'%s, %s, %s' % (e.location.name, e.location.street, e.location.city)
            event['location'] = icalendar.vText(location)
        if e.location and e.location.latitude and e.location.longitude:
            event['geo'] = icalendar.vGeo((e.location.latitude, e.location.longitude))
        
        cal.add_component(event)
    
    response = HttpResponse(cal.as_string())
    response['Content-Type'] = 'text/calendar; charset=UTF-8'
    response['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate'
    response['Pragma'] = 'no-cache'
    response['Expires'] = 'Fri, 01 Jan 1990 00:00:00 GMT'
    return response