Exemplo n.º 1
0
def change():
    """ Change (create) or delete an event by POST request """
    calendar = Config.calendar()
    changes = request.get_json()
    for uid in changes["deleted"]:
        try:
            calendar.event_by_uid(uid).delete()
        except caldav.lib.error.NotFoundError:
            # the event haven't been in the calendar, maybe was just created and deleted
            pass

    for schedule in changes["created"]:
        c = iCalendar()
        c.add('prodid', '-//Schedule shift//csirt.cz//')
        c.add('version', '2.0')
        event = iEvent()
        event.add('uid', schedule["id"])
        event.add('summary', schedule["calendarId"] + " " + schedule["title"])
        # event.add('dtstart', parse(schedule["start"]["_date"]).astimezone().date()+timedelta(1))

        # print(schedule)
        # import ipdb; ipdb.set_trace()

        event.add('dtstart', parse(schedule["start-ics"]))
        event.add('dtend', parse(schedule["end-ics"]))
        # event.add('dtstart', parse(schedule["start"]["_date"]))
        # event.add('dtend', parse(schedule["end"]["_date"]))
        c.add_component(event)
        calendar.add_event(c.to_ical().decode("utf-8"))

    return "Saved"
Exemplo n.º 2
0
 def _as_ics(self, citymeo=False):
     if not self.initialized:
         self._lazy_init()
     cal = iCalendar()
     cal.add('prodid', '-//PonyConf.io//PonyConf//FR')
     cal.add('version', '2.0')
     cal.add('x-wr-calname', self.conference.name)
     cal.add('x-wr-timezone', settings.TIME_ZONE)
     cal.add('calscale', 'GREGORIAN')
     talks = self.talks
     if citymeo and talks.exists():
         talks = talks.filter(start_date__gte=now() - timedelta(minutes=5))
         if talks.exists():
             limit = talks.first().start_date.replace(hour=23,
                                                      minute=59,
                                                      second=59)
             talks = talks.filter(start_date__lte=limit)
     for talk in talks:
         event = iEvent()
         event.add('dtstart', talk.start_date)
         if not talk.end_date:
             continue
         event.add('dtend', talk.end_date)
         event.add('dtstamp', talk.updated)
         event.add('summary', talk.title)
         if talk.room:
             event.add('location', talk.room)
         event.add('status', 'CONFIRMED' if talk.accepted else 'TENTATIVE')
         if not citymeo:
             event.add('description', talk.description)
         event.add('uid', '%s/%s' % (self.site.domain, talk.id))
         cal.add_component(event)
     return cal.to_ical()
def to_ical_event(ev):
    """
    Convert Eventure Event to ical (CalDAV) string format
    Params: ev: core.models.event
    """
    cal = Calendar()
    cal.add('prodid', '-//Eventure Interactive.//CalDAV Client//EN')
    cal.add('version', '2.0')

    event = iEvent()
    event.add('summary', ev.title)
    event.add('dtstart', ev.start)
    event.add('dtend', ev.end)
    event.add('dtstamp', ev.created)
    event.add('uid', 'EVENTURE-' + str(ev.id))
    event['location'] = vText(ev.location)

    # Add Guests
    for guest in ev.guests.all():
        attendee = vCalAddress('MAILTO:%s' % (guest.email))
        attendee.params['cn'] = vText(guest.name)
        attendee.params['ROLE'] = vText('REQ-PARTICIPANT')
        event.add('attendee', attendee, encode=0)

    cal.add_component(event)

    return cal.to_ical()
