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)
This commit is contained in:
Skia 2016-04-19 01:54:51 +02:00
parent 7a9689a20d
commit 25c1e6dc58
3 changed files with 84 additions and 24 deletions

View File

@ -1,10 +1,18 @@
{% extends "core/base.jinja" %} {% extends "core/base.jinja" %}
{% macro barman_logout_link(user) %} {% macro add_product(id, content) %}
<form method="post" action="{{ url('counter:logout', counter_id=counter.id) }}" class="inline"> <form method="post" action="{{ url('counter:click', counter_id=counter.id, user_id=customer.user.id) }}" class="inline" style="display:inline">
{% csrf_token %} {% csrf_token %}
<input type="hidden" name="user_id" value="{{ user.id }}"> <input type="hidden" name="action" value="add_product">
<button type="submit" name="submit_param" value="submit_value" class="link-button">{{ user.get_display_name() }}</button> <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> </form>
{% endmacro %} {% endmacro %}
@ -12,15 +20,30 @@
<h3>Counter</h3> <h3>Counter</h3>
<h4>{{ counter }}</h4> <h4>{{ counter }}</h4>
<p><strong>Club: </strong> {{ counter.club }}</p> <p><strong>Club: </strong> {{ counter.club }}</p>
<p><strong>Products: </strong> {{ counter.products.all() }}</p>
<div> <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) }}"> <form method="post" action="{{ url('counter:click', counter_id=counter.id, user_id=customer.user.id) }}">
{% csrf_token %} {% csrf_token %}
{{ form.as_p() }} <input type="hidden" name="action" value="finish">
<input type="submit" value="CLICK" /> <input type="submit" value="Finish" />
</form> </form>
<form method="post" action="{{ url('counter:click', counter_id=counter.id, user_id=customer.user.id) }}">
{% csrf_token %}
<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> </div>
{% endblock %} {% endblock %}

View File

@ -5,6 +5,7 @@ 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.http import HttpResponseRedirect
from django.utils import timezone from django.utils import timezone
from django.conf import settings from django.conf import settings
from django import forms from django import forms
@ -100,44 +101,78 @@ class BasketForm(forms.Form):
total += (q or 0)*p.selling_price total += (q or 0)*p.selling_price
print(total) print(total)
class CounterClick(DetailView, ProcessFormView, FormMixin): class CounterClick(DetailView):
""" """
The click view 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 model = Counter
template_name = 'counter/counter_click.jinja' template_name = 'counter/counter_click.jinja'
pk_url_kwarg = "counter_id" pk_url_kwarg = "counter_id"
form_class = BasketForm
prefix = "prod"
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
"""Simple get view"""
self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first() 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) return super(CounterClick, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
""" Handle the many possibilities of the post request """
self.object = self.get_object() self.object = self.get_object()
self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first() self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first()
return super(CounterClick, self).post(request, *args, **kwargs) if 'basket' not in request.session.keys():
request.session['basket'] = {}
def form_valid(self, form): if 'add_product' in request.POST['action']:
return super(CounterClick, self).form_valid(form) 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 get_form_kwargs(self): def add_product(self, request):
kwargs = super(CounterClick, self).get_form_kwargs() """ Add a product to the basket """
kwargs['initial'].update({'counter': self.object, 'customer': self.customer}) if str(request.POST['product_id']) in request.session['basket']:
return kwargs 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): def get_context_data(self, **kwargs):
""" """ Add customer to the context """
"""
kwargs = super(CounterClick, self).get_context_data(**kwargs) kwargs = super(CounterClick, self).get_context_data(**kwargs)
kwargs['customer'] = self.customer kwargs['customer'] = self.customer
kwargs['form'] = self.get_form()
return kwargs return kwargs
def get_success_url(self):
return reverse_lazy('counter:click', args=self.args, kwargs=self.kwargs)
class CounterLogin(RedirectView): class CounterLogin(RedirectView):
""" """
Handle the login of a barman Handle the login of a barman

View File

@ -57,6 +57,8 @@ MIDDLEWARE_CLASSES = (
'core.middleware.AuthenticationMiddleware', 'core.middleware.AuthenticationMiddleware',
) )
SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'
ROOT_URLCONF = 'sith.urls' ROOT_URLCONF = 'sith.urls'
TEMPLATES = [ TEMPLATES = [