171 lines
5.8 KiB
Python
171 lines
5.8 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.core.urlresolvers import reverse
|
|
from django.shortcuts import get_object_or_404
|
|
from django.utils.timezone import now
|
|
|
|
from rest_framework import permissions, serializers
|
|
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):
|
|
min_datetime = now()
|
|
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()
|
|
|
|
|
|
class SlotSerializer(serializers.Serializer):
|
|
pass
|
|
|
|
|
|
class Fillslot(GenericAPIView):
|
|
serializer_class = SlotSerializer
|
|
permission_classes = (permissions.IsAuthenticated,)
|
|
|
|
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,
|
|
}
|
|
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):
|
|
'''
|
|
Cancel a booking.
|
|
|
|
It will return an error (code 1) if the booking was already cancelled.
|
|
'''
|
|
permission_classes = (permissions.IsAuthenticated,)
|
|
|
|
def post(self, request, booking_pk=None, format=None):
|
|
booking = get_object_or_404(Booking, id=booking_pk)
|
|
if booking.cancellation_datetime:
|
|
response = {'err': 1, 'reason': 'already cancelled'}
|
|
return Response(response)
|
|
booking.cancel()
|
|
response = {'err': 0, 'booking_id': booking.id}
|
|
return Response(response)
|
|
|
|
cancel_booking = CancelBooking.as_view()
|
|
|
|
|
|
class AcceptBooking(APIView):
|
|
'''
|
|
Accept a booking currently in the waiting list.
|
|
|
|
It will return error codes if the booking was cancelled before (code 1) and
|
|
if the booking was not in waiting list (code 2).
|
|
'''
|
|
permission_classes = (permissions.IsAuthenticated,)
|
|
|
|
def post(self, request, booking_pk=None, format=None):
|
|
booking = get_object_or_404(Booking, id=booking_pk)
|
|
if booking.cancellation_datetime:
|
|
response = {'err': 1, 'reason': 'booking is cancelled'}
|
|
return Response(response)
|
|
if not booking.in_waiting_list:
|
|
response = {'err': 2, 'reason': 'booking is not in waiting list'}
|
|
return Response(response)
|
|
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()
|