Added counters in api

This commit is contained in:
Antoine Bartuccio 2016-08-05 20:01:23 +02:00
parent feb7b4689b
commit c2ccf63021
5 changed files with 52 additions and 2 deletions

View File

@ -5,6 +5,7 @@ from rest_framework import routers
# Router config
router = routers.DefaultRouter()
router.register(r'counter', CounterViewSet, base_name='api_counter')
urlpatterns = [

View File

@ -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)

View File

@ -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')

View File

@ -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

View File

@ -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!!!