Sith/counter/views.py

146 lines
5.3 KiB
Python
Raw Normal View History

2016-03-28 12:54:35 +00:00
from django.shortcuts import render
2016-03-31 08:36:00 +00:00
from django.views.generic import ListView, DetailView, RedirectView
2016-04-15 09:50:31 +00:00
from django.views.generic.edit import UpdateView, CreateView, DeleteView, ProcessFormView, FormMixin
2016-03-29 08:30:24 +00:00
from django.forms.models import modelform_factory
from django.forms import CheckboxSelectMultiple
from django.core.urlresolvers import reverse_lazy
2016-03-31 08:36:00 +00:00
from django.contrib.auth.forms import AuthenticationForm
2016-04-12 11:08:37 +00:00
from django.utils import timezone
from django.conf import settings
2016-04-15 09:50:31 +00:00
from django import forms
2016-04-12 11:08:37 +00:00
from datetime import timedelta
2016-03-28 12:54:35 +00:00
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
2016-04-12 11:08:37 +00:00
from subscription.models import Subscriber
2016-04-15 09:50:31 +00:00
from accounting.models import Customer
2016-03-28 12:54:35 +00:00
from counter.models import Counter
2016-04-15 09:50:31 +00:00
class GetUserForm(forms.Form):
username = forms.CharField(label="Name", max_length=64, required=False)
class CounterMain(DetailView, FormMixin):
2016-03-31 08:36:00 +00:00
"""
The public (barman) view
"""
2016-03-28 12:54:35 +00:00
model = Counter
2016-04-15 09:50:31 +00:00
template_name = 'counter/counter_main.jinja'
2016-03-28 12:54:35 +00:00
pk_url_kwarg = "counter_id"
2016-04-15 09:50:31 +00:00
form_class = GetUserForm
2016-03-29 08:30:24 +00:00
2016-04-12 08:00:47 +00:00
def get_context_data(self, **kwargs):
2016-04-12 11:08:37 +00:00
"""
Get the barman list for the template
Also handle the timeout
"""
2016-04-15 09:50:31 +00:00
kwargs = super(CounterMain, self).get_context_data(**kwargs)
kwargs['login_form'] = AuthenticationForm()
kwargs['form'] = self.get_form()
print(kwargs)
2016-04-12 08:00:47 +00:00
if str(self.object.id) in list(Counter.barmen_session.keys()):
2016-04-12 11:08:37 +00:00
if (timezone.now() - Counter.barmen_session[str(self.object.id)]['time']) < timedelta(minutes=settings.SITH_BARMAN_TIMEOUT):
2016-04-15 09:50:31 +00:00
kwargs['barmen'] = []
2016-04-12 11:08:37 +00:00
for b in Counter.barmen_session[str(self.object.id)]['users']:
2016-04-15 09:50:31 +00:00
kwargs['barmen'].append(Subscriber.objects.filter(id=b).first())
2016-04-12 11:08:37 +00:00
Counter.barmen_session[str(self.object.id)]['time'] = timezone.now()
else:
Counter.barmen_session[str(self.object.id)]['users'] = {}
2016-04-12 08:00:47 +00:00
else:
2016-04-15 09:50:31 +00:00
kwargs['barmen'] = []
return kwargs
class CounterClick(DetailView, ProcessFormView, FormMixin):
"""
The click view
"""
model = Counter # TODO change that to a basket class
template_name = 'counter/counter_click.jinja'
pk_url_kwarg = "counter_id"
form_class = GetUserForm
def post(self, request, *args, **kwargs):
# TODO: handle the loading of a user, to display the click view
# TODO: Do the form and the template for the click view
return super(CounterClick, self).post(request, *args, **kwargs)
def get_success_url(self):
return reverse_lazy('counter:click', args=self.args, kwargs=self.kwargs)
2016-04-12 08:00:47 +00:00
2016-03-31 08:36:00 +00:00
class CounterLogin(RedirectView):
2016-04-12 08:00:47 +00:00
"""
Handle the login of a barman
Logged barmen are stored in the class-wide variable 'barmen_session', in the Counter model
"""
2016-03-31 08:36:00 +00:00
permanent = False
2016-04-12 08:00:47 +00:00
def post(self, request, *args, **kwargs):
2016-04-12 11:08:37 +00:00
"""
Register the logged user as barman for this counter
"""
2016-04-12 08:00:47 +00:00
self.counter_id = kwargs['counter_id']
# TODO: make some checks on the counter type
form = AuthenticationForm(request, data=request.POST)
2016-03-31 08:36:00 +00:00
if form.is_valid():
2016-04-12 11:08:37 +00:00
user = Subscriber.objects.filter(username=form.cleaned_data['username']).first()
2016-04-12 08:00:47 +00:00
if self.counter_id not in Counter.barmen_session.keys():
2016-04-12 11:08:37 +00:00
Counter.barmen_session[self.counter_id] = {'users': {user.id}, 'time': timezone.now()}
2016-04-12 08:00:47 +00:00
else:
2016-04-12 11:08:37 +00:00
Counter.barmen_session[self.counter_id]['users'].add(user.id)
2016-04-12 08:00:47 +00:00
else:
print("Error logging the barman") # TODO handle that nicely
return super(CounterLogin, self).post(request, *args, **kwargs)
def get_redirect_url(self, *args, **kwargs):
return reverse_lazy('counter:details', args=args, kwargs=kwargs)
class CounterLogout(RedirectView):
permanent = False
def post(self, request, *args, **kwargs):
2016-04-12 11:08:37 +00:00
"""
Unregister the user from the barman
"""
2016-04-12 08:00:47 +00:00
self.counter_id = kwargs['counter_id']
2016-04-12 11:08:37 +00:00
Counter.barmen_session[str(self.counter_id)]['users'].remove(int(request.POST['user_id']))
2016-04-12 08:00:47 +00:00
return super(CounterLogout, self).post(request, *args, **kwargs)
def get_redirect_url(self, *args, **kwargs):
return reverse_lazy('counter:details', args=args, kwargs=kwargs)
2016-03-31 08:36:00 +00:00
class CounterListView(CanViewMixin, ListView):
"""
A list view for the admins
"""
model = Counter
template_name = 'counter/counter_list.jinja'
2016-03-29 08:30:24 +00:00
class CounterEditView(CanEditMixin, UpdateView):
"""
2016-03-31 08:36:00 +00:00
Edit a counter's main informations (for the counter's admin)
2016-03-29 08:30:24 +00:00
"""
model = Counter
form_class = modelform_factory(Counter, fields=['name', 'club', 'type', 'products'],
widgets={'products':CheckboxSelectMultiple})
pk_url_kwarg = "counter_id"
template_name = 'counter/counter_edit.jinja'
class CounterCreateView(CanEditMixin, CreateView):
"""
2016-03-31 08:36:00 +00:00
Create a counter (for the admins)
2016-03-29 08:30:24 +00:00
"""
model = Counter
form_class = modelform_factory(Counter, fields=['name', 'club', 'type', 'products'],
widgets={'products':CheckboxSelectMultiple})
template_name = 'counter/counter_edit.jinja'
class CounterDeleteView(CanEditMixin, DeleteView):
"""
2016-03-31 08:36:00 +00:00
Delete a counter (for the admins)
2016-03-29 08:30:24 +00:00
"""
model = Counter
pk_url_kwarg = "counter_id"
template_name = 'core/delete_confirm.jinja'
success_url = reverse_lazy('counter:admin_list')
2016-03-31 08:36:00 +00:00