Exemplo n.º 4
0
 def _as_ics(self, citymeo=False):
     if not self.initialized:
         self._lazy_init()
     cal = iCalendar()
     cal.add('prodid', '-//PonyConf.io//PonyConf//FR')
     cal.add('version', '2.0')
     cal.add('x-wr-calname', self.conference.name)
     cal.add('x-wr-timezone', settings.TIME_ZONE)
     cal.add('calscale', 'GREGORIAN')
     talks = self.talks
     if citymeo and talks.exists():
         talks = talks.filter(start_date__gte=now()-timedelta(minutes=5))
         if talks.exists():
             limit = talks.first().start_date.replace(hour=23, minute=59, second=59)
             talks = talks.filter(start_date__lte=limit)
     for talk in talks:
         event = iEvent()
         event.add('dtstart', talk.start_date)
         if not talk.end_date:
             continue
         event.add('dtend', talk.end_date)
         event.add('dtstamp', talk.updated)
         event.add('summary', talk.title)
         if talk.room:
             event.add('location', talk.room)
         event.add('status', 'CONFIRMED' if talk.accepted else 'TENTATIVE')
         if not citymeo:
             event.add('description', talk.description)
         event.add('uid', '%s/%s' % (self.site.domain, talk.id))
         cal.add_component(event)
     return cal.to_ical()
Exemplo n.º 5
0
def export(request, events, year=None):
    cal = iCalendar()
    site = Site.objects.get_current()

    cal.add('prodid', '-//%s Events Calendar//%s//' % (site.name, site.domain))
    cal.add('version', '2.0')

    site_token = site.domain.split('.')
    site_token.reverse()
    site_token = '.'.join(site_token)

    for event in events:
        show = event.show

        description = ""
        if show.info.headliner:
            description += "Headliner: %s\n" % show.info.headliner
        elif show.info.headliner_text:
            description += "Headliner: %s\n" % show.info.headliner_text
        if show.info.openers_text:
            description += "Openers: %s\n" % show.info.openers_text
        if show.info.venue:
            description += "Venue: %s\n" % show.info.venue
        else:
            description += "Venue: %s\n" % show.info.venue_text
        if show.info.description:
            description += "Description: %s\n" % show.info.description

        ical_event = iEvent()
        ical_event.add('summary', show.info.get_title())
        ical_event.add('description', description)
        ical_event.add('location', show.info.venue)
        ical_event.add('dtstart', show.date.start)
        ical_event.add('dtend', show.date.end and show.date.end
                       or show.date.start)
        ical_event.add('dtstamp', show.date.end and show.date.end
                       or show.date.start)
        ical_event['uid'] = '%d.event.events.%s' % (show.id, site_token)

        cal.add_component(ical_event)

    response = HttpResponse(cal.to_ical(), content_type="text/calendar")
    # title or headliner
    if year:
        response[
            'Content-Disposition'] = 'attachment; filename=%ss-calendar-%s.ics' % (
                slugify(request.user.profile), year)
    else:
        response[
            'Content-Disposition'] = 'attachment; filename=%s.ics' % slugify(
                events[0])
    return response
Exemplo n.º 6
0
 def getics():
     cal = Calendar()
     cal['summary'] = 'KLJ Ichtegem agenda'
     for evt in Event.query.all():
         ev = iEvent()
         ev.add('dtstart', evt.begin.replace(tzinfo=pytz.timezone("Europe/Brussels")))
         ev.add('dtend', evt.end.replace(tzinfo=pytz.timezone("Europe/Brussels")))
         ev['location'] = vText(evt.location)
         ev.add('summary', evt.title)
         ev.add('description', evt.description+"\nprijs: "+str(evt.price)+" EUR")
         cal.add_component(ev)
     c=cal.to_ical()
     return str(c)
Exemplo n.º 7
0
def to_icalendar(events):
    """ Converts events to .ics format
    :param events = Iterable(cal.schema.Event)
    :return bytes
    """
    summary = "Calendar for Columbia University made by ADI (adicu.com)"
    cal = iCalendar(dtstart=vDatetime(datetime.now()),
                    summary=summary)

    for e in events:
        # for every event, create an event object
        organizer = vCalAddress("MAILTO:''")
        organizer.params['cn'] = e.user.name
        vevent = iEvent(summary=e.name, organizer=organizer,
                        location=vText(e.location), dtstart=vDatetime(e.start),
                        description=e.url)

        cal.add_component(vevent)

    return cal.to_ical()
