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-06-26 17:30:28 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-07-17 10:38:02 +00:00
|
|
|
from django.conf import settings
|
2016-07-21 23:19:04 +00:00
|
|
|
from django.db import DataError, transaction
|
2016-04-12 11:08:37 +00:00
|
|
|
|
2016-06-26 17:30:28 +00:00
|
|
|
import re
|
2016-08-01 22:32:55 +00:00
|
|
|
from datetime import date, timedelta
|
2016-08-19 21:24:23 +00:00
|
|
|
from ajax_select.fields import AutoCompleteSelectField, AutoCompleteSelectMultipleField
|
|
|
|
from ajax_select import make_ajax_form, make_ajax_field
|
2016-03-28 12:54:35 +00:00
|
|
|
|
2016-07-28 14:11:18 +00:00
|
|
|
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin
|
2016-08-19 21:24:23 +00:00
|
|
|
from core.views.forms import SelectUser
|
2016-04-12 11:08:37 +00:00
|
|
|
from subscription.models import Subscriber
|
2016-08-01 22:32:55 +00:00
|
|
|
from subscription.views import get_subscriber
|
2016-07-27 18:05:45 +00:00
|
|
|
from counter.models import Counter, Customer, Product, Selling, Refilling, ProductType
|
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)
|
|
|
|
"""
|
2016-06-26 17:30:28 +00:00
|
|
|
code = forms.CharField(label="Code", max_length=10, required=False)
|
2016-08-19 21:24:23 +00:00
|
|
|
id = AutoCompleteSelectField('users', required=False, label=_("Select user"), help_text=None)
|
2016-04-15 09:50:31 +00:00
|
|
|
|
2016-06-26 17:30:28 +00:00
|
|
|
def as_p(self):
|
|
|
|
self.fields['code'].widget.attrs['autofocus'] = True
|
|
|
|
return super(GetUserForm, self).as_p()
|
|
|
|
|
2016-04-18 02:37:37 +00:00
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super(GetUserForm, self).clean()
|
2016-08-01 22:32:55 +00:00
|
|
|
cus = None
|
2016-04-18 02:37:37 +00:00
|
|
|
if cleaned_data['code'] != "":
|
2016-08-01 22:32:55 +00:00
|
|
|
cus = Customer.objects.filter(account_id=cleaned_data['code']).first()
|
2016-04-18 02:37:37 +00:00
|
|
|
elif cleaned_data['id'] is not None:
|
2016-08-01 22:32:55 +00:00
|
|
|
cus = Customer.objects.filter(user=cleaned_data['id']).first()
|
|
|
|
sub = get_subscriber(cus.user) if cus is not None else None
|
2016-08-18 17:52:20 +00:00
|
|
|
if (cus is None or sub is None or not sub.subscriptions.last() or
|
|
|
|
(date.today() - sub.subscriptions.last().subscription_end) > timedelta(days=90)):
|
2016-07-27 18:05:45 +00:00
|
|
|
raise forms.ValidationError(_("User not found"))
|
2016-08-01 22:32:55 +00:00
|
|
|
cleaned_data['user_id'] = cus.user.id
|
|
|
|
cleaned_data['user'] = cus.user
|
2016-04-18 02:37:37 +00:00
|
|
|
return cleaned_data
|
|
|
|
|
2016-07-17 09:38:19 +00:00
|
|
|
class RefillForm(forms.ModelForm):
|
|
|
|
error_css_class = 'error'
|
|
|
|
required_css_class = 'required'
|
|
|
|
class Meta:
|
|
|
|
model = Refilling
|
|
|
|
fields = ['amount', 'payment_method', 'bank']
|
|
|
|
|
2016-04-18 02:37:37 +00:00
|
|
|
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
|
|
|
"""
|
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)
|
|
|
|
kwargs['login_form'] = AuthenticationForm()
|
2016-06-26 18:07:29 +00:00
|
|
|
kwargs['login_form'].fields['username'].widget.attrs['autofocus'] = True
|
2016-08-20 14:09:46 +00:00
|
|
|
kwargs['login_form'].cleaned_data = {} # add_error fails if there are no cleaned_data
|
|
|
|
if "credentials" in self.request.GET:
|
|
|
|
kwargs['login_form'].add_error(None, _("Bad credentials"))
|
|
|
|
if "subscription" in self.request.GET:
|
|
|
|
kwargs['login_form'].add_error(None, _("User is not subscriber"))
|
2016-04-15 09:50:31 +00:00
|
|
|
kwargs['form'] = self.get_form()
|
2016-07-21 18:03:31 +00:00
|
|
|
if self.object.type == 'BAR':
|
2016-08-06 10:37:36 +00:00
|
|
|
kwargs['barmen'] = self.object.get_barmen_list()
|
2016-07-21 18:03:31 +00:00
|
|
|
elif self.request.user.is_authenticated():
|
|
|
|
kwargs['barmen'] = [self.request.user]
|
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-08-20 00:55:48 +00:00
|
|
|
request.session['too_young'] = False
|
2016-08-20 20:12:46 +00:00
|
|
|
request.session['not_allowed'] = False
|
2016-07-17 09:38:19 +00:00
|
|
|
self.refill_form = None
|
2016-04-19 17:58:57 +00:00
|
|
|
ret = super(CounterClick, self).get(request, *args, **kwargs)
|
2016-07-21 18:03:31 +00:00
|
|
|
if ((self.object.type != "BAR" and not request.user.is_authenticated()) or
|
|
|
|
(self.object.type == "BAR" and
|
2016-08-06 10:37:36 +00:00
|
|
|
len(self.object.get_barmen_list()) < 1)): # Check that at least one barman is logged in
|
2016-07-21 18:03:31 +00:00
|
|
|
ret = self.cancel(request) # Otherwise, go to main view
|
2016-04-19 17:58:57 +00:00
|
|
|
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-07-17 09:38:19 +00:00
|
|
|
self.refill_form = None
|
2016-07-22 11:34:34 +00:00
|
|
|
if ((self.object.type != "BAR" and not request.user.is_authenticated()) or
|
|
|
|
(self.object.type == "BAR" and
|
2016-08-06 10:37:36 +00:00
|
|
|
len(self.object.get_barmen_list()) < 1)): # Check that at least one barman is logged in
|
2016-04-19 17:58:57 +00:00
|
|
|
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-08-20 00:55:48 +00:00
|
|
|
request.session['too_young'] = False
|
2016-08-20 20:12:46 +00:00
|
|
|
request.session['not_allowed'] = False
|
2016-07-22 11:34:34 +00:00
|
|
|
if self.object.type != "BAR":
|
|
|
|
self.operator = request.user
|
|
|
|
elif self.is_barman_price():
|
|
|
|
self.operator = self.customer.user
|
|
|
|
else:
|
2016-08-06 10:37:36 +00:00
|
|
|
self.operator = self.object.get_random_barman()
|
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)
|
2016-06-26 18:07:29 +00:00
|
|
|
elif 'refill' in request.POST['action']:
|
|
|
|
self.refill(request)
|
2016-06-26 17:30:28 +00:00
|
|
|
elif 'code' in request.POST['action']:
|
|
|
|
return self.parse_code(request)
|
2016-04-18 23:54:51 +00:00
|
|
|
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-26 17:30:28 +00:00
|
|
|
def is_barman_price(self):
|
2016-08-06 10:37:36 +00:00
|
|
|
if self.object.type == "BAR" and self.customer.user.id in [s.id for s in self.object.get_barmen_list()]:
|
2016-06-01 22:26:31 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2016-08-20 00:55:48 +00:00
|
|
|
def get_product(self, pid):
|
|
|
|
return Product.objects.filter(pk=int(pid)).first()
|
|
|
|
|
2016-06-01 22:26:31 +00:00
|
|
|
def get_price(self, pid):
|
2016-08-20 00:55:48 +00:00
|
|
|
p = self.get_product(pid)
|
2016-06-01 22:26:31 +00:00
|
|
|
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-08-20 14:09:46 +00:00
|
|
|
def get_total_quantity_for_pid(self, request, pid):
|
|
|
|
pid = str(pid)
|
|
|
|
try:
|
|
|
|
return request.session['basket'][pid]['qty'] + request.session['basket'][pid]['bonus_qty']
|
|
|
|
except:
|
|
|
|
return 0
|
|
|
|
|
2016-06-26 17:30:28 +00:00
|
|
|
def add_product(self, request, q = 1, p=None):
|
2016-08-20 00:55:48 +00:00
|
|
|
"""
|
|
|
|
Add a product to the basket
|
|
|
|
q is the quantity passed as integer
|
|
|
|
p is the product id, passed as an integer
|
|
|
|
"""
|
2016-07-16 14:48:56 +00:00
|
|
|
pid = p or request.POST['product_id']
|
|
|
|
pid = str(pid)
|
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)
|
2016-08-20 14:09:46 +00:00
|
|
|
product = self.get_product(pid)
|
2016-08-20 20:12:46 +00:00
|
|
|
can_buy = False
|
|
|
|
for g in product.buying_groups.all():
|
|
|
|
if request.user.is_in_group(g.name):
|
|
|
|
can_buy = True
|
|
|
|
if not can_buy:
|
|
|
|
request.session['not_allowed'] = True
|
|
|
|
return False
|
2016-08-20 14:09:46 +00:00
|
|
|
bq = 0 # Bonus quantity, for trays
|
|
|
|
if product.tray: # Handle the tray to adjust the quantity q to add and the bonus quantity bq
|
|
|
|
total_qty_mod_6 = self.get_total_quantity_for_pid(request, pid) % 6
|
|
|
|
bq = int((total_qty_mod_6 + q) / 6) # Integer division
|
|
|
|
q -= bq
|
|
|
|
if self.customer.amount < (total + q*float(price)): # Check for enough money
|
2016-06-02 12:55:12 +00:00
|
|
|
request.session['not_enough'] = True
|
2016-06-26 17:30:28 +00:00
|
|
|
return False
|
2016-08-20 14:09:46 +00:00
|
|
|
if self.customer.user.get_age() < product.limit_age: # Check if affordable
|
2016-08-20 00:55:48 +00:00
|
|
|
request.session['too_young'] = True
|
|
|
|
return False
|
2016-08-20 14:09:46 +00:00
|
|
|
if pid in request.session['basket']: # Add if already in basket
|
2016-06-02 12:55:12 +00:00
|
|
|
request.session['basket'][pid]['qty'] += q
|
2016-08-20 14:09:46 +00:00
|
|
|
request.session['basket'][pid]['bonus_qty'] += bq
|
|
|
|
else: # or create if not
|
|
|
|
request.session['basket'][pid] = {'qty': q, 'price': int(price*100), 'bonus_qty': bq}
|
2016-04-18 23:54:51 +00:00
|
|
|
request.session.modified = True
|
2016-06-26 17:30:28 +00:00
|
|
|
return True
|
2016-04-18 23:54:51 +00:00
|
|
|
|
|
|
|
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'])
|
2016-08-20 14:09:46 +00:00
|
|
|
product = self.get_product(pid)
|
2016-04-19 17:58:57 +00:00
|
|
|
if pid in request.session['basket']:
|
2016-08-20 14:09:46 +00:00
|
|
|
if product.tray and (self.get_total_quantity_for_pid(request, pid) % 6 == 0) and request.session['basket'][pid]['bonus_qty']:
|
|
|
|
request.session['basket'][pid]['bonus_qty'] -= 1
|
|
|
|
else:
|
|
|
|
request.session['basket'][pid]['qty'] -= 1
|
2016-06-01 22:26:31 +00:00
|
|
|
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-08-20 14:09:46 +00:00
|
|
|
request.session['basket'][pid] = None
|
2016-04-18 23:54:51 +00:00
|
|
|
request.session.modified = True
|
|
|
|
|
2016-06-26 17:30:28 +00:00
|
|
|
def parse_code(self, request):
|
|
|
|
"""Parse the string entered by the barman"""
|
|
|
|
string = str(request.POST['code']).upper()
|
2016-07-18 15:47:43 +00:00
|
|
|
if string == _("END"):
|
2016-06-26 17:30:28 +00:00
|
|
|
return self.finish(request)
|
2016-07-18 15:47:43 +00:00
|
|
|
elif string == _("CAN"):
|
2016-06-26 17:30:28 +00:00
|
|
|
return self.cancel(request)
|
|
|
|
regex = re.compile(r"^((?P<nb>[0-9]+)X)?(?P<code>[A-Z0-9]+)$")
|
|
|
|
m = regex.match(string)
|
|
|
|
if m is not None:
|
|
|
|
nb = m.group('nb')
|
|
|
|
code = m.group('code')
|
|
|
|
if nb is None:
|
|
|
|
nb = 1
|
|
|
|
else:
|
|
|
|
nb = int(nb)
|
2016-07-22 11:34:34 +00:00
|
|
|
p = self.object.products.filter(code=code).first()
|
2016-06-26 17:30:28 +00:00
|
|
|
if p is not None:
|
|
|
|
while nb > 0 and not self.add_product(request, nb, p.id):
|
|
|
|
nb -= 1
|
|
|
|
context = self.get_context_data(object=self.object)
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
2016-04-18 23:54:51 +00:00
|
|
|
def finish(self, request):
|
|
|
|
""" Finish the click session, and validate the basket """
|
2016-07-21 23:19:04 +00:00
|
|
|
with transaction.atomic():
|
|
|
|
request.session['last_basket'] = []
|
|
|
|
for pid,infos in request.session['basket'].items():
|
|
|
|
# This duplicates code for DB optimization (prevent to load many times the same object)
|
|
|
|
p = Product.objects.filter(pk=pid).first()
|
|
|
|
if self.is_barman_price():
|
|
|
|
uprice = p.special_selling_price
|
|
|
|
else:
|
|
|
|
uprice = p.selling_price
|
|
|
|
if uprice * infos['qty'] > self.customer.amount:
|
|
|
|
raise DataError(_("You have not enough money to buy all the basket"))
|
2016-08-20 14:09:46 +00:00
|
|
|
request.session['last_basket'].append("%d x %s" % (infos['qty']+infos['bonus_qty'], p.name))
|
2016-08-18 01:04:50 +00:00
|
|
|
s = Selling(label=p.name, product=p, club=p.club, counter=self.object, unit_price=uprice,
|
2016-07-22 11:34:34 +00:00
|
|
|
quantity=infos['qty'], seller=self.operator, customer=self.customer)
|
2016-07-21 23:19:04 +00:00
|
|
|
s.save()
|
2016-08-20 14:09:46 +00:00
|
|
|
if infos['bonus_qty']:
|
|
|
|
s = Selling(label=p.name + " (Plateau)", product=p, club=p.club, counter=self.object, unit_price=0,
|
|
|
|
quantity=infos['bonus_qty'], seller=self.operator, customer=self.customer)
|
|
|
|
s.save()
|
2016-07-21 23:19:04 +00:00
|
|
|
request.session['last_customer'] = self.customer.user.get_display_name()
|
|
|
|
request.session['last_total'] = "%0.2f" % self.sum_basket(request)
|
|
|
|
request.session['new_customer_amount'] = str(self.customer.amount)
|
|
|
|
del request.session['basket']
|
|
|
|
request.session.modified = True
|
|
|
|
kwargs = {
|
|
|
|
'counter_id': self.object.id,
|
|
|
|
}
|
|
|
|
return HttpResponseRedirect(reverse_lazy('counter:details', args=self.args, kwargs=kwargs))
|
2016-04-18 23:54:51 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2016-06-26 18:07:29 +00:00
|
|
|
def refill(self, request):
|
|
|
|
"""Refill the customer's account"""
|
2016-07-17 09:38:19 +00:00
|
|
|
form = RefillForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
form.instance.counter = self.object
|
2016-07-22 11:34:34 +00:00
|
|
|
form.instance.operator = self.operator
|
2016-07-17 09:38:19 +00:00
|
|
|
form.instance.customer = self.customer
|
|
|
|
form.instance.save()
|
|
|
|
else:
|
|
|
|
self.refill_form = form
|
2016-06-26 18:07:29 +00:00
|
|
|
|
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-07-17 09:38:19 +00:00
|
|
|
kwargs['refill_form'] = self.refill_form or RefillForm()
|
2016-08-20 20:12:46 +00:00
|
|
|
kwargs['categories'] = ProductType.objects.all()
|
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-08-20 14:09:46 +00:00
|
|
|
self.errors = []
|
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-07-21 18:03:31 +00:00
|
|
|
if user.is_subscribed():
|
|
|
|
Counter.add_barman(self.counter_id, user.id)
|
2016-08-20 14:09:46 +00:00
|
|
|
else:
|
|
|
|
self.errors += ["subscription"]
|
2016-04-12 08:00:47 +00:00
|
|
|
else:
|
2016-08-20 14:09:46 +00:00
|
|
|
self.errors += ["credentials"]
|
2016-04-12 08:00:47 +00:00
|
|
|
return super(CounterLogin, self).post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
2016-08-20 14:09:46 +00:00
|
|
|
return reverse_lazy('counter:details', args=args, kwargs=kwargs)+"?"+'&'.join(self.errors)
|
2016-04-12 08:00:47 +00:00
|
|
|
|
|
|
|
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-07-18 11:22:50 +00:00
|
|
|
Counter.del_barman(self.counter_id, 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-08-19 21:24:23 +00:00
|
|
|
class CounterEditForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Counter
|
|
|
|
fields = ['sellers', 'products']
|
|
|
|
sellers = make_ajax_field(Counter, 'sellers', 'users', show_help_text=False)
|
|
|
|
products = make_ajax_field(Counter, 'products', 'products')
|
|
|
|
|
|
|
|
class CounterEditView(CanEditMixin, UpdateView):
|
|
|
|
"""
|
|
|
|
Edit a counter's main informations (for the counter's manager)
|
|
|
|
"""
|
|
|
|
model = Counter
|
|
|
|
form_class = CounterEditForm
|
|
|
|
pk_url_kwarg = "counter_id"
|
|
|
|
template_name = 'counter/counter_edit.jinja'
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy('counter:admin', kwargs={'counter_id': self.object.id})
|
|
|
|
|
|
|
|
class CounterEditPropView(CanEditPropMixin, UpdateView):
|
2016-03-29 08:30:24 +00:00
|
|
|
"""
|
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
|
2016-08-19 21:24:23 +00:00
|
|
|
form_class = modelform_factory(Counter, fields=['name', 'club', 'type'])
|
2016-03-29 08:30:24 +00:00
|
|
|
pk_url_kwarg = "counter_id"
|
2016-08-18 01:04:50 +00:00
|
|
|
template_name = 'core/edit.jinja'
|
2016-03-29 08:30:24 +00:00
|
|
|
|
|
|
|
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})
|
2016-08-18 01:04:50 +00:00
|
|
|
template_name = 'core/create.jinja'
|
2016-03-29 08:30:24 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2016-07-27 15:23:02 +00:00
|
|
|
# Product management
|
|
|
|
|
2016-08-01 14:36:16 +00:00
|
|
|
class ProductTypeListView(CanEditPropMixin, ListView):
|
2016-07-27 18:05:45 +00:00
|
|
|
"""
|
|
|
|
A list view for the admins
|
|
|
|
"""
|
|
|
|
model = ProductType
|
|
|
|
template_name = 'counter/producttype_list.jinja'
|
|
|
|
|
2016-07-28 14:11:18 +00:00
|
|
|
class ProductTypeCreateView(CanCreateMixin, CreateView):
|
2016-07-27 18:05:45 +00:00
|
|
|
"""
|
|
|
|
A create view for the admins
|
|
|
|
"""
|
|
|
|
model = ProductType
|
|
|
|
fields = ['name', 'description', 'icon']
|
|
|
|
template_name = 'core/create.jinja'
|
|
|
|
|
2016-07-28 14:11:18 +00:00
|
|
|
class ProductTypeEditView(CanEditPropMixin, UpdateView):
|
2016-07-27 18:05:45 +00:00
|
|
|
"""
|
|
|
|
An edit view for the admins
|
|
|
|
"""
|
|
|
|
model = ProductType
|
|
|
|
template_name = 'core/edit.jinja'
|
|
|
|
fields = ['name', 'description', 'icon']
|
|
|
|
pk_url_kwarg = "type_id"
|
|
|
|
|
2016-08-01 14:36:16 +00:00
|
|
|
class ProductListView(CanEditPropMixin, ListView):
|
2016-07-27 15:23:02 +00:00
|
|
|
"""
|
|
|
|
A list view for the admins
|
|
|
|
"""
|
|
|
|
model = Product
|
|
|
|
template_name = 'counter/product_list.jinja'
|
|
|
|
|
2016-08-20 20:12:46 +00:00
|
|
|
class ProductEditForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Product
|
|
|
|
fields = ['name', 'description', 'product_type', 'code', 'parent_product', 'buying_groups', 'purchase_price',
|
|
|
|
'selling_price', 'special_selling_price', 'icon', 'club', 'limit_age', 'tray']
|
|
|
|
parent_product = AutoCompleteSelectField('products', show_help_text=False, label=_("Parent product"), required=False)
|
|
|
|
buying_groups = AutoCompleteSelectMultipleField('groups', show_help_text=False, help_text="", label=_("Buying groups"))
|
|
|
|
club = AutoCompleteSelectField('clubs', show_help_text=False)
|
|
|
|
counters = AutoCompleteSelectMultipleField('counters', show_help_text=False, help_text="", label=_("Counters"), required=False)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(ProductEditForm, self).__init__(*args, **kwargs)
|
|
|
|
if self.instance.id:
|
|
|
|
self.fields['counters'].initial = [str(c.id) for c in self.instance.counters.all()]
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
ret = super(ProductEditForm, self).save(*args, **kwargs)
|
|
|
|
if self.fields['counters'].initial:
|
|
|
|
for cid in self.fields['counters'].initial:
|
|
|
|
c = Counter.objects.filter(id=int(cid)).first()
|
|
|
|
c.products.remove(self.instance)
|
|
|
|
c.save()
|
|
|
|
for cid in self.cleaned_data['counters']:
|
|
|
|
c = Counter.objects.filter(id=int(cid)).first()
|
|
|
|
c.products.add(self.instance)
|
|
|
|
c.save()
|
|
|
|
return ret
|
|
|
|
|
2016-07-28 14:11:18 +00:00
|
|
|
class ProductCreateView(CanCreateMixin, CreateView):
|
2016-07-27 15:23:02 +00:00
|
|
|
"""
|
|
|
|
A create view for the admins
|
|
|
|
"""
|
|
|
|
model = Product
|
2016-08-20 20:12:46 +00:00
|
|
|
form_class = ProductEditForm
|
2016-07-27 18:05:45 +00:00
|
|
|
template_name = 'core/create.jinja'
|
2016-07-27 15:23:02 +00:00
|
|
|
|
2016-07-28 14:11:18 +00:00
|
|
|
class ProductEditView(CanEditPropMixin, UpdateView):
|
2016-07-27 15:23:02 +00:00
|
|
|
"""
|
|
|
|
An edit view for the admins
|
|
|
|
"""
|
|
|
|
model = Product
|
2016-08-19 21:24:23 +00:00
|
|
|
form_class = ProductEditForm
|
2016-07-27 15:23:02 +00:00
|
|
|
pk_url_kwarg = "product_id"
|
|
|
|
template_name = 'core/edit.jinja'
|
2016-08-18 19:06:10 +00:00
|
|
|
|
|
|
|
class RefillingDeleteView(CanEditPropMixin, DeleteView):
|
|
|
|
"""
|
|
|
|
Delete a refilling (for the admins)
|
|
|
|
"""
|
|
|
|
model = Refilling
|
|
|
|
pk_url_kwarg = "refilling_id"
|
|
|
|
template_name = 'core/delete_confirm.jinja'
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy('core:user_account', kwargs={'user_id': self.object.customer.user.id})
|
|
|
|
|
|
|
|
class SellingDeleteView(CanEditPropMixin, DeleteView):
|
|
|
|
"""
|
|
|
|
Delete a selling (for the admins)
|
|
|
|
"""
|
|
|
|
model = Selling
|
|
|
|
pk_url_kwarg = "selling_id"
|
|
|
|
template_name = 'core/delete_confirm.jinja'
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy('core:user_account', kwargs={'user_id': self.object.customer.user.id})
|
|
|
|
|