Add barman list in API

This commit is contained in:
Skia 2016-08-06 12:37:36 +02:00 committed by klmp200
parent b9ea687df1
commit 1a6373e9ce
4 changed files with 21 additions and 14 deletions

View File

@ -1,13 +1,13 @@
from . import serializers
from django.shortcuts import get_object_or_404
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
from api.views import serializers
@api_view(['GET'])
@ -30,7 +30,7 @@ class CounterViewSet(viewsets.ModelViewSet):
@list_route()
def bar(self, request):
"""
Return all counters (api/v1/counter/bar)
Return all bars (api/v1/counter/bar)
"""
self.queryset = Counter.objects.filter(type="BAR")
serializer = self.get_serializer(self.queryset, many=True)

View File

@ -5,7 +5,11 @@ from counter.models import Counter
class Counter(serializers.ModelSerializer):
is_open = serializers.BooleanField(read_only=True)
barman_list = serializers.ListField(
child = serializers.IntegerField()
)
class Meta:
model = Counter
fields = ('id', 'name', 'is_open')
fields = ('id', 'name', 'type', 'is_open', 'barman_list')

View File

@ -164,14 +164,14 @@ class Counter(models.Model):
c = Counter.objects.filter(id=counter_id).first()
Permanency(user=u, counter=c, start=user_tuple[1], end=Counter.barmen_session[counter_id]['time']).save()
def get_barmen_list(counter_id):
def get_barmen_list(self):
"""
Returns the barman list as list of User
Also handle the timeout of the barmen
"""
bl = []
counter_id = int(counter_id)
counter_id = self.id
if counter_id in list(Counter.barmen_session.keys()):
for b in Counter.barmen_session[counter_id]['users']:
# Reminder: user is stored as a tuple with its login time
@ -185,16 +185,19 @@ class Counter(models.Model):
Counter.barmen_session[counter_id]['users'] = set()
return bl
def get_random_barman(counter_id):
bl = Counter.get_barmen_list(counter_id)
def get_random_barman(self):
bl = self.get_barmen_list()
return bl[randrange(0, len(bl))]
def is_open(self):
response = False
if len(Counter.get_barmen_list(self.id)) > 0:
if len(self.get_barmen_list()) > 0:
response = True
return response
def barman_list(self):
return [b.id for b in self.get_barmen_list()]
class Refilling(models.Model):
"""
Handle the refilling

View File

@ -77,7 +77,7 @@ class CounterMain(DetailView, ProcessFormView, FormMixin):
kwargs['login_form'].fields['username'].widget.attrs['autofocus'] = True
kwargs['form'] = self.get_form()
if self.object.type == 'BAR':
kwargs['barmen'] = Counter.get_barmen_list(self.object.id)
kwargs['barmen'] = self.object.get_barmen_list()
elif self.request.user.is_authenticated():
kwargs['barmen'] = [self.request.user]
if 'last_basket' in self.request.session.keys():
@ -118,7 +118,7 @@ class CounterClick(DetailView):
ret = super(CounterClick, self).get(request, *args, **kwargs)
if ((self.object.type != "BAR" and not request.user.is_authenticated()) or
(self.object.type == "BAR" and
len(Counter.get_barmen_list(self.object.id)) < 1)): # Check that at least one barman is logged in
len(self.object.get_barmen_list()) < 1)): # Check that at least one barman is logged in
ret = self.cancel(request) # Otherwise, go to main view
return ret
@ -129,7 +129,7 @@ class CounterClick(DetailView):
self.refill_form = None
if ((self.object.type != "BAR" and not request.user.is_authenticated()) or
(self.object.type == "BAR" and
len(Counter.get_barmen_list(self.object.id)) < 1)): # Check that at least one barman is logged in
len(self.object.get_barmen_list()) < 1)): # Check that at least one barman is logged in
return self.cancel(request)
if 'basket' not in request.session.keys():
request.session['basket'] = {}
@ -140,7 +140,7 @@ class CounterClick(DetailView):
elif self.is_barman_price():
self.operator = self.customer.user
else:
self.operator = Counter.get_random_barman(self.object.id)
self.operator = self.object.get_random_barman()
if 'add_product' in request.POST['action']:
self.add_product(request)
@ -158,7 +158,7 @@ class CounterClick(DetailView):
return self.render_to_response(context)
def is_barman_price(self):
if self.object.type == "BAR" and self.customer.user.id in [s.id for s in Counter.get_barmen_list(self.object.id)]:
if self.object.type == "BAR" and self.customer.user.id in [s.id for s in self.object.get_barmen_list()]:
return True
else:
return False