Exemplo n.º 8
0
    def item_export_html(self):
        try:
            from icalendar import Calendar as iCal
            from icalendar import Event as iEvent
        except ImportError:
            return self.render_error("Event Error", "Event exporting is not supported in this installation")
        cal = iCal()
        collection = self.item

        if self.cur_agent_can_global('do_anything'):
            recursive_filter = None
        else:
            visible_memberships = self.permission_cache.filter_items('view Membership.item', Membership.objects)
            recursive_filter = Q(child_memberships__in=visible_memberships.values('pk').query)
        all_members = collection.all_contained_collection_members(recursive_filter)
        all_events = Event.objects.filter(pk__in=all_members.values('pk').query)
        all_events = self.permission_cache.filter_items('view Event.start_date', all_events)
        all_events = self.permission_cache.filter_items('view Event.start_time', all_events)
        all_events = self.permission_cache.filter_items('view Event.end_date', all_events)

        cal.add('prodid', '-//Deme//Deme Calendar Module//EN')
        cal.add('version', '2.0')
        cal.add('calscale', 'GREGORIAN')

        for member in all_events:
            newEvent = iEvent()
            newEvent.add('summary', member.display_name())
            newEvent.add('dtstart', datetime.combine(member.start_date, member.start_time))
            newEvent.add('dtend', datetime.combine(member.end_date, member.end_time))
            newEvent.add('location', member.location)
            newEvent.add('description', member.body)
            cal.add_component(newEvent)

        response = HttpResponse(cal.as_string(), mimetype='text/calendar')
        response['Content-Disposition'] = 'attachment; filename=demeCalendar.ics'
        return response
Exemplo n.º 9
0
    def get(self, request):
        items = Event.objects.filter(status='ACTIVE')

        academies = []
        ids = request.GET.get('academy', '')
        slugs = request.GET.get('academy_slug', '')

        ids = ids.split(",") if ids else []
        slugs = slugs.split(",") if slugs else []

        if ids:
            items = Event.objects.filter(academy__id__in=ids,
                                         status='ACTIVE').order_by('id')

        elif slugs:
            items = Event.objects.filter(academy__slug__in=slugs,
                                         status='ACTIVE').order_by('id')

        else:
            items = []

        if not ids and not slugs:
            raise ValidationException(
                "You need to specify at least one academy or academy_slug (comma separated) in the querystring"
            )

        if (Academy.objects.filter(id__in=ids).count() != len(ids) or
                Academy.objects.filter(slug__in=slugs).count() != len(slugs)):
            raise ValidationException("Some academy not exist")

        upcoming = request.GET.get('upcoming')
        if upcoming == 'true':
            now = timezone.now()
            items = items.filter(starting_at__gte=now)

        academies_repr = ical_academies_repr(ids=ids, slugs=slugs)

        calendar = iCalendar()
        calendar.add('prodid',
                     f'-//BreatheCode//Academy Events{academies_repr}//EN')
        calendar.add('X-WR-CALNAME', f'Academy - Events')
        calendar.add('X-WR-CALDESC', '')
        calendar.add('REFRESH-INTERVAL', 'PT15M')

        calendar.add('version', '2.0')

        for item in items:
            event = iEvent()

            if item.title:
                event.add('summary', item.title)

            if item.description:
                event.add('description', item.description)

            event.add('uid', f'breathecode_event_{item.id}')
            event.add('dtstart', item.starting_at)
            event.add('dtend', item.ending_at)
            event.add('dtstamp', item.created_at)

            if item.author and item.author.email:
                organizer = vCalAddress(f'MAILTO:{item.author.email}')

                if item.author.first_name and item.author.last_name:
                    organizer.params['cn'] = vText(f'{item.author.first_name} '
                                                   f'{item.author.last_name}')
                elif item.author.first_name:
                    organizer.params['cn'] = vText(item.author.first_name)
                elif item.author.last_name:
                    organizer.params['cn'] = vText(item.author.last_name)

                organizer.params['role'] = vText('OWNER')
                event['organizer'] = organizer

            if item.venue and (item.venue.country or item.venue.state or
                               item.venue.city or item.venue.street_address):
                value = ''

                if item.venue.street_address:
                    value = f'{value}{item.venue.street_address}, '

                if item.venue.city:
                    value = f'{value}{item.venue.city}, '

                if item.venue.state:
                    value = f'{value}{item.venue.state}, '

                if item.venue.country:
                    value = f'{value}{item.venue.country}'

                value = re.sub(', $', '', value)
                event['location'] = vText(value)

            calendar.add_component(event)

        calendar_text = calendar.to_ical()

        response = HttpResponse(calendar_text, content_type='text/calendar')
        response['Content-Disposition'] = 'attachment; filename="calendar.ics"'
        return response
