chrono/chrono/api/views.py

152 lines
5.1 KiB
Python
Raw Normal View History

# 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.core.urlresolvers import reverse
2016-06-20 14:48:50 +02:00
from django.utils.timezone import now
from rest_framework import permissions, serializers, status
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from ..agendas.models import Agenda, Event, Booking
class Agendas(GenericAPIView):
def get(self, request, pk=None, format=None):
response = {'data': [{
'id': x.id,
'slug': x.slug,
'api': {
'datetimes': request.build_absolute_uri(
reverse('api-agenda-datetimes', kwargs={'pk': x.id})),
},
'text': x.label}
for x in Agenda.objects.all().order_by('label')]}
return Response(response)
agendas = Agendas.as_view()
class Datetimes(GenericAPIView):
def get(self, request, pk=None, format=None):
2016-02-23 20:58:25 +01:00
min_datetime = now()
2016-02-13 17:14:45 +01:00
response = {'data': [{
'id': x.id,
'text': unicode(x)}
for x in Event.objects.filter(agenda=pk).filter(
start_datetime__gte=min_datetime,
full=False)]}
return Response(response)
datetimes = Datetimes.as_view()
2016-02-13 16:52:04 +01:00
class SlotSerializer(serializers.Serializer):
pass
class Fillslot(GenericAPIView):
serializer_class = SlotSerializer
permission_classes = (permissions.IsAuthenticated,)
2016-02-13 16:52:04 +01:00
def post(self, request, agenda_pk=None, event_pk=None, format=None):
event = Event.objects.filter(id=event_pk)[0]
new_booking = Booking(event_id=event_pk, extra_data=request.data)
if event.waiting_list_places:
if event.waiting_list >= event.waiting_list_places:
return Response({'err': 1, 'reason': 'sold out'})
if event.booked_places >= event.places or event.waiting_list:
# if this is full or there are people waiting, put new bookings
# in the waiting list.
new_booking.in_waiting_list = True
else:
if event.booked_places >= event.places:
return Response({'err': 1, 'reason': 'sold out'})
new_booking.save()
response = {
'err': 0,
'in_waiting_list': new_booking.in_waiting_list,
'booking_id': new_booking.id,
}
2016-02-13 16:52:04 +01:00
return Response(response)
fillslot = Fillslot.as_view()
class BookingAPI(APIView):
permission_classes = (permissions.IsAuthenticated,)
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.cancel()
response = {'err': 0, 'booking_id': self.booking.id}
return Response(response)
booking = BookingAPI.as_view()
class CancelBooking(APIView):
permission_classes = (permissions.IsAuthenticated,)
def post(self, request, booking_pk=None, format=None):
booking = Booking.objects.get(id=booking_pk, cancellation_datetime__isnull=True)
booking.cancel()
response = {'err': 0, 'booking_id': booking.id}
return Response(response)
cancel_booking = CancelBooking.as_view()
class AcceptBooking(APIView):
permission_classes = (permissions.IsAuthenticated,)
def post(self, request, booking_pk=None, format=None):
booking = Booking.objects.get(id=booking_pk,
cancellation_datetime__isnull=True,
in_waiting_list=True)
booking.accept()
response = {'err': 0, 'booking_id': booking.id}
return Response(response)
accept_booking = AcceptBooking.as_view()
class SlotStatus(GenericAPIView):
serializer_class = SlotSerializer
permission_classes = (permissions.IsAuthenticated,)
def get(self, request, agenda_pk=None, event_pk=None, format=None):
event = Event.objects.filter(id=event_pk)[0]
response = {
'err': 0,
'places': {
'total': event.places,
'reserved': event.booked_places,
'available': event.places - event.booked_places,
}
}
return Response(response)
slot_status = SlotStatus.as_view()