add booking API
This commit is contained in:
parent
2e25f21dd0
commit
16ed50b9d4
|
@ -0,0 +1,26 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
import jsonfield.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('agendas', '0002_event'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Booking',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||
('extra_data', jsonfield.fields.JSONField(null=True)),
|
||||
('event', models.ForeignKey(to='agendas.Event')),
|
||||
],
|
||||
options={
|
||||
},
|
||||
bases=(models.Model,),
|
||||
),
|
||||
]
|
|
@ -19,6 +19,8 @@ from django.db import models
|
|||
from django.utils.text import slugify
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from jsonfield import JSONField
|
||||
|
||||
|
||||
class Agenda(models.Model):
|
||||
label = models.CharField(_('Label'), max_length=50)
|
||||
|
@ -53,3 +55,8 @@ class Event(models.Model):
|
|||
|
||||
class Meta:
|
||||
ordering = ['agenda', 'start_datetime']
|
||||
|
||||
|
||||
class Booking(models.Model):
|
||||
event = models.ForeignKey(Event)
|
||||
extra_data = JSONField(null=True)
|
||||
|
|
|
@ -20,4 +20,5 @@ from . import views
|
|||
|
||||
urlpatterns = patterns('',
|
||||
url(r'agenda/(?P<pk>\w+)/datetimes/', views.datetimes),
|
||||
url(r'agenda/(?P<agenda_pk>\w+)/fillslot/(?P<event_pk>\w+)/', views.fillslot),
|
||||
)
|
||||
|
|
|
@ -16,10 +16,11 @@
|
|||
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from rest_framework import serializers
|
||||
from rest_framework.generics import GenericAPIView
|
||||
from rest_framework.response import Response
|
||||
|
||||
from ..agendas.models import Event
|
||||
from ..agendas.models import Event, Booking
|
||||
|
||||
|
||||
class Datetimes(GenericAPIView):
|
||||
|
@ -30,3 +31,19 @@ class Datetimes(GenericAPIView):
|
|||
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):
|
||||
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()
|
||||
|
|
1
setup.py
1
setup.py
|
@ -105,6 +105,7 @@ setup(
|
|||
install_requires=['django>=1.7, <1.8',
|
||||
'gadjo',
|
||||
'djangorestframework>=3.1',
|
||||
'django-jsonfield >= 0.9.3',
|
||||
],
|
||||
zip_safe=False,
|
||||
cmdclass={
|
||||
|
|
|
@ -4,7 +4,7 @@ from webtest import TestApp
|
|||
|
||||
from chrono.wsgi import application
|
||||
|
||||
from chrono.agendas.models import Agenda, Event
|
||||
from chrono.agendas.models import Agenda, Event, Booking
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
|
@ -34,3 +34,19 @@ def test_datetimes_api(some_data):
|
|||
resp = app.get('/api/agenda/1/datetimes/')
|
||||
assert 'data' in resp.json
|
||||
assert len(resp.json['data']) == 3
|
||||
|
||||
|
||||
def test_booking_api(some_data):
|
||||
app = TestApp(application)
|
||||
event = Event.objects.filter(agenda_id=1)[0]
|
||||
resp = app.post('/api/agenda/1/fillslot/%s/' % event.id)
|
||||
Booking.objects.get(id=resp.json['booking_id'])
|
||||
assert Booking.objects.count() == 1
|
||||
|
||||
def test_booking_api_with_data(some_data):
|
||||
app = TestApp(application)
|
||||
event = Event.objects.filter(agenda_id=1)[0]
|
||||
resp = app.post_json('/api/agenda/1/fillslot/%s/' % event.id,
|
||||
params={'hello': 'world'})
|
||||
assert Booking.objects.count() == 1
|
||||
assert Booking.objects.all()[0].extra_data == {'hello': 'world'}
|
||||
|
|
Loading…
Reference in New Issue