Exemplo n.º 10
0
    def get(self, request):
        items = Cohort.objects.all()

        ids = request.GET.get('academy', '')
        slugs = request.GET.get('academy_slug', '')

        ids = ids.split(",") if ids else []
        slugs = slugs.split(",") if slugs else []

        if ids:
            items = Cohort.objects.filter(academy__id__in=ids).order_by('id')

        elif slugs:
            items = Cohort.objects.filter(
                academy__slug__in=slugs).order_by('id')

        else:
            items = []

        if not ids and not slugs:
            raise ValidationException(
                "You need to specify at least one academy or academy_slug (comma separated) in the querystring"
            )

        if (Academy.objects.filter(id__in=ids).count() != len(ids) or
                Academy.objects.filter(slug__in=slugs).count() != len(slugs)):
            raise ValidationException("Some academy not exist")

        items = items.exclude(stage='DELETED')

        upcoming = request.GET.get('upcoming')
        if upcoming == 'true':
            now = timezone.now()
            items = items.filter(kickoff_date__gte=now)

        academies_repr = ical_academies_repr(ids=ids, slugs=slugs)

        calendar = iCalendar()
        calendar.add('prodid',
                     f'-//BreatheCode//Academy Cohorts{academies_repr}//EN')
        calendar.add('X-WR-CALNAME', f'Academy - Cohorts')
        calendar.add('X-WR-CALDESC', '')
        calendar.add('REFRESH-INTERVAL', 'PT15M')

        calendar.add('version', '2.0')

        for item in items:
            event = iEvent()

            event.add('summary', item.name)
            event.add('uid', f'breathecode_cohort_{item.id}')
            event.add('dtstart', item.kickoff_date)

            if item.ending_date:
                event.add('dtend', item.ending_date)

            event.add('dtstamp', item.created_at)

            teacher = CohortUser.objects.filter(role='TEACHER',
                                                cohort__id=item.id).first()

            if teacher:
                organizer = vCalAddress(f'MAILTO:{teacher.user.email}')

                if teacher.user.first_name and teacher.user.last_name:
                    organizer.params['cn'] = vText(
                        f'{teacher.user.first_name} '
                        f'{teacher.user.last_name}')
                elif teacher.user.first_name:
                    organizer.params['cn'] = vText(teacher.user.first_name)
                elif teacher.user.last_name:
                    organizer.params['cn'] = vText(teacher.user.last_name)

                organizer.params['role'] = vText('OWNER')
                event['organizer'] = organizer

            location = item.academy.name

            if item.academy.website_url:
                location = f'{location} ({item.academy.website_url})'
            event['location'] = vText(item.academy.name)

            calendar.add_component(event)

        calendar_text = calendar.to_ical()

        response = HttpResponse(calendar_text, content_type='text/calendar')
        response['Content-Disposition'] = 'attachment; filename="calendar.ics"'
        return response
