Commit c1a151d7 authored by Skia's avatar Skia
Browse files

Add timeout for barmen

parent 20a97526
Pipeline #7 skipped
......@@ -5,9 +5,13 @@ from django.forms.models import modelform_factory
from django.forms import CheckboxSelectMultiple
from django.core.urlresolvers import reverse_lazy
from django.contrib.auth.forms import AuthenticationForm
from django.utils import timezone
from django.conf import settings
from datetime import timedelta
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
from core.models import User
from subscription.models import Subscriber
from counter.models import Counter
class CounterDetail(DetailView):
......@@ -19,12 +23,23 @@ class CounterDetail(DetailView):
pk_url_kwarg = "counter_id"
def get_context_data(self, **kwargs):
"""
Get the barman list for the template
Also handle the timeout
"""
context = super(CounterDetail, self).get_context_data(**kwargs)
context['login_form'] = AuthenticationForm()
print(self.object.id)
print(list(Counter.barmen_session.keys()))
if str(self.object.id) in list(Counter.barmen_session.keys()):
context['barmen'] = []
for b in Counter.barmen_session[str(self.object.id)]:
context['barmen'].append(User.objects.filter(id=b).first())
if (timezone.now() - Counter.barmen_session[str(self.object.id)]['time']) < timedelta(minutes=settings.SITH_BARMAN_TIMEOUT):
context['barmen'] = []
for b in Counter.barmen_session[str(self.object.id)]['users']:
context['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'] = {}
else:
context['barmen'] = []
return context
......@@ -37,15 +52,18 @@ class CounterLogin(RedirectView):
"""
permanent = False
def post(self, request, *args, **kwargs):
"""
Register the logged user as barman for this counter
"""
self.counter_id = kwargs['counter_id']
# TODO: make some checks on the counter type
form = AuthenticationForm(request, data=request.POST)
if form.is_valid():
user = User.objects.filter(username=form.cleaned_data['username']).first()
user = Subscriber.objects.filter(username=form.cleaned_data['username']).first()
if self.counter_id not in Counter.barmen_session.keys():
Counter.barmen_session[self.counter_id] = {user.id} # TODO add timeout
Counter.barmen_session[self.counter_id] = {'users': {user.id}, 'time': timezone.now()}
else:
Counter.barmen_session[self.counter_id].add(user.id)
Counter.barmen_session[self.counter_id]['users'].add(user.id)
else:
print("Error logging the barman") # TODO handle that nicely
return super(CounterLogin, self).post(request, *args, **kwargs)
......@@ -56,8 +74,11 @@ class CounterLogin(RedirectView):
class CounterLogout(RedirectView):
permanent = False
def post(self, request, *args, **kwargs):
"""
Unregister the user from the barman
"""
self.counter_id = kwargs['counter_id']
Counter.barmen_session[self.counter_id].remove(int(request.POST['user_id']))
Counter.barmen_session[str(self.counter_id)]['users'].remove(int(request.POST['user_id']))
return super(CounterLogout, self).post(request, *args, **kwargs)
def get_redirect_url(self, *args, **kwargs):
......
......@@ -246,3 +246,6 @@ CLUB_ROLES = {
# This corresponds to the maximum role a user can freely subscribe to
# In this case, MAXIMUM_FREE_ROLE=1 means that a user can set himself as "Membre actif" or "Curieux", but not higher
MAXIMUM_FREE_ROLE=1
# Minutes to timeout the logged barmen
SITH_BARMAN_TIMEOUT=20
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