mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-22 14:13:21 +00:00
Improve Eboutic basket
This commit is contained in:
parent
c099f1c5d7
commit
45f5a58b4a
26
eboutic/migrations/0004_auto_20160726_1902.py
Normal file
26
eboutic/migrations/0004_auto_20160726_1902.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('eboutic', '0003_auto_20160726_1708'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='basketitem',
|
||||||
|
name='product_id',
|
||||||
|
field=models.IntegerField(verbose_name='product id', default=0),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='invoiceitem',
|
||||||
|
name='product_id',
|
||||||
|
field=models.IntegerField(verbose_name='product id', default=0),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
]
|
@ -7,17 +7,37 @@ from core.models import User
|
|||||||
|
|
||||||
class Basket(models.Model):
|
class Basket(models.Model):
|
||||||
"""
|
"""
|
||||||
Basket is built when the user validate its session basket and asks for payment
|
Basket is built when the user connects to an eboutic page
|
||||||
"""
|
"""
|
||||||
user = models.ForeignKey(User, related_name='baskets', verbose_name=_('user'), blank=False)
|
user = models.ForeignKey(User, related_name='baskets', verbose_name=_('user'), blank=False)
|
||||||
date = models.DateTimeField(_('date'), auto_now=True)
|
date = models.DateTimeField(_('date'), auto_now=True)
|
||||||
|
|
||||||
|
def add_product(self, p, q = 1):
|
||||||
|
item = self.items.filter(product_id=p.id).first()
|
||||||
|
if item is None:
|
||||||
|
BasketItem(basket=self, product_id=p.id, product_name=p.name, type=p.product_type.name,
|
||||||
|
quantity=q, product_unit_price=p.selling_price).save()
|
||||||
|
else:
|
||||||
|
item.quantity += q
|
||||||
|
item.save()
|
||||||
|
|
||||||
|
def del_product(self, p, q = 1):
|
||||||
|
item = self.items.filter(product_id=p.id).first()
|
||||||
|
if item is not None:
|
||||||
|
item.quantity -= q
|
||||||
|
item.save()
|
||||||
|
if item.quantity <= 0:
|
||||||
|
item.delete()
|
||||||
|
|
||||||
def get_total(self):
|
def get_total(self):
|
||||||
total = 0
|
total = 0
|
||||||
for i in self.items.all():
|
for i in self.items.all():
|
||||||
total += i.quantity * i.product_unit_price
|
total += i.quantity * i.product_unit_price
|
||||||
return total
|
return total
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Basket (%d items)" % self.items.all().count()
|
||||||
|
|
||||||
class Invoice(models.Model):
|
class Invoice(models.Model):
|
||||||
"""
|
"""
|
||||||
Invoices are generated once the payment has been validated
|
Invoices are generated once the payment has been validated
|
||||||
@ -49,6 +69,7 @@ class Invoice(models.Model):
|
|||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
class AbstractBaseItem(models.Model):
|
class AbstractBaseItem(models.Model):
|
||||||
|
product_id = models.IntegerField(_('product id'))
|
||||||
product_name = models.CharField(_('product name'), max_length=255)
|
product_name = models.CharField(_('product name'), max_length=255)
|
||||||
type = models.CharField(_('product type'), max_length=255)
|
type = models.CharField(_('product type'), max_length=255)
|
||||||
product_unit_price = CurrencyField(_('unit price'))
|
product_unit_price = CurrencyField(_('unit price'))
|
||||||
|
@ -22,13 +22,12 @@
|
|||||||
<div id="basket">
|
<div id="basket">
|
||||||
<p>{% trans %}Basket: {% endtrans %}</p>
|
<p>{% trans %}Basket: {% endtrans %}</p>
|
||||||
<ul>
|
<ul>
|
||||||
{% for id,infos in request.session['basket']|dictsort %}
|
{% for i in basket.items.all()|sort(attribute='id') %}
|
||||||
{% set product = eboutic.products.filter(id=id).first() %}
|
<li>{{ del_product(i.product_id, '-') }} {{ i.quantity }}
|
||||||
{% set s = infos['qty'] * infos['price'] / 100 %}
|
{{ add_product(i.product_id, '+') }} {{ i.product_name }}: {{ "%0.2f"|format(i.product_unit_price*i.quantity) }} €</li>
|
||||||
<li>{{ del_product(id, '-') }} {{ infos['qty'] }} {{ add_product(id, '+') }} {{ product.name }}: {{ "%0.2f"|format(s) }} €</li>
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
<p><strong>{% trans %}Total: {% endtrans %}{{ "%0.2f"|format(basket_total) }} €</strong></p>
|
<p><strong>{% trans %}Total: {% endtrans %}{{ "%0.2f"|format(basket.get_total()) }} €</strong></p>
|
||||||
<form method="post" action="{{ url('eboutic:command') }}">
|
<form method="post" action="{{ url('eboutic:command') }}">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<input type="submit" value="{% trans %}Proceed to command{% endtrans %}" />
|
<input type="submit" value="{% trans %}Proceed to command{% endtrans %}" />
|
||||||
|
@ -23,17 +23,22 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
<tbody>
|
<tbody>
|
||||||
</table>
|
</table>
|
||||||
|
<p><strong>Total: </strong>{{ basket.get_total() }} €</p>
|
||||||
<form method="post" action="{{ settings.SITH_EBOUTIC_ET_URL }}">
|
<form method="post" action="{{ settings.SITH_EBOUTIC_ET_URL }}">
|
||||||
{% for (field_name,field_value) in et_request.items() -%}
|
{% for (field_name,field_value) in et_request.items() -%}
|
||||||
<input type="hidden" name="{{ field_name }}" value="{{ field_value }}">
|
<input type="hidden" name="{{ field_name }}" value="{{ field_value }}">
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<input type="submit" value="{% trans %}Pay with credit card{% endtrans %}" />
|
<input type="submit" value="{% trans %}Pay with credit card{% endtrans %}" />
|
||||||
</form>
|
</form>
|
||||||
|
{% if basket.items.filter(type="REFILLING").exists() %}
|
||||||
|
<p>{% trans %}AE account payment disabled because your basket contains refilling items.{% endtrans %}</p>
|
||||||
|
{% else %}
|
||||||
<form method="post" action="{{ url('eboutic:pay_with_sith') }}">
|
<form method="post" action="{{ url('eboutic:pay_with_sith') }}">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<input type="hidden" name="action" value="pay_with_sith_account">
|
<input type="hidden" name="action" value="pay_with_sith_account">
|
||||||
<input type="submit" value="{% trans %}Pay with Sith account{% endtrans %}" />
|
<input type="submit" value="{% trans %}Pay with Sith account{% endtrans %}" />
|
||||||
</form>
|
</form>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
{% else %}
|
{% else %}
|
||||||
{% trans %}Payment successful{% endtrans %}
|
{% trans %}Payment successful{% endtrans %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
<p><a href="{{ url('eboutic:main') }}">{% trans %}Return to eboutic{% endtrans %}</a></p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
102
eboutic/views.py
102
eboutic/views.py
@ -21,60 +21,54 @@ from eboutic.models import Basket, Invoice, BasketItem, InvoiceItem
|
|||||||
class EbouticMain(TemplateView):
|
class EbouticMain(TemplateView):
|
||||||
template_name = 'eboutic/eboutic_main.jinja'
|
template_name = 'eboutic/eboutic_main.jinja'
|
||||||
|
|
||||||
def sum_basket(request):
|
def make_basket(self, request):
|
||||||
total = 0
|
if 'basket_id' not in request.session.keys(): # Init the basket session entry
|
||||||
for pid,infos in request.session['basket'].items():
|
self.basket = Basket(user=request.user)
|
||||||
total += infos['price'] * infos['qty']
|
self.basket.save()
|
||||||
return total / 100
|
else:
|
||||||
|
self.basket = Basket.objects.filter(id=request.session['basket_id']).first()
|
||||||
|
if self.basket is None:
|
||||||
|
self.basket = Basket(user=request.user)
|
||||||
|
self.basket.save()
|
||||||
|
request.session['basket_id'] = self.basket.id
|
||||||
|
request.session.modified = True
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
if 'basket' not in request.session.keys(): # Init the basket session entry
|
if not request.user.is_authenticated():
|
||||||
request.session['basket'] = {}
|
return HttpResponseRedirect(reverse_lazy('core:login', args=self.args, kwargs=kwargs))
|
||||||
request.session['basket_total'] = 0
|
self.make_basket(request)
|
||||||
return super(EbouticMain, self).get(request, *args, **kwargs)
|
return super(EbouticMain, self).get(request, *args, **kwargs)
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
if 'basket' not in request.session.keys(): # Init the basket session entry
|
if not request.user.is_authenticated():
|
||||||
request.session['basket'] = {}
|
return HttpResponseRedirect(reverse_lazy('core:login', args=self.args, kwargs=kwargs))
|
||||||
request.session['basket_total'] = 0
|
self.make_basket(request)
|
||||||
if 'add_product' in request.POST['action']:
|
if 'add_product' in request.POST['action']:
|
||||||
self.add_product(request)
|
self.add_product(request)
|
||||||
elif 'del_product' in request.POST['action']:
|
elif 'del_product' in request.POST['action']:
|
||||||
self.del_product(request)
|
self.del_product(request)
|
||||||
elif 'pay' in request.POST['action']:
|
|
||||||
self.ask_payment(request)
|
|
||||||
return self.render_to_response(self.get_context_data(**kwargs))
|
return self.render_to_response(self.get_context_data(**kwargs))
|
||||||
|
|
||||||
def get_price(self, pid):
|
|
||||||
return Product.objects.filter(pk=pid).first().selling_price
|
|
||||||
|
|
||||||
def add_product(self, request, q = 1, p=None):
|
def add_product(self, request):
|
||||||
""" Add a product to the basket """
|
""" Add a product to the basket """
|
||||||
pid = p or request.POST['product_id']
|
try:
|
||||||
pid = str(pid)
|
p = Product.objects.filter(id=int(request.POST['product_id'])).first()
|
||||||
price = self.get_price(pid)
|
self.basket.add_product(p)
|
||||||
total = EbouticMain.sum_basket(request)
|
except:
|
||||||
if pid in request.session['basket']:
|
pass
|
||||||
request.session['basket'][pid]['qty'] += q
|
|
||||||
else:
|
|
||||||
request.session['basket'][pid] = {'qty': q, 'price': int(price*100)}
|
|
||||||
request.session.modified = True
|
|
||||||
return True
|
|
||||||
|
|
||||||
def del_product(self, request):
|
def del_product(self, request):
|
||||||
""" Delete a product from the basket """
|
""" Delete a product from the basket """
|
||||||
pid = str(request.POST['product_id'])
|
try:
|
||||||
if pid in request.session['basket']:
|
p = Product.objects.filter(id=int(request.POST['product_id'])).first()
|
||||||
request.session['basket'][pid]['qty'] -= 1
|
self.basket.del_product(p)
|
||||||
if request.session['basket'][pid]['qty'] <= 0:
|
except:
|
||||||
del request.session['basket'][pid]
|
pass
|
||||||
else:
|
|
||||||
request.session['basket'][pid] = 0
|
|
||||||
request.session.modified = True
|
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
kwargs = super(EbouticMain, self).get_context_data(**kwargs)
|
kwargs = super(EbouticMain, self).get_context_data(**kwargs)
|
||||||
kwargs['basket_total'] = EbouticMain.sum_basket(self.request)
|
kwargs['basket'] = self.basket
|
||||||
kwargs['eboutic'] = Counter.objects.filter(type="EBOUTIC").first()
|
kwargs['eboutic'] = Counter.objects.filter(type="EBOUTIC").first()
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
@ -82,35 +76,22 @@ class EbouticCommand(TemplateView):
|
|||||||
template_name = 'eboutic/eboutic_makecommand.jinja'
|
template_name = 'eboutic/eboutic_makecommand.jinja'
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
|
if not request.user.is_authenticated():
|
||||||
|
return HttpResponseRedirect(reverse_lazy('core:login', args=self.args, kwargs=kwargs))
|
||||||
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
|
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
if not request.user.is_authenticated():
|
if not request.user.is_authenticated():
|
||||||
return HttpResponseRedirect(reverse_lazy('core:login', args=self.args, kwargs=kwargs))
|
return HttpResponseRedirect(reverse_lazy('core:login', args=self.args, kwargs=kwargs))
|
||||||
if 'basket' not in request.session.keys():
|
if 'basket_id' not in request.session.keys():
|
||||||
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
|
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
|
||||||
if self.make_basket(request):
|
self.basket = Basket.objects.filter(id=request.session['basket_id']).first()
|
||||||
|
if self.basket is None:
|
||||||
|
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
|
||||||
|
else:
|
||||||
kwargs['basket'] = self.basket
|
kwargs['basket'] = self.basket
|
||||||
return self.render_to_response(self.get_context_data(**kwargs))
|
return self.render_to_response(self.get_context_data(**kwargs))
|
||||||
|
|
||||||
def make_basket(self, request):
|
|
||||||
b = None
|
|
||||||
if 'basket_id' in request.session.keys():
|
|
||||||
b = Basket.objects.filter(id=request.session['basket_id']).first()
|
|
||||||
if b is None:
|
|
||||||
b = Basket()
|
|
||||||
b.user = request.user
|
|
||||||
b.save()
|
|
||||||
request.session['basket_id'] = b.id
|
|
||||||
request.session.modified = True
|
|
||||||
b.items.all().delete()
|
|
||||||
for pid,infos in request.session['basket'].items():
|
|
||||||
p = Product.objects.filter(id=int(pid)).first()
|
|
||||||
BasketItem(basket=b, product_name=p.name, type=p.product_type.name,
|
|
||||||
quantity=infos['qty'], product_unit_price=infos['price']/100).save()
|
|
||||||
self.basket = b
|
|
||||||
return True
|
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
kwargs = super(EbouticCommand, self).get_context_data(**kwargs)
|
kwargs = super(EbouticCommand, self).get_context_data(**kwargs)
|
||||||
kwargs['et_request'] = OrderedDict()
|
kwargs['et_request'] = OrderedDict()
|
||||||
@ -140,7 +121,7 @@ class EbouticPayWithSith(TemplateView):
|
|||||||
if 'basket_id' not in request.session.keys() or not request.user.is_authenticated():
|
if 'basket_id' not in request.session.keys() or not request.user.is_authenticated():
|
||||||
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
|
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
|
||||||
b = Basket.objects.filter(id=request.session['basket_id']).first()
|
b = Basket.objects.filter(id=request.session['basket_id']).first()
|
||||||
if b is None:
|
if b is None or b.items.filter(type="REFILLING").exists():
|
||||||
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
|
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
|
||||||
c = Customer.objects.filter(user__id=b.user.id).first()
|
c = Customer.objects.filter(user__id=b.user.id).first()
|
||||||
if c is None:
|
if c is None:
|
||||||
@ -154,12 +135,12 @@ class EbouticPayWithSith(TemplateView):
|
|||||||
i.payment_method = "SITH_ACCOUNT"
|
i.payment_method = "SITH_ACCOUNT"
|
||||||
i.save()
|
i.save()
|
||||||
for it in b.items.all():
|
for it in b.items.all():
|
||||||
InvoiceItem(invoice=i, product_name=it.product_name, type=it.type,
|
InvoiceItem(invoice=i, product_id=it.product_id, product_name=it.product_name, type=it.type,
|
||||||
product_unit_price=it.product_unit_price, quantity=it.quantity).save()
|
product_unit_price=it.product_unit_price, quantity=it.quantity).save()
|
||||||
i.validate()
|
i.validate()
|
||||||
|
b.delete()
|
||||||
kwargs['not_enough'] = False
|
kwargs['not_enough'] = False
|
||||||
request.session.pop('basket_id', None)
|
request.session.pop('basket_id', None)
|
||||||
request.session.pop('basket', None)
|
|
||||||
except DataError as e:
|
except DataError as e:
|
||||||
kwargs['not_enough'] = True
|
kwargs['not_enough'] = True
|
||||||
return self.render_to_response(self.get_context_data(**kwargs))
|
return self.render_to_response(self.get_context_data(**kwargs))
|
||||||
@ -190,10 +171,11 @@ class EtransactionAutoAnswer(View):
|
|||||||
i.payment_method = "CREDIT_CARD"
|
i.payment_method = "CREDIT_CARD"
|
||||||
i.save()
|
i.save()
|
||||||
for it in b.items.all():
|
for it in b.items.all():
|
||||||
InvoiceItem(invoice=i, product_name=it.product_name, type=it.type,
|
InvoiceItem(invoice=i, product_id=it.product_id, product_name=it.product_name, type=it.type,
|
||||||
product_unit_price=it.product_unit_price, quantity=it.quantity).save()
|
product_unit_price=it.product_unit_price, quantity=it.quantity).save()
|
||||||
i.validate()
|
i.validate()
|
||||||
b.delete()
|
b.delete()
|
||||||
return HttpResponse("Payment validated")
|
return HttpResponse()
|
||||||
else:
|
else:
|
||||||
return HttpResponse("Payment failed with error: "+request.GET['Error'])
|
return HttpResponse("Payment failed with error: "+request.GET['Error'])
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user