2016-02-13 16:31:14 +01:00
|
|
|
# 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/>.
|
|
|
|
|
2016-02-13 17:52:32 +01:00
|
|
|
from django.db.models import F
|
2016-02-23 19:49:35 +01:00
|
|
|
from django.utils.formats import date_format
|
2016-02-23 20:58:25 +01:00
|
|
|
from django.utils.timezone import localtime, now
|
2016-02-13 16:31:14 +01:00
|
|
|
|
2016-06-18 11:59:26 +02:00
|
|
|
from rest_framework import permissions, serializers, status
|
2016-02-13 16:31:14 +01:00
|
|
|
from rest_framework.generics import GenericAPIView
|
|
|
|
from rest_framework.response import Response
|
2016-03-30 00:51:34 +02:00
|
|
|
from rest_framework.views import APIView
|
2016-02-13 16:31:14 +01:00
|
|
|
|
2016-02-13 16:52:04 +01:00
|
|
|
from ..agendas.models import Event, Booking
|
2016-02-13 16:31:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
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,
|
2016-02-23 19:49:35 +01:00
|
|
|
'text': date_format(localtime(x.start_datetime), format='DATETIME_FORMAT')}
|
2016-02-13 17:52:32 +01:00
|
|
|
for x in Event.objects.filter(agenda=pk).filter(
|
2016-02-23 20:58:25 +01:00
|
|
|
places__gt=F('booked_places'),
|
|
|
|
start_datetime__gte=min_datetime)
|
2016-02-13 17:14:45 +01:00
|
|
|
]}
|
2016-02-13 16:31:14 +01:00
|
|
|
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
|
2016-06-18 11:59:26 +02:00
|
|
|
permission_classes = (permissions.IsAuthenticated,)
|
2016-02-13 16:52:04 +01:00
|
|
|
|
|
|
|
def post(self, request, agenda_pk=None, event_pk=None, format=None):
|
2016-02-13 17:52:32 +01:00
|
|
|
event = Event.objects.filter(id=event_pk)[0]
|
2016-02-13 17:14:45 +01:00
|
|
|
if event.booked_places >= event.places:
|
|
|
|
return Response({'err': 1, 'reason': 'sold out'}, status.HTTP_400_BAD_REQUEST)
|
2016-02-13 16:52:04 +01:00
|
|
|
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()
|
2016-03-30 00:51:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BookingAPI(APIView):
|
2016-06-18 11:59:26 +02:00
|
|
|
permission_classes = (permissions.IsAuthenticated,)
|
|
|
|
|
2016-03-30 00:51:34 +02:00
|
|
|
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()
|