Exemplo n.º 11
0
    def get(self, request):
        items = Cohort.objects.all()

        ids = request.GET.get('academy', '')
        slugs = request.GET.get('academy_slug', '')

        ids = ids.split(",") if ids else []
        slugs = slugs.split(",") if slugs else []

        if ids:
            items = Cohort.objects.filter(academy__id__in=ids).order_by('id')

        elif slugs:
            items = Cohort.objects.filter(
                academy__slug__in=slugs).order_by('id')

        else:
            items = []

        if not ids and not slugs:
            raise ValidationException(
                "You need to specify at least one academy or academy_slug (comma separated) in the querystring"
            )

        if (Academy.objects.filter(id__in=ids).count() != len(ids) or
                Academy.objects.filter(slug__in=slugs).count() != len(slugs)):
            raise ValidationException("Some academy not exist")

        items = items.exclude(stage='DELETED')

        upcoming = request.GET.get('upcoming')
        if upcoming == 'true':
            now = timezone.now()
            items = items.filter(kickoff_date__gte=now)

        academies_repr = ical_academies_repr(ids=ids, slugs=slugs)
        key = server_id()

        calendar = iCalendar()
        calendar.add(
            'prodid',
            f'-//BreatheCode//Academy Cohorts{academies_repr} {key}//EN')
        calendar.add('X-WR-CALNAME', f'Academy - Cohorts')
        calendar.add('X-WR-CALDESC', '')
        calendar.add('REFRESH-INTERVAL;VALUE=DURATION', 'PT15M')

        url = os.getenv('API_URL')
        if url:
            url = re.sub(r'/$', '', url) + '/v1/events/ical/cohorts'
            if ids or slugs:
                url = url + '?'

                if ids:
                    url = url + 'academy=' + ','.join(ids)

                if ids and slugs:
                    url = url + '&'

                if slugs:
                    url = url + 'academy_slug=' + ','.join(slugs)

            calendar.add('url', url)

        calendar.add('version', '2.0')

        for item in items:
            event = iEvent()
            event_first_day = iEvent()
            event_last_day = iEvent()
            has_last_day = False

            event.add('summary', item.name)
            event.add('uid', f'breathecode_cohort_{item.id}_{key}')
            event.add('dtstart', item.kickoff_date)

            timeslots = CohortTimeSlot.objects.filter(cohort__id=item.id)
            first_timeslot = timeslots.order_by('starting_at').first()

            if first_timeslot:
                event_first_day.add('summary', f'{item.name} - First day')
                event_first_day.add(
                    'uid', f'breathecode_cohort_{item.id}_first_{key}')
                event_first_day.add('dtstart', first_timeslot.starting_at)
                event_first_day.add('dtend', first_timeslot.ending_at)
                event_first_day.add('dtstamp', first_timeslot.created_at)

            if item.ending_date:
                event.add('dtend', item.ending_date)
                timeslots_datetime = []

                for timeslot in timeslots:
                    starting_at = timeslot.starting_at
                    ending_at = timeslot.ending_at
                    diff = ending_at - starting_at

                    if timeslot.recurrent:
                        ending_at = fix_datetime_weekday(item.ending_date,
                                                         ending_at,
                                                         prev=True)
                        starting_at = ending_at - diff

                    timeslots_datetime.append((starting_at, ending_at))

                last_timeslot = None

                if timeslots_datetime:
                    timeslots_datetime.sort(key=lambda x: x[1], reverse=True)
                    last_timeslot = timeslots_datetime[0]
                    has_last_day = True

                    event_last_day.add('summary', f'{item.name} - Last day')
                    event_last_day.add(
                        'uid', f'breathecode_cohort_{item.id}_last_{key}')
                    event_last_day.add('dtstart', last_timeslot[0])
                    event_last_day.add('dtend', last_timeslot[1])
                    event_last_day.add('dtstamp', item.created_at)

            event.add('dtstamp', item.created_at)

            teacher = CohortUser.objects.filter(role='TEACHER',
                                                cohort__id=item.id).first()

            if teacher:
                organizer = vCalAddress(f'MAILTO:{teacher.user.email}')

                if teacher.user.first_name and teacher.user.last_name:
                    organizer.params['cn'] = vText(
                        f'{teacher.user.first_name} '
                        f'{teacher.user.last_name}')
                elif teacher.user.first_name:
                    organizer.params['cn'] = vText(teacher.user.first_name)
                elif teacher.user.last_name:
                    organizer.params['cn'] = vText(teacher.user.last_name)

                organizer.params['role'] = vText('OWNER')
                event['organizer'] = organizer

                if first_timeslot:
                    event_first_day['organizer'] = organizer

                if has_last_day:
                    event_last_day['organizer'] = organizer

            location = item.academy.name

            if item.academy.website_url:
                location = f'{location} ({item.academy.website_url})'

            event['location'] = vText(item.academy.name)

            if first_timeslot:
                event_first_day['location'] = vText(item.academy.name)

            if has_last_day:
                event_last_day['location'] = vText(item.academy.name)

            if first_timeslot:
                calendar.add_component(event_first_day)
            calendar.add_component(event)

            if has_last_day:
                calendar.add_component(event_last_day)

        calendar_text = calendar.to_ical()

        response = HttpResponse(calendar_text, content_type='text/calendar')
        response['Content-Disposition'] = 'attachment; filename="calendar.ics"'
        return response
