mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-18 04:03:22 +00:00
Add timeout for barmen
This commit is contained in:
parent
20a97526d4
commit
c1a151d754
@ -5,9 +5,13 @@ from django.forms.models import modelform_factory
|
|||||||
from django.forms import CheckboxSelectMultiple
|
from django.forms import CheckboxSelectMultiple
|
||||||
from django.core.urlresolvers import reverse_lazy
|
from django.core.urlresolvers import reverse_lazy
|
||||||
from django.contrib.auth.forms import AuthenticationForm
|
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.views import CanViewMixin, CanEditMixin, CanEditPropMixin
|
||||||
from core.models import User
|
from subscription.models import Subscriber
|
||||||
from counter.models import Counter
|
from counter.models import Counter
|
||||||
|
|
||||||
class CounterDetail(DetailView):
|
class CounterDetail(DetailView):
|
||||||
@ -19,12 +23,23 @@ class CounterDetail(DetailView):
|
|||||||
pk_url_kwarg = "counter_id"
|
pk_url_kwarg = "counter_id"
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
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 = super(CounterDetail, self).get_context_data(**kwargs)
|
||||||
context['login_form'] = AuthenticationForm()
|
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()):
|
if str(self.object.id) in list(Counter.barmen_session.keys()):
|
||||||
|
if (timezone.now() - Counter.barmen_session[str(self.object.id)]['time']) < timedelta(minutes=settings.SITH_BARMAN_TIMEOUT):
|
||||||
context['barmen'] = []
|
context['barmen'] = []
|
||||||
for b in Counter.barmen_session[str(self.object.id)]:
|
for b in Counter.barmen_session[str(self.object.id)]['users']:
|
||||||
context['barmen'].append(User.objects.filter(id=b).first())
|
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:
|
else:
|
||||||
context['barmen'] = []
|
context['barmen'] = []
|
||||||
return context
|
return context
|
||||||
@ -37,15 +52,18 @@ class CounterLogin(RedirectView):
|
|||||||
"""
|
"""
|
||||||
permanent = False
|
permanent = False
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Register the logged user as barman for this counter
|
||||||
|
"""
|
||||||
self.counter_id = kwargs['counter_id']
|
self.counter_id = kwargs['counter_id']
|
||||||
# TODO: make some checks on the counter type
|
# TODO: make some checks on the counter type
|
||||||
form = AuthenticationForm(request, data=request.POST)
|
form = AuthenticationForm(request, data=request.POST)
|
||||||
if form.is_valid():
|
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():
|
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:
|
else:
|
||||||
Counter.barmen_session[self.counter_id].add(user.id)
|
Counter.barmen_session[self.counter_id]['users'].add(user.id)
|
||||||
else:
|
else:
|
||||||
print("Error logging the barman") # TODO handle that nicely
|
print("Error logging the barman") # TODO handle that nicely
|
||||||
return super(CounterLogin, self).post(request, *args, **kwargs)
|
return super(CounterLogin, self).post(request, *args, **kwargs)
|
||||||
@ -56,8 +74,11 @@ class CounterLogin(RedirectView):
|
|||||||
class CounterLogout(RedirectView):
|
class CounterLogout(RedirectView):
|
||||||
permanent = False
|
permanent = False
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Unregister the user from the barman
|
||||||
|
"""
|
||||||
self.counter_id = kwargs['counter_id']
|
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)
|
return super(CounterLogout, self).post(request, *args, **kwargs)
|
||||||
|
|
||||||
def get_redirect_url(self, *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
|
# 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
|
# 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
|
MAXIMUM_FREE_ROLE=1
|
||||||
|
|
||||||
|
# Minutes to timeout the logged barmen
|
||||||
|
SITH_BARMAN_TIMEOUT=20
|
||||||
|
Loading…
Reference in New Issue
Block a user