Commit 25c1e6dc authored by Skia's avatar Skia 🤘
Browse files

Add the basket handling in the click view of the counter

Now there is still to handle the validation by generating the appropriate invoice(s)
parent 7a9689a2
Pipeline #15 passed with stage
{% extends "core/base.jinja" %}
{% macro barman_logout_link(user) %}
<form method="post" action="{{ url('counter:logout', counter_id=counter.id) }}" class="inline">
{% macro add_product(id, content) %}
<form method="post" action="{{ url('counter:click', counter_id=counter.id, user_id=customer.user.id) }}" class="inline" style="display:inline">
{% csrf_token %}
<input type="hidden" name="user_id" value="{{ user.id }}">
<button type="submit" name="submit_param" value="submit_value" class="link-button">{{ user.get_display_name() }}</button>
<input type="hidden" name="action" value="add_product">
<button type="submit" name="product_id" value="{{ id }}"> {{ content }} </button>
</form>
{% endmacro %}
{% macro del_product(id, content) %}
<form method="post" action="{{ url('counter:click', counter_id=counter.id, user_id=customer.user.id) }}" class="inline" style="display:inline">
{% csrf_token %}
<input type="hidden" name="action" value="del_product">
<button type="submit" name="product_id" value="{{ id }}"> {{ content }} </button>
</form>
{% endmacro %}
......@@ -12,15 +20,30 @@
<h3>Counter</h3>
<h4>{{ counter }}</h4>
<p><strong>Club: </strong> {{ counter.club }}</p>
<p><strong>Products: </strong> {{ counter.products.all() }}</p>
<div>
Customer: {{ customer }}
<p>Customer: {{ customer }}</p>
<p>Basket: </p>
<ul>
{% for id,qte in request.session['basket'].items() %}
<li>{{ del_product(id, '-') }} {{ qte }} {{ add_product(id, '+') }} {{ counter.products.filter(id=id).first().name }}</li>
{% endfor %}
</ul>
<form method="post" action="{{ url('counter:click', counter_id=counter.id, user_id=customer.user.id) }}">
{% csrf_token %}
<input type="hidden" name="action" value="finish">
<input type="submit" value="Finish" />
</form>
<form method="post" action="{{ url('counter:click', counter_id=counter.id, user_id=customer.user.id) }}">
{% csrf_token %}
{{ form.as_p() }}
<input type="submit" value="CLICK" />
<input type="hidden" name="action" value="cancel">
<input type="submit" value="Cancel" />
</form>
<p><strong>Products: </strong>
{% for p in counter.products.all() %}
{{ add_product(p.id, p.name) }}
{% endfor %}
</p>
</div>
{% endblock %}
......
......@@ -5,6 +5,7 @@ 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.http import HttpResponseRedirect
from django.utils import timezone
from django.conf import settings
from django import forms
......@@ -100,44 +101,78 @@ class BasketForm(forms.Form):
total += (q or 0)*p.selling_price
print(total)
class CounterClick(DetailView, ProcessFormView, FormMixin):
class CounterClick(DetailView):
"""
The click view
This is a detail view not to have to worry about loading the counter
Everything is made by hand in the post method
"""
model = Counter
template_name = 'counter/counter_click.jinja'
pk_url_kwarg = "counter_id"
form_class = BasketForm
prefix = "prod"
def get(self, request, *args, **kwargs):
"""Simple get view"""
self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first()
if 'basket' not in request.session.keys():
request.session['basket'] = {}
return super(CounterClick, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
""" Handle the many possibilities of the post request """
self.object = self.get_object()
self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first()
return super(CounterClick, self).post(request, *args, **kwargs)
def form_valid(self, form):
return super(CounterClick, self).form_valid(form)
def get_form_kwargs(self):
kwargs = super(CounterClick, self).get_form_kwargs()
kwargs['initial'].update({'counter': self.object, 'customer': self.customer})
return kwargs
if 'basket' not in request.session.keys():
request.session['basket'] = {}
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)
def add_product(self, request):
""" Add a product to the basket """
if str(request.POST['product_id']) in request.session['basket']:
request.session['basket'][str(request.POST['product_id'])] += 1
else:
request.session['basket'][str(request.POST['product_id'])] = 1
request.session.modified = True
def del_product(self, request):
""" Delete a product from the basket """
if str(request.POST['product_id']) in request.session['basket']:
request.session['basket'][str(request.POST['product_id'])] -= 1
if request.session['basket'][str(request.POST['product_id'])] <= 0:
del request.session['basket'][str(request.POST['product_id'])]
else:
request.session['basket'][str(request.POST['product_id'])] = 0
request.session.modified = True
def finish(self, request):
""" Finish the click session, and validate the basket """
# TODO: handle the basket
kwargs = {'counter_id': self.object.id}
del request.session['basket']
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}
del request.session['basket']
return HttpResponseRedirect(reverse_lazy('counter:details', args=self.args, kwargs=kwargs))
def get_context_data(self, **kwargs):
"""
"""
""" Add customer to the context """
kwargs = super(CounterClick, self).get_context_data(**kwargs)
kwargs['customer'] = self.customer
kwargs['form'] = self.get_form()
return kwargs
def get_success_url(self):
return reverse_lazy('counter:click', args=self.args, kwargs=self.kwargs)
class CounterLogin(RedirectView):
"""
Handle the login of a barman
......
......@@ -57,6 +57,8 @@ MIDDLEWARE_CLASSES = (
'core.middleware.AuthenticationMiddleware',
)
SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'
ROOT_URLCONF = 'sith.urls'
TEMPLATES = [
......
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