From c2ccf6302180f30730bcef3d4aa8b8b17bf8cbe6 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Fri, 5 Aug 2016 20:01:23 +0200 Subject: [PATCH] Added counters in api --- api/urls.py | 3 ++- api/views/api.py | 33 +++++++++++++++++++++++++++++++++ api/views/serializers.py | 10 ++++++++++ counter/models.py | 6 ++++++ sith/urls.py | 2 +- 5 files changed, 52 insertions(+), 2 deletions(-) diff --git a/api/urls.py b/api/urls.py index 353775fa..31673927 100644 --- a/api/urls.py +++ b/api/urls.py @@ -5,6 +5,7 @@ from rest_framework import routers # Router config router = routers.DefaultRouter() +router.register(r'counter', CounterViewSet, base_name='api_counter') urlpatterns = [ @@ -13,4 +14,4 @@ urlpatterns = [ url(r'^login/', include('rest_framework.urls', namespace='rest_framework')), url(r'^markdown$', RenderMarkdown, name='api_markdown'), -] \ No newline at end of file +] diff --git a/api/views/api.py b/api/views/api.py index 8e99c3b3..5bb7a938 100644 --- a/api/views/api.py +++ b/api/views/api.py @@ -1,7 +1,13 @@ +from . import serializers from rest_framework.response import Response from rest_framework.decorators import api_view +from rest_framework import viewsets +from rest_framework.decorators import detail_route +from rest_framework.decorators import list_route +from django.shortcuts import get_object_or_404 from core.templatetags.renderer import markdown +from counter.models import Counter @api_view(['GET']) @@ -11,3 +17,30 @@ def RenderMarkdown(request): """ if request.method == 'GET': return Response(markdown(request.GET['text'])) + + +class CounterViewSet(viewsets.ModelViewSet): + """ + Manage Counters (api/v1/counter) + """ + + serializer_class = serializers.Counter + queryset = Counter.objects.all() + + @list_route() + def bar(self, request): + """ + Return all counters (api/v1/counter/all) + """ + self.queryset = Counter.objects.filter(type="BAR") + serializer = self.get_serializer(self.queryset, many=True) + return Response(serializer.data) + + @detail_route(methods=['GET']) + def id(self, request, pk=None): + """ + Get by id (api/v1/{nk}/id) + """ + self.queryset = get_object_or_404(Counter.objects.filter(id=pk)) + serializer = self.get_serializer(self.queryset) + return Response(serializer.data) diff --git a/api/views/serializers.py b/api/views/serializers.py index 236cd38b..599dd3e4 100644 --- a/api/views/serializers.py +++ b/api/views/serializers.py @@ -1 +1,11 @@ from rest_framework import serializers +from counter.models import Counter + + +class Counter(serializers.ModelSerializer): + + is_open = serializers.BooleanField(read_only=True) + + class Meta: + model = Counter + fields = ('id', 'name', 'is_open') diff --git a/counter/models.py b/counter/models.py index e705e918..600d05ee 100644 --- a/counter/models.py +++ b/counter/models.py @@ -189,6 +189,12 @@ class Counter(models.Model): bl = Counter.get_barmen_list(counter_id) return bl[randrange(0, len(bl))] + def is_open(self): + response = False + if len(Counter.get_barmen_list(self.id)) > 0: + response = True + return response + class Refilling(models.Model): """ Handle the refilling diff --git a/sith/urls.py b/sith/urls.py index 445115b6..019850f2 100644 --- a/sith/urls.py +++ b/sith/urls.py @@ -29,6 +29,6 @@ urlpatterns = [ url(r'^accounting/', include('accounting.urls', namespace="accounting", app_name="accounting")), url(r'^eboutic/', include('eboutic.urls', namespace="eboutic", app_name="eboutic")), url(r'^launderette/', include('launderette.urls', namespace="launderette", app_name="launderette")), - url(r'^api/', include('api.urls', namespace="api", app_name="api")), + url(r'^api/v1/', include('api.urls', namespace="api", app_name="api")), url(r'^admin/', include(admin.site.urls)), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # TODO: remove me for production!!!