chrono/chrono/api/views.py

76 lines
2.7 KiB
Python

# chrono - agendas system
# Copyright (C) 2016 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.db.models import F
from django.utils.formats import date_format
from django.utils.timezone import localtime, now
from rest_framework import serializers, status
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from ..agendas.models import Event, Booking
class Datetimes(GenericAPIView):
def get(self, request, pk=None, format=None):
min_datetime = now()
response = {'data': [{
'id': x.id,
'text': date_format(localtime(x.start_datetime), format='DATETIME_FORMAT')}
for x in Event.objects.filter(agenda=pk).filter(
places__gt=F('booked_places'),
start_datetime__gte=min_datetime)
]}
return Response(response)
datetimes = Datetimes.as_view()
class SlotSerializer(serializers.Serializer):
pass
class Fillslot(GenericAPIView):
serializer_class = SlotSerializer
def post(self, request, agenda_pk=None, event_pk=None, format=None):
event = Event.objects.filter(id=event_pk)[0]
if event.booked_places >= event.places:
return Response({'err': 1, 'reason': 'sold out'}, status.HTTP_400_BAD_REQUEST)
booking = Booking(event_id=event_pk, extra_data=request.data)
booking.save()
response = {'err': 0, 'booking_id': booking.id}
return Response(response)
fillslot = Fillslot.as_view()
class BookingAPI(APIView):
def initial(self, request, *args, **kwargs):
super(BookingAPI, self).initial(request, *args, **kwargs)
self.booking = Booking.objects.get(id=kwargs.get('booking_pk'),
cancellation_datetime__isnull=True)
def delete(self, request, *args, **kwargs):
self.booking.cancellation_datetime = now()
self.booking.save()
response = {'err': 0, 'booking_id': self.booking.id}
return Response(response)
booking = BookingAPI.as_view()