api: add POST endpoint to cancel a booking (#11472)

This commit is contained in:
Frédéric Péters 2016-06-23 19:31:12 +02:00
parent 989d50f8df
commit c3ef0d7441
4 changed files with 31 additions and 3 deletions

View File

@ -19,7 +19,7 @@ from django.db import models
from django.db.models.expressions import RawSQL
from django.utils.formats import date_format
from django.utils.text import slugify
from django.utils.timezone import localtime
from django.utils.timezone import localtime, now
from django.utils.translation import ugettext_lazy as _
from jsonfield import JSONField
@ -81,3 +81,7 @@ class Booking(models.Model):
event = models.ForeignKey(Event)
extra_data = JSONField(null=True)
cancellation_datetime = models.DateTimeField(null=True)
def cancel(self):
self.cancellation_datetime = now()
self.save()

View File

@ -24,4 +24,5 @@ urlpatterns = patterns('',
url(r'agenda/(?P<agenda_pk>\w+)/fillslot/(?P<event_pk>\w+)/$', views.fillslot),
url(r'agenda/(?P<agenda_pk>\w+)/status/(?P<event_pk>\w+)/$', views.slot_status),
url(r'booking/(?P<booking_pk>\w+)/$', views.booking),
url(r'booking/(?P<booking_pk>\w+)/cancel/$', views.cancel_booking),
)

View File

@ -86,14 +86,25 @@ class BookingAPI(APIView):
cancellation_datetime__isnull=True)
def delete(self, request, *args, **kwargs):
self.booking.cancellation_datetime = now()
self.booking.save()
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 SlotStatus(GenericAPIView):
serializer_class = SlotSerializer
permission_classes = (permissions.IsAuthenticated,)

View File

@ -114,6 +114,18 @@ def test_booking_cancellation_api(app, some_data, user):
resp = app.delete('/api/booking/%s/' % booking_id)
assert Booking.objects.filter(cancellation_datetime__isnull=False).count() == 1
def test_booking_cancellation_post_api(app, some_data, user):
agenda_id = Agenda.objects.filter(label=u'Foo bar')[0].id
event = Event.objects.filter(agenda_id=agenda_id)[0]
resp = app.post('/api/agenda/%s/fillslot/%s/' % (agenda_id, event.id), status=403)
app.authorization = ('Basic', ('john.doe', 'password'))
resp = app.post('/api/agenda/%s/fillslot/%s/' % (agenda_id, event.id))
booking_id = resp.json['booking_id']
assert Booking.objects.count() == 1
resp = app.post('/api/booking/%s/cancel/' % booking_id)
assert Booking.objects.filter(cancellation_datetime__isnull=False).count() == 1
def test_soldout(app, some_data, user):
agenda_id = Agenda.objects.filter(label=u'Foo bar')[0].id
event = Event.objects.filter(agenda_id=agenda_id).exclude(start_datetime__lt=now())[0]