Beispiel #1
0
def _import_events(events, user):
    event_remap = {}

    for event in events:
        serializer = EventSerializer(data=event)

        if serializer.is_valid():
            instance = serializer.save(user=user)
            event_remap[event['id']] = instance.pk
        else:
            raise ValidationError({'events': {event['id']: serializer.errors}})

    logger.info(f"Imported {len(events)} events.")

    return event_remap
Beispiel #2
0
    def get(self, request, *args, **kwargs):
        user = self.request.user
        course = Course.objects.get(pk=self.kwargs['course'])
        course_schedules = CourseSchedule.objects.for_user(user.pk).for_course(
            course.pk)

        events = coursescheduleservice.course_schedules_to_events(
            course, course_schedules)

        serializer = EventSerializer(events, many=True)

        return Response(serializer.data)
def _create_events_from_calendar(external_calendar, calendar):
    events = []

    time_zone = pytz.timezone(external_calendar.get_user().settings.time_zone)

    for component in calendar.walk():
        if component.name == "VTIMEZONE":
            time_zone = pytz.timezone(component.get("TZID"))
        elif component.name == "VEVENT":
            start = component.get("DTSTART").dt
            if component.get("DTEND") is not None:
                end = component.get("DTEND").dt
            elif component.get("DURATION") is not None:
                end = start + component.get("DURATION").dt
            else:
                end = datetime.datetime.combine(start, datetime.time.max)
            all_day = not isinstance(start, datetime.datetime)
            show_end_time = isinstance(start, datetime.datetime)

            if all_day:
                start = datetime.datetime.combine(start, datetime.time.min)
            if timezone.is_naive(start):
                start = timezone.make_aware(start, time_zone)
            start = start.astimezone(pytz.utc)

            if all_day:
                end = datetime.datetime.combine(end, datetime.time.min)
            if timezone.is_naive(end):
                end = timezone.make_aware(end, time_zone)
            end = end.astimezone(pytz.utc)

            event = Event(id=len(events),
                          title=component.get("SUMMARY"),
                          all_day=all_day,
                          show_end_time=show_end_time,
                          start=start,
                          end=end,
                          url=component.get("URL"),
                          comments=component.get("DESCRIPTION"),
                          user=external_calendar.get_user(),
                          calendar_item_type=enums.EXTERNAL)

            events.append(event)

    serializer = EventSerializer(events, many=True)
    events_json = json.dumps(serializer.data)
    if len(events_json.encode('utf-8')) <= settings.FEED_MAX_CACHEABLE_SIZE:
        cache.set(_get_cache_prefix(external_calendar), events_json,
                  settings.FEED_CACHE_TTL)

    return events
Beispiel #4
0
def _create_events_from_course_schedules(course, course_schedules):
    events = []
    cache_prefix = _get_cache_prefix(course)

    day = course.start_date
    while day <= course.end_date:
        for course_schedule in course_schedules.iterator():
            if course_schedule.days_of_week[enums.PYTHON_TO_HELIUM_DAY_OF_WEEK[
                    day.weekday()]] == "1":
                start_time = _get_start_time_for_weekday(
                    course_schedule,
                    enums.PYTHON_TO_HELIUM_DAY_OF_WEEK[day.weekday()])
                end_time = _get_end_time_for_weekday(
                    course_schedule,
                    enums.PYTHON_TO_HELIUM_DAY_OF_WEEK[day.weekday()])
                start = make_aware(
                    datetime.datetime.combine(day, start_time),
                    pytz.timezone(
                        course.get_user().settings.time_zone)).astimezone(
                            pytz.utc)
                end = make_aware(
                    datetime.datetime.combine(day, end_time),
                    pytz.timezone(
                        course.get_user().settings.time_zone)).astimezone(
                            pytz.utc)
                comments = _get_comments(course)

                unique_str = str(course.get_user().pk) + str(
                    course_schedule.pk) + start.isoformat() + end.isoformat()
                event = Event(id=abs(hash(unique_str)) % (10**8),
                              title=course.title,
                              all_day=False,
                              show_end_time=True,
                              start=start,
                              end=end,
                              owner_id=course.pk,
                              user=course.get_user(),
                              calendar_item_type=enums.COURSE,
                              comments=comments)
                events.append(event)

                serializer = EventSerializer(event)
                cache.set(cache_prefix + str(event.id),
                          json.dumps(serializer.data))

                break

        day += datetime.timedelta(days=1)

    return events
Beispiel #5
0
class ExportSerializer(serializers.Serializer):
    external_calendars = ExternalCalendarSerializer(many=True)

    course_groups = CourseGroupSerializer(many=True)

    courses = CourseSerializer(many=True)

    course_schedules = CourseScheduleSerializer(many=True)

    categories = CategorySerializer(many=True)

    material_groups = MaterialGroupSerializer(many=True)

    materials = MaterialSerializer(many=True)

    events = EventSerializer(many=True)

    homework = HomeworkSerializer(many=True)

    reminders = ReminderSerializer(many=True)