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-18 23:54:51 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2016-04-12 11:08:37 +00:00
|
|
|
from django.utils import timezone
|
2016-04-15 09:50:31 +00:00
|
|
|
from django import forms
|
2016-04-12 11:08:37 +00:00
|
|
|
|
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-05-31 17:32:15 +00:00
|
|
|
from counter.models import Counter, Customer, Product, Selling, Refilling
|
2016-03-28 12:54:35 +00:00
|
|
|
|
2016-04-15 09:50:31 +00:00
|
|
|
class GetUserForm(forms.Form):
|
2016-04-18 02:37:37 +00:00
|
|
|
"""
|
|
|
|
The Form class aims at providing a valid user_id field in its cleaned data, in order to pass it to some view,
|
|
|
|
reverse function, or any other use.
|
|
|
|
|
|
|
|
The Form implements a nice JS widget allowing the user to type a customer account id, or search the database with
|
|
|
|
some nickname, first name, or last name (TODO)
|
|
|
|
"""
|
|
|
|
code = forms.CharField(label="Code", max_length=64, required=False)
|
|
|
|
id = forms.IntegerField(label="ID", required=False)
|
|
|
|
# TODO: add a nice JS widget to search for users
|
2016-04-15 09:50:31 +00:00
|
|
|
|
2016-04-18 02:37:37 +00:00
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super(GetUserForm, self).clean()
|
|
|
|
user = None
|
|
|
|
if cleaned_data['code'] != "":
|
|
|
|
user = Customer.objects.filter(account_id=cleaned_data['code']).first()
|
|
|
|
elif cleaned_data['id'] is not None:
|
|
|
|
user = Customer.objects.filter(user=cleaned_data['id']).first()
|
|
|
|
if user is None:
|
|
|
|
raise forms.ValidationError("User not found")
|
|
|
|
cleaned_data['user_id'] = user.user.id
|
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
class CounterMain(DetailView, ProcessFormView, 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-18 02:37:37 +00:00
|
|
|
form_class = GetUserForm # Form to enter a client code and get the corresponding user id
|
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
|
|
|
"""
|
2016-04-18 02:37:37 +00:00
|
|
|
We handle here the login form for the barman
|
2016-04-12 11:08:37 +00:00
|
|
|
|
|
|
|
Also handle the timeout
|
|
|
|
"""
|
2016-04-18 02:37:37 +00:00
|
|
|
if self.request.method == 'POST':
|
|
|
|
self.object = self.get_object()
|
2016-04-15 09:50:31 +00:00
|
|
|
kwargs = super(CounterMain, self).get_context_data(**kwargs)
|
2016-04-18 02:37:37 +00:00
|
|
|
# TODO: make some checks on the counter type, in order not to make the AuthenticationForm if there is no need to
|
2016-04-15 09:50:31 +00:00
|
|
|
kwargs['login_form'] = AuthenticationForm()
|
|
|
|
kwargs['form'] = self.get_form()
|
2016-04-19 17:58:57 +00:00
|
|
|
kwargs['barmen'] = Counter.get_barmen_list(self.object.id)
|
2016-05-31 23:33:20 +00:00
|
|
|
if 'last_basket' in self.request.session.keys():
|
|
|
|
kwargs['last_basket'] = self.request.session.pop('last_basket')
|
|
|
|
kwargs['last_customer'] = self.request.session.pop('last_customer')
|
|
|
|
kwargs['last_total'] = self.request.session.pop('last_total')
|
|
|
|
kwargs['new_customer_amount'] = self.request.session.pop('new_customer_amount')
|
2016-04-15 09:50:31 +00:00
|
|
|
return kwargs
|
|
|
|
|
2016-04-18 02:37:37 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
"""
|
|
|
|
We handle here the redirection, passing the user id of the asked customer
|
|
|
|
"""
|
|
|
|
self.kwargs['user_id'] = form.cleaned_data['user_id']
|
|
|
|
return super(CounterMain, self).form_valid(form)
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy('counter:click', args=self.args, kwargs=self.kwargs)
|
|
|
|
|
2016-04-18 23:54:51 +00:00
|
|
|
class CounterClick(DetailView):
|
2016-04-15 09:50:31 +00:00
|
|
|
"""
|
|
|
|
The click view
|
2016-04-18 23:54:51 +00:00
|
|
|
This is a detail view not to have to worry about loading the counter
|
|
|
|
Everything is made by hand in the post method
|
2016-04-15 09:50:31 +00:00
|
|
|
"""
|
2016-04-18 15:34:21 +00:00
|
|
|
model = Counter
|
2016-04-15 09:50:31 +00:00
|
|
|
template_name = 'counter/counter_click.jinja'
|
|
|
|
pk_url_kwarg = "counter_id"
|
2016-04-18 15:34:21 +00:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
2016-04-18 23:54:51 +00:00
|
|
|
"""Simple get view"""
|
2016-04-18 15:34:21 +00:00
|
|
|
self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first()
|
2016-06-02 12:55:12 +00:00
|
|
|
if 'basket' not in request.session.keys(): # Init the basket session entry
|
|
|
|
request.session['basket'] = {}
|
|
|
|
request.session['basket_total'] = 0
|
|
|
|
request.session['not_enough'] = False
|
2016-04-19 17:58:57 +00:00
|
|
|
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
|
|
|
|
return ret
|
2016-04-15 09:50:31 +00:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2016-04-18 23:54:51 +00:00
|
|
|
""" Handle the many possibilities of the post request """
|
2016-04-18 15:34:21 +00:00
|
|
|
self.object = self.get_object()
|
|
|
|
self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first()
|
2016-04-19 17:58:57 +00:00
|
|
|
if len(Counter.get_barmen_list(self.object.id)) < 1: # Check that at least one barman is logged in
|
|
|
|
return self.cancel(request)
|
2016-04-18 23:54:51 +00:00
|
|
|
if 'basket' not in request.session.keys():
|
|
|
|
request.session['basket'] = {}
|
2016-06-02 12:55:12 +00:00
|
|
|
request.session['basket_total'] = 0
|
|
|
|
request.session['not_enough'] = False
|
2016-04-18 23:54:51 +00:00
|
|
|
|
|
|
|
if 'add_product' in request.POST['action']:
|
|
|
|
self.add_product(request)
|
|
|
|
elif 'del_product' in request.POST['action']:
|
|
|
|
self.del_product(request)
|
|
|
|
elif 'cancel' in request.POST['action']:
|
|
|
|
return self.cancel(request)
|
|
|
|
elif 'finish' in request.POST['action']:
|
|
|
|
return self.finish(request)
|
|
|
|
context = self.get_context_data(object=self.object)
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
2016-06-01 22:26:31 +00:00
|
|
|
def is_barman_price(self, ):
|
2016-06-02 12:55:12 +00:00
|
|
|
if self.customer.user.id in [s.id for s in Counter.get_barmen_list(self.object.id)]:
|
2016-06-01 22:26:31 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_price(self, pid):
|
|
|
|
p = Product.objects.filter(pk=pid).first()
|
|
|
|
if self.is_barman_price():
|
|
|
|
price = p.special_selling_price
|
|
|
|
else:
|
|
|
|
price = p.selling_price
|
|
|
|
return price
|
|
|
|
|
|
|
|
def sum_basket(self, request):
|
|
|
|
total = 0
|
|
|
|
for pid,infos in request.session['basket'].items():
|
|
|
|
total += infos['price'] * infos['qty']
|
2016-06-02 12:55:12 +00:00
|
|
|
return total / 100
|
2016-06-01 22:26:31 +00:00
|
|
|
|
2016-06-02 12:55:12 +00:00
|
|
|
def add_product(self, request, q = 1):
|
2016-04-18 23:54:51 +00:00
|
|
|
""" Add a product to the basket """
|
2016-04-19 17:58:57 +00:00
|
|
|
pid = str(request.POST['product_id'])
|
2016-06-01 22:26:31 +00:00
|
|
|
price = self.get_price(pid)
|
2016-06-02 12:55:12 +00:00
|
|
|
total = self.sum_basket(request)
|
|
|
|
if self.customer.amount < (total + float(price)):
|
|
|
|
request.session['not_enough'] = True
|
2016-06-01 22:26:31 +00:00
|
|
|
return
|
2016-04-19 17:58:57 +00:00
|
|
|
if pid in request.session['basket']:
|
2016-06-02 12:55:12 +00:00
|
|
|
request.session['basket'][pid]['qty'] += q
|
2016-04-18 23:54:51 +00:00
|
|
|
else:
|
2016-06-02 12:55:12 +00:00
|
|
|
request.session['basket'][pid] = {'qty': q, 'price': int(price*100)}
|
|
|
|
request.session['not_enough'] = False
|
2016-04-18 23:54:51 +00:00
|
|
|
request.session.modified = True
|
|
|
|
|
|
|
|
def del_product(self, request):
|
|
|
|
""" Delete a product from the basket """
|
2016-04-19 17:58:57 +00:00
|
|
|
pid = str(request.POST['product_id'])
|
|
|
|
if pid in request.session['basket']:
|
2016-06-01 22:26:31 +00:00
|
|
|
request.session['basket'][pid]['qty'] -= 1
|
|
|
|
if request.session['basket'][pid]['qty'] <= 0:
|
2016-04-19 17:58:57 +00:00
|
|
|
del request.session['basket'][pid]
|
2016-04-18 23:54:51 +00:00
|
|
|
else:
|
2016-04-19 17:58:57 +00:00
|
|
|
request.session['basket'][pid] = 0
|
2016-04-18 23:54:51 +00:00
|
|
|
request.session.modified = True
|
|
|
|
|
|
|
|
def finish(self, request):
|
|
|
|
""" Finish the click session, and validate the basket """
|
2016-06-01 22:26:31 +00:00
|
|
|
if self.is_barman_price():
|
2016-05-31 23:33:20 +00:00
|
|
|
seller = self.customer.user
|
|
|
|
else:
|
|
|
|
seller = Counter.get_random_barman(self.object.id)
|
|
|
|
request.session['last_basket'] = []
|
2016-06-02 12:55:12 +00:00
|
|
|
for pid,infos in request.session['basket'].items():
|
2016-06-01 22:26:31 +00:00
|
|
|
# This duplicates code for DB optimization (prevent to load many times the same object)
|
2016-05-31 17:32:15 +00:00
|
|
|
p = Product.objects.filter(pk=pid).first()
|
2016-06-01 22:26:31 +00:00
|
|
|
if self.is_barman_price():
|
2016-05-31 23:33:20 +00:00
|
|
|
uprice = p.special_selling_price
|
|
|
|
else:
|
|
|
|
uprice = p.selling_price
|
2016-06-02 12:55:12 +00:00
|
|
|
request.session['last_basket'].append("%d x %s" % (infos['qty'], p.name))
|
2016-05-31 23:33:20 +00:00
|
|
|
s = Selling(product=p, counter=self.object, unit_price=uprice,
|
2016-06-02 12:55:12 +00:00
|
|
|
quantity=infos['qty'], seller=seller, customer=self.customer)
|
2016-05-31 17:32:15 +00:00
|
|
|
s.save()
|
2016-05-31 23:33:20 +00:00
|
|
|
request.session['last_customer'] = self.customer.user.get_display_name()
|
2016-06-02 12:55:12 +00:00
|
|
|
request.session['last_total'] = "%0.2f" % self.sum_basket(request)
|
2016-05-31 23:33:20 +00:00
|
|
|
request.session['new_customer_amount'] = str(self.customer.amount)
|
2016-04-18 23:54:51 +00:00
|
|
|
del request.session['basket']
|
2016-05-31 23:33:20 +00:00
|
|
|
request.session.modified = True
|
2016-06-01 22:26:31 +00:00
|
|
|
kwargs = {
|
|
|
|
'counter_id': self.object.id,
|
|
|
|
}
|
2016-04-18 23:54:51 +00:00
|
|
|
return HttpResponseRedirect(reverse_lazy('counter:details', args=self.args, kwargs=kwargs))
|
|
|
|
|
|
|
|
def cancel(self, request):
|
|
|
|
""" Cancel the click session """
|
|
|
|
kwargs = {'counter_id': self.object.id}
|
2016-04-19 17:58:57 +00:00
|
|
|
request.session.pop('basket', None)
|
2016-04-18 23:54:51 +00:00
|
|
|
return HttpResponseRedirect(reverse_lazy('counter:details', args=self.args, kwargs=kwargs))
|
2016-04-18 15:34:21 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2016-04-18 23:54:51 +00:00
|
|
|
""" Add customer to the context """
|
2016-04-18 15:34:21 +00:00
|
|
|
kwargs = super(CounterClick, self).get_context_data(**kwargs)
|
|
|
|
kwargs['customer'] = self.customer
|
2016-06-02 12:55:12 +00:00
|
|
|
kwargs['basket_total'] = self.sum_basket(self.request)
|
2016-04-18 15:34:21 +00:00
|
|
|
return kwargs
|
|
|
|
|
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']
|
|
|
|
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
|
|
|
|
2016-04-18 15:34:21 +00:00
|
|
|
## Counter admin views
|
|
|
|
|
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
|
|
|
|
|
|
|
|