Commit 1a6373e9 authored by Skia's avatar Skia Committed by Sli
Browse files

Add barman list in API

parent b9ea687d
from . import serializers from django.shortcuts import get_object_or_404
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.decorators import api_view from rest_framework.decorators import api_view
from rest_framework import viewsets from rest_framework import viewsets
from rest_framework.decorators import detail_route from rest_framework.decorators import detail_route
from rest_framework.decorators import list_route from rest_framework.decorators import list_route
from django.shortcuts import get_object_or_404
from core.templatetags.renderer import markdown from core.templatetags.renderer import markdown
from counter.models import Counter from counter.models import Counter
from api.views import serializers
@api_view(['GET']) @api_view(['GET'])
...@@ -30,7 +30,7 @@ class CounterViewSet(viewsets.ModelViewSet): ...@@ -30,7 +30,7 @@ class CounterViewSet(viewsets.ModelViewSet):
@list_route() @list_route()
def bar(self, request): 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") self.queryset = Counter.objects.filter(type="BAR")
serializer = self.get_serializer(self.queryset, many=True) serializer = self.get_serializer(self.queryset, many=True)
......
...@@ -5,7 +5,11 @@ from counter.models import Counter ...@@ -5,7 +5,11 @@ from counter.models import Counter
class Counter(serializers.ModelSerializer): class Counter(serializers.ModelSerializer):
is_open = serializers.BooleanField(read_only=True) is_open = serializers.BooleanField(read_only=True)
barman_list = serializers.ListField(
child = serializers.IntegerField()
)
class Meta: class Meta:
model = Counter model = Counter
fields = ('id', 'name', 'is_open') fields = ('id', 'name', 'type', 'is_open', 'barman_list')
...@@ -164,14 +164,14 @@ class Counter(models.Model): ...@@ -164,14 +164,14 @@ class Counter(models.Model):
c = Counter.objects.filter(id=counter_id).first() 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() 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 Returns the barman list as list of User
Also handle the timeout of the barmen Also handle the timeout of the barmen
""" """
bl = [] bl = []
counter_id = int(counter_id) counter_id = self.id
if counter_id in list(Counter.barmen_session.keys()): if counter_id in list(Counter.barmen_session.keys()):
for b in Counter.barmen_session[counter_id]['users']: for b in Counter.barmen_session[counter_id]['users']:
# Reminder: user is stored as a tuple with its login time # Reminder: user is stored as a tuple with its login time
...@@ -185,16 +185,19 @@ class Counter(models.Model): ...@@ -185,16 +185,19 @@ class Counter(models.Model):
Counter.barmen_session[counter_id]['users'] = set() Counter.barmen_session[counter_id]['users'] = set()
return bl return bl
def get_random_barman(counter_id): def get_random_barman(self):
bl = Counter.get_barmen_list(counter_id) bl = self.get_barmen_list()
return bl[randrange(0, len(bl))] return bl[randrange(0, len(bl))]
def is_open(self): def is_open(self):
response = False response = False
if len(Counter.get_barmen_list(self.id)) > 0: if len(self.get_barmen_list()) > 0:
response = True response = True
return response return response
def barman_list(self):
return [b.id for b in self.get_barmen_list()]
class Refilling(models.Model): class Refilling(models.Model):
""" """
Handle the refilling Handle the refilling
......
...@@ -77,7 +77,7 @@ class CounterMain(DetailView, ProcessFormView, FormMixin): ...@@ -77,7 +77,7 @@ class CounterMain(DetailView, ProcessFormView, FormMixin):
kwargs['login_form'].fields['username'].widget.attrs['autofocus'] = True kwargs['login_form'].fields['username'].widget.attrs['autofocus'] = True
kwargs['form'] = self.get_form() kwargs['form'] = self.get_form()
if self.object.type == 'BAR': 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(): elif self.request.user.is_authenticated():
kwargs['barmen'] = [self.request.user] kwargs['barmen'] = [self.request.user]
if 'last_basket' in self.request.session.keys(): if 'last_basket' in self.request.session.keys():
...@@ -118,7 +118,7 @@ class CounterClick(DetailView): ...@@ -118,7 +118,7 @@ class CounterClick(DetailView):
ret = super(CounterClick, self).get(request, *args, **kwargs) ret = super(CounterClick, self).get(request, *args, **kwargs)
if ((self.object.type != "BAR" and not request.user.is_authenticated()) or if ((self.object.type != "BAR" and not request.user.is_authenticated()) or
(self.object.type == "BAR" and (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 ret = self.cancel(request) # Otherwise, go to main view
return ret return ret
...@@ -129,7 +129,7 @@ class CounterClick(DetailView): ...@@ -129,7 +129,7 @@ class CounterClick(DetailView):
self.refill_form = None self.refill_form = None
if ((self.object.type != "BAR" and not request.user.is_authenticated()) or if ((self.object.type != "BAR" and not request.user.is_authenticated()) or
(self.object.type == "BAR" and (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) return self.cancel(request)
if 'basket' not in request.session.keys(): if 'basket' not in request.session.keys():
request.session['basket'] = {} request.session['basket'] = {}
...@@ -140,7 +140,7 @@ class CounterClick(DetailView): ...@@ -140,7 +140,7 @@ class CounterClick(DetailView):
elif self.is_barman_price(): elif self.is_barman_price():
self.operator = self.customer.user self.operator = self.customer.user
else: else:
self.operator = Counter.get_random_barman(self.object.id) self.operator = self.object.get_random_barman()
if 'add_product' in request.POST['action']: if 'add_product' in request.POST['action']:
self.add_product(request) self.add_product(request)
...@@ -158,7 +158,7 @@ class CounterClick(DetailView): ...@@ -158,7 +158,7 @@ class CounterClick(DetailView):
return self.render_to_response(context) return self.render_to_response(context)
def is_barman_price(self): 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 return True
else: else:
return False return False
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment