Add atomic transaction in counters

This commit is contained in:
Skia 2016-07-22 01:19:04 +02:00
parent 1f3e186e27
commit 525d7e6709
2 changed files with 28 additions and 23 deletions

View File

@ -6,6 +6,7 @@
{% block infos %} {% block infos %}
<h3>{% trans %}User account{% endtrans %}</h3> <h3>{% trans %}User account{% endtrans %}</h3>
<p>{% trans %}Amount: {% endtrans %}{{ customer.amount }} €</p>
<p>{{ customer.refillings.all() }}</p> <p>{{ customer.refillings.all() }}</p>
<p>{{ customer.buyings.all() }}</p> <p>{{ customer.buyings.all() }}</p>
{% endblock %} {% endblock %}

View File

@ -10,6 +10,7 @@ from django.utils import timezone
from django import forms from django import forms
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.conf import settings from django.conf import settings
from django.db import DataError, transaction
import re import re
@ -218,31 +219,34 @@ class CounterClick(DetailView):
def finish(self, request): def finish(self, request):
""" Finish the click session, and validate the basket """ """ Finish the click session, and validate the basket """
if self.is_barman_price(): with transaction.atomic():
seller = self.customer.user
else:
seller = Counter.get_random_barman(self.object.id)
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(): if self.is_barman_price():
uprice = p.special_selling_price seller = self.customer.user
else: else:
uprice = p.selling_price seller = Counter.get_random_barman(self.object.id)
request.session['last_basket'].append("%d x %s" % (infos['qty'], p.name)) request.session['last_basket'] = []
s = Selling(product=p, counter=self.object, unit_price=uprice, for pid,infos in request.session['basket'].items():
quantity=infos['qty'], seller=seller, customer=self.customer) # This duplicates code for DB optimization (prevent to load many times the same object)
s.save() p = Product.objects.filter(pk=pid).first()
request.session['last_customer'] = self.customer.user.get_display_name() if self.is_barman_price():
request.session['last_total'] = "%0.2f" % self.sum_basket(request) uprice = p.special_selling_price
request.session['new_customer_amount'] = str(self.customer.amount) else:
del request.session['basket'] uprice = p.selling_price
request.session.modified = True if uprice * infos['qty'] > self.customer.amount:
kwargs = { raise DataError(_("You have not enough money to buy all the basket"))
'counter_id': self.object.id, request.session['last_basket'].append("%d x %s" % (infos['qty'], p.name))
} s = Selling(product=p, counter=self.object, unit_price=uprice,
return HttpResponseRedirect(reverse_lazy('counter:details', args=self.args, kwargs=kwargs)) quantity=infos['qty'], seller=seller, customer=self.customer)
s.save()
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))
def cancel(self, request): def cancel(self, request):
""" Cancel the click session """ """ Cancel the click session """