2015-10-19 13:02:37 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-10-20 14:26:25 +00:00
|
|
|
"""
|
|
|
|
Views for the Duck Booking Tool API
|
|
|
|
"""
|
|
|
|
|
2015-10-21 13:04:53 +00:00
|
|
|
import re
|
|
|
|
|
2015-01-22 15:22:57 +00:00
|
|
|
from django.conf import settings
|
2015-10-19 13:02:37 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from rest_framework import viewsets
|
|
|
|
from rest_framework.decorators import detail_route, list_route
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2015-10-20 14:26:25 +00:00
|
|
|
from .serializers import DuckSerializer, CompetenceSerializer
|
2015-10-21 13:04:53 +00:00
|
|
|
from booking.models import Duck, Competence, Booking, Species, Location
|
2015-10-19 13:02:37 +00:00
|
|
|
|
|
|
|
class DuckViewSet(viewsets.ModelViewSet):
|
2015-10-20 14:26:25 +00:00
|
|
|
"""
|
|
|
|
View set for duck handling
|
|
|
|
"""
|
|
|
|
|
2015-10-19 13:02:37 +00:00
|
|
|
serializer_class = DuckSerializer
|
|
|
|
queryset = Duck.objects.all()
|
|
|
|
|
|
|
|
@detail_route(methods=['post'], permission_classes=[IsAuthenticated])
|
|
|
|
def book(self, request, pk=None):
|
2015-10-20 14:26:25 +00:00
|
|
|
"""
|
|
|
|
API call to book a duck
|
|
|
|
"""
|
|
|
|
|
2015-10-19 13:02:37 +00:00
|
|
|
duck = self.get_object()
|
|
|
|
competence = get_object_or_404(Competence, pk=request.data['competence'])
|
|
|
|
force = request.data.get('force', False)
|
|
|
|
|
|
|
|
# If the duck is already booked, return 'already-booked' as the
|
|
|
|
# result
|
|
|
|
if duck.booked_by() != None:
|
|
|
|
return Response({'status': 'already-booked'})
|
|
|
|
|
|
|
|
# Result 'fail' means a problem
|
|
|
|
result = 'fail'
|
|
|
|
comp_level = 0
|
2015-01-22 15:22:57 +00:00
|
|
|
|
2015-10-19 13:02:37 +00:00
|
|
|
# Check if the duck has the requested competence
|
2015-10-19 15:08:44 +00:00
|
|
|
dcomp_list = duck.competences.filter(comp=competence)
|
2015-01-22 15:22:57 +00:00
|
|
|
|
2015-10-19 13:02:37 +00:00
|
|
|
if len(dcomp_list) < 1:
|
|
|
|
comp_level = 0
|
|
|
|
else:
|
|
|
|
comp_level = dcomp_list[0].level()
|
2015-01-22 15:22:57 +00:00
|
|
|
|
2015-10-19 13:02:37 +00:00
|
|
|
# If the competence level is too low, set result to 'bad-comp'
|
|
|
|
if comp_level <= settings.COMP_WARN_LEVEL:
|
|
|
|
result = 'bad-comp'
|
2015-01-22 15:22:57 +00:00
|
|
|
|
2015-10-19 13:02:37 +00:00
|
|
|
# If the duck has high enough competence or the booking is
|
|
|
|
# forced, return status 'success'
|
|
|
|
if result != 'bad-comp' or force:
|
|
|
|
result = 'ok'
|
2015-01-22 15:22:57 +00:00
|
|
|
|
2015-10-19 13:02:37 +00:00
|
|
|
booking = Booking(duck=duck, user=request.user, comp_req=competence)
|
|
|
|
booking.save()
|
2015-01-22 15:22:57 +00:00
|
|
|
|
2015-10-19 13:02:37 +00:00
|
|
|
return Response({'status': result})
|
2015-01-22 15:22:57 +00:00
|
|
|
|
2015-10-19 13:02:37 +00:00
|
|
|
@list_route(methods=['post'], permission_classes=[IsAuthenticated])
|
|
|
|
def donate(self, request):
|
2015-10-20 14:26:25 +00:00
|
|
|
"""
|
|
|
|
API call to donate a new duck
|
|
|
|
"""
|
|
|
|
|
2015-10-21 13:04:53 +00:00
|
|
|
color = request.data.get('color')
|
|
|
|
species_id = request.data.get('species')
|
|
|
|
location_id = request.data.get('location')
|
|
|
|
|
|
|
|
if None in (color, species_id, location_id):
|
|
|
|
return Response({'status': 'incomplete-request'}, status=400)
|
|
|
|
|
|
|
|
try:
|
|
|
|
species = Species.objects.get(pk=species_id)
|
|
|
|
except Species.DoesNotExist:
|
|
|
|
return Response({'status': 'bad-species'}, status=404)
|
|
|
|
|
|
|
|
try:
|
|
|
|
location = Location.objects.get(pk=location_id)
|
|
|
|
except Location.DoesNotExist:
|
|
|
|
return Response({'status': 'bad-location'}, status=404)
|
|
|
|
|
|
|
|
if not re.match(r'^[0-9a-f]{6}$', color, flags=re.IGNORECASE):
|
|
|
|
return Response({'status': 'bad-color'}, status=400)
|
|
|
|
|
|
|
|
color = color.lower()
|
|
|
|
|
|
|
|
duck = Duck.objects.create(donated_by=request.user,
|
|
|
|
species=species,
|
|
|
|
location=location,
|
|
|
|
color=color)
|
|
|
|
|
|
|
|
return Response({'status': 'ok', 'id': duck.pk})
|
2015-10-19 15:08:44 +00:00
|
|
|
|
|
|
|
class CompetenceViewSet(viewsets.ModelViewSet):
|
2015-10-20 14:26:25 +00:00
|
|
|
"""
|
|
|
|
View set for competence handling
|
|
|
|
"""
|
|
|
|
|
2015-10-19 15:08:44 +00:00
|
|
|
serializer_class = CompetenceSerializer
|
|
|
|
queryset = Competence.objects.all()
|