Exemplo n.º 12
0
    def get(self, request, user_id):
        items = Cohort.objects.all()

        if not User.objects.filter(id=user_id).count():
            raise ValidationException("Student not exist",
                                      404,
                                      slug='student-not-exist')

        cohort_ids = (CohortUser.objects.filter(user_id=user_id).values_list(
            'cohort_id', flat=True).exclude(cohort__stage='DELETED'))

        items = CohortTimeSlot.objects.filter(
            cohort__id__in=cohort_ids).order_by('id')
        items = items

        upcoming = request.GET.get('upcoming')
        if upcoming == 'true':
            now = timezone.now()
            items = items.filter(cohort__kickoff_date__gte=now)

        key = server_id()

        calendar = iCalendar()
        calendar.add(
            'prodid',
            f'-//BreatheCode//Student Schedule ({user_id}) {key}//EN')
        calendar.add('X-WR-CALNAME', f'Academy - Schedule')
        calendar.add('X-WR-CALDESC', '')
        calendar.add('REFRESH-INTERVAL;VALUE=DURATION', 'PT15M')

        url = os.getenv('API_URL')
        if url:
            url = re.sub(r'/$', '',
                         url) + '/v1/events/ical/student/' + str(user_id)
            calendar.add('url', url)

        calendar.add('version', '2.0')

        for item in items:
            event = iEvent()

            event.add('summary', item.cohort.name)
            event.add('uid', f'breathecode_cohort_time_slot_{item.id}_{key}')

            event.add('dtstart', item.starting_at)
            event.add('dtstamp', item.starting_at)

            until_date = item.cohort.ending_date

            if not until_date:
                until_date = timezone.make_aware(
                    datetime(year=2100,
                             month=12,
                             day=31,
                             hour=12,
                             minute=00,
                             second=00))

            if item.recurrent:
                event.add('rrule', {
                    'freq': item.recurrency_type,
                    'until': until_date
                })

            event.add('dtend', item.ending_at)

            teacher = CohortUser.objects.filter(
                role='TEACHER', cohort__id=item.cohort.id).first()

            if teacher:
                organizer = vCalAddress(f'MAILTO:{teacher.user.email}')

                if teacher.user.first_name and teacher.user.last_name:
                    organizer.params['cn'] = vText(
                        f'{teacher.user.first_name} '
                        f'{teacher.user.last_name}')
                elif teacher.user.first_name:
                    organizer.params['cn'] = vText(teacher.user.first_name)
                elif teacher.user.last_name:
                    organizer.params['cn'] = vText(teacher.user.last_name)

                organizer.params['role'] = vText('OWNER')
                event['organizer'] = organizer

            location = item.cohort.academy.name

            if item.cohort.academy.website_url:
                location = f'{location} ({item.cohort.academy.website_url})'
            event['location'] = vText(item.cohort.academy.name)

            calendar.add_component(event)

        calendar_text = calendar.to_ical()

        response = HttpResponse(calendar_text, content_type='text/calendar')
        response['Content-Disposition'] = 'attachment; filename="calendar.ics"'
        return response