Пример #1
0
 def get(self, request, uuid):
     offerings = models.Offering.objects.all().filter_for_user(request.user)
     offering = get_object_or_404(offerings, uuid=uuid)
     bookings = get_offering_bookings(offering)
     serializer = serializers.BookingSerializer(
         instance=bookings, many=True, context={'request': request})
     return Response(serializer.data, status=status.HTTP_200_OK)
Пример #2
0
    def validate_order_item(self, request):
        schedules = self.order_item.attributes.get('schedules')

        # We check that the schedule is set.
        if not schedules:
            raise ValidationError(_('Schedules are required.'))

        if not len(schedules):
            raise ValidationError(_('Schedules are required.'))

        for period in schedules:
            try:
                start = period['start']
                end = period['end']

                if not start or not end:
                    raise ValidationError(
                        _('Value \'start\' or \'end\' does not exist in schedules item.'
                          ))
            except KeyError:
                raise ValidationError(
                    _('Key \'start\' or \'end\' does not exist in schedules item.'
                      ))

            for value in [start, end]:
                match = datetime_re.match(value)
                kw = match.groupdict()
                if list(
                        filter(
                            lambda x: not kw[x],
                            [
                                'hour', 'month', 'second', 'year', 'tzinfo',
                                'day', 'minute'
                            ],
                        )):
                    raise ValidationError(
                        _('The value %s does not match the format.') % value)

        # Check that the schedule is available for the offering.
        offering = self.order_item.offering
        offering_schedules = offering.attributes.get('schedules', [])

        for period in schedules:
            if not is_interval_in_schedules(
                    TimePeriod(period['start'], period['end']),
                [TimePeriod(i['start'], i['end']) for i in offering_schedules],
            ):
                raise ValidationError(
                    _('Time period from %s to %s is not available for selected offering.'
                      ) % (period['start'], period['end']))

        # Check that there are no other bookings.
        bookings = get_offering_bookings(offering)
        for period in schedules:
            if is_interval_in_schedules(
                    TimePeriod(period['start'], period['end']), bookings):
                raise ValidationError(
                    _('Time period from %s to %s is not available.') %
                    (period['start'], period['end']))
Пример #3
0
    def get_bookings(self):
        def get_date(event_date):
            date = event_date.get('date', None)
            if not date:
                return event_date.get('dateTime', None)

            return date

        bookings = get_offering_bookings(self.offering)
        utc = pytz.UTC
        now = utc.localize(timezone.datetime.now())
        reg_exp = re.compile(r'[^a-z0-9]')
        waldur_bookings = [
            TimePeriod(b.start, b.end, re.sub(reg_exp, '', b.id))
            for b in bookings
            if b.start > now
        ]
        google_bookings = []

        for event in self.backend.get_events(calendar_id=self.calendar_id):
            start = event.get('start')
            if start:
                start = get_date(start)
            else:
                continue

            end = event.get('end')
            if end:
                end = get_date(end)
            else:
                continue

            google_bookings.append(TimePeriod(start, end, event['id']))

        need_to_delete = {b.id for b in google_bookings} - {
            b.id for b in waldur_bookings
        }
        need_to_update = []
        need_to_add = []

        for booking in waldur_bookings:
            google_booking = list(filter(lambda x: x.id == booking.id, google_bookings))
            if len(google_booking):
                google_booking = google_booking[0]
                if (
                    booking.start != google_booking.start
                    or booking.end != google_booking.end
                ):
                    need_to_update.append(booking)
            else:
                need_to_add.append(booking)

        return need_to_add, need_to_delete, need_to_update
Пример #4
0
    def get_bookings(self):
        def get_date(event_date):
            date = event_date.get('date', None)
            if not date:
                return event_date.get('dateTime', None)

            return date

        waldur_bookings = get_offering_bookings(self.offering)
        google_bookings = []

        for event in self.backend.get_events(calendar_id=self.calendar_id):
            start = event.get('start')
            if start:
                start = get_date(start)
            else:
                continue

            end = event.get('end')
            if end:
                end = get_date(end)
            else:
                continue

            attendees = []
            if event.get('attendees'):
                for attendee in event.get('attendees'):
                    attendees.append(
                        {
                            'displayName': attendee['displayName'],
                            'email': attendee['email'],
                        }
                    )

            google_bookings.append(
                TimePeriod(
                    start,
                    end,
                    event['id'],
                    location=event.get('location'),
                    attendees=attendees,
                )
            )

        need_to_delete = {b.id for b in google_bookings} - {
            b.id for b in waldur_bookings
        }
        need_to_update = []
        need_to_add = []

        for booking in waldur_bookings:
            google_booking = list(filter(lambda x: x.id == booking.id, google_bookings))
            if len(google_booking):
                google_booking = google_booking[0]
                if (
                    booking.start != google_booking.start
                    or booking.end != google_booking.end
                    or booking.location != google_booking.location
                    or booking.attendees != google_booking.attendees
                ):
                    need_to_update.append(booking)
            else:
                need_to_add.append(booking)

        return need_to_add, need_to_delete, need_to_update