Commit 9a135ade authored by Skia's avatar Skia
Browse files

Refactor counters view a bit, and protect ClickView if no barman is logged

parent 6eb0ec16
Pipeline #17 passed with stage
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from django.conf import settings from django.conf import settings
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from datetime import timedelta
from club.models import Club from club.models import Club
from accounting.models import Product from accounting.models import Product
from core.models import Group from core.models import Group
from subscription.models import Subscriber
class Counter(models.Model): class Counter(models.Model):
name = models.CharField(_('name'), max_length=30) name = models.CharField(_('name'), max_length=30)
...@@ -31,3 +35,16 @@ class Counter(models.Model): ...@@ -31,3 +35,16 @@ class Counter(models.Model):
def can_be_viewed_by(self, user): def can_be_viewed_by(self, user):
return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP)
def get_barmen_list(counter_id):
bl = []
counter_id = str(counter_id)
if counter_id in list(Counter.barmen_session.keys()):
if (timezone.now() - Counter.barmen_session[counter_id]['time']) < timedelta(minutes=settings.SITH_BARMAN_TIMEOUT):
for b in Counter.barmen_session[counter_id]['users']:
bl.append(Subscriber.objects.filter(id=b).first())
Counter.barmen_session[counter_id]['time'] = timezone.now()
else:
Counter.barmen_session[counter_id]['users'] = set()
return bl
...@@ -7,10 +7,8 @@ from django.core.urlresolvers import reverse_lazy ...@@ -7,10 +7,8 @@ from django.core.urlresolvers import reverse_lazy
from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth.forms import AuthenticationForm
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.utils import timezone from django.utils import timezone
from django.conf import settings
from django import forms from django import forms
from datetime import timedelta
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
from subscription.models import Subscriber from subscription.models import Subscriber
...@@ -62,16 +60,7 @@ class CounterMain(DetailView, ProcessFormView, FormMixin): ...@@ -62,16 +60,7 @@ class CounterMain(DetailView, ProcessFormView, FormMixin):
# TODO: make some checks on the counter type, in order not to make the AuthenticationForm if there is no need to # TODO: make some checks on the counter type, in order not to make the AuthenticationForm if there is no need to
kwargs['login_form'] = AuthenticationForm() kwargs['login_form'] = AuthenticationForm()
kwargs['form'] = self.get_form() kwargs['form'] = self.get_form()
if str(self.object.id) in list(Counter.barmen_session.keys()): kwargs['barmen'] = Counter.get_barmen_list(self.object.id)
if (timezone.now() - Counter.barmen_session[str(self.object.id)]['time']) < timedelta(minutes=settings.SITH_BARMAN_TIMEOUT):
kwargs['barmen'] = []
for b in Counter.barmen_session[str(self.object.id)]['users']:
kwargs['barmen'].append(Subscriber.objects.filter(id=b).first())
Counter.barmen_session[str(self.object.id)]['time'] = timezone.now()
else:
Counter.barmen_session[str(self.object.id)]['users'] = set()
else:
kwargs['barmen'] = []
return kwargs return kwargs
def form_valid(self, form): def form_valid(self, form):
...@@ -114,14 +103,19 @@ class CounterClick(DetailView): ...@@ -114,14 +103,19 @@ class CounterClick(DetailView):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
"""Simple get view""" """Simple get view"""
self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first() self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first()
if 'basket' not in request.session.keys(): ret = super(CounterClick, self).get(request, *args, **kwargs)
if len(Counter.get_barmen_list(self.object.id)) < 1: # Check that at least one barman is logged in
return self.cancel(request) # Otherwise, go to main view
if 'basket' not in request.session.keys(): # Init the basket session entry
request.session['basket'] = {} request.session['basket'] = {}
return super(CounterClick, self).get(request, *args, **kwargs) return ret
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
""" Handle the many possibilities of the post request """ """ Handle the many possibilities of the post request """
self.object = self.get_object() self.object = self.get_object()
self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first() self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first()
if len(Counter.get_barmen_list(self.object.id)) < 1: # Check that at least one barman is logged in
return self.cancel(request)
if 'basket' not in request.session.keys(): if 'basket' not in request.session.keys():
request.session['basket'] = {} request.session['basket'] = {}
...@@ -138,20 +132,22 @@ class CounterClick(DetailView): ...@@ -138,20 +132,22 @@ class CounterClick(DetailView):
def add_product(self, request): def add_product(self, request):
""" Add a product to the basket """ """ Add a product to the basket """
if str(request.POST['product_id']) in request.session['basket']: pid = str(request.POST['product_id'])
request.session['basket'][str(request.POST['product_id'])] += 1 if pid in request.session['basket']:
request.session['basket'][pid] += 1
else: else:
request.session['basket'][str(request.POST['product_id'])] = 1 request.session['basket'][pid] = 1
request.session.modified = True request.session.modified = True
def del_product(self, request): def del_product(self, request):
""" Delete a product from the basket """ """ Delete a product from the basket """
if str(request.POST['product_id']) in request.session['basket']: pid = str(request.POST['product_id'])
request.session['basket'][str(request.POST['product_id'])] -= 1 if pid in request.session['basket']:
if request.session['basket'][str(request.POST['product_id'])] <= 0: request.session['basket'][pid] -= 1
del request.session['basket'][str(request.POST['product_id'])] if request.session['basket'][pid] <= 0:
del request.session['basket'][pid]
else: else:
request.session['basket'][str(request.POST['product_id'])] = 0 request.session['basket'][pid] = 0
request.session.modified = True request.session.modified = True
def finish(self, request): def finish(self, request):
...@@ -164,7 +160,7 @@ class CounterClick(DetailView): ...@@ -164,7 +160,7 @@ class CounterClick(DetailView):
def cancel(self, request): def cancel(self, request):
""" Cancel the click session """ """ Cancel the click session """
kwargs = {'counter_id': self.object.id} kwargs = {'counter_id': self.object.id}
del request.session['basket'] request.session.pop('basket', None)
return HttpResponseRedirect(reverse_lazy('counter:details', args=self.args, kwargs=kwargs)) return HttpResponseRedirect(reverse_lazy('counter:details', args=self.args, kwargs=kwargs))
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
......
...@@ -57,8 +57,6 @@ MIDDLEWARE_CLASSES = ( ...@@ -57,8 +57,6 @@ MIDDLEWARE_CLASSES = (
'core.middleware.AuthenticationMiddleware', 'core.middleware.AuthenticationMiddleware',
) )
SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'
ROOT_URLCONF = 'sith.urls' ROOT_URLCONF = 'sith.urls'
TEMPLATES = [ TEMPLATES = [
......
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