views.py 5.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
from django.shortcuts import render

from django.core.urlresolvers import reverse_lazy
from django.views.generic import TemplateView, View
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.db import transaction, DataError
from django.utils.translation import ugettext as _

Skia's avatar
Skia committed
10
11
from counter.models import Product, Customer, Counter
from eboutic.models import Basket, Invoice, BasketItem, InvoiceItem
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

# Create your views here.
class EbouticMain(TemplateView):
    template_name = 'eboutic/eboutic_main.jinja'

    def sum_basket(request):
        total = 0
        for pid,infos in request.session['basket'].items():
            total += infos['price'] * infos['qty']
        return total / 100

    def get(self, request, *args, **kwargs):
        if 'basket' not in request.session.keys(): # Init the basket session entry
            request.session['basket'] = {}
            request.session['basket_total'] = 0
        return super(EbouticMain, self).get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        if 'basket' not in request.session.keys(): # Init the basket session entry
            request.session['basket'] = {}
            request.session['basket_total'] = 0
        if 'add_product' in request.POST['action']:
            self.add_product(request)
        elif 'del_product' in request.POST['action']:
            self.del_product(request)
        elif 'pay' in request.POST['action']:
            self.ask_payment(request)
        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):
        """ Add a product to the basket """
        pid = p or request.POST['product_id']
        pid = str(pid)
        price = self.get_price(pid)
        total = EbouticMain.sum_basket(request)
        if pid in request.session['basket']:
            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):
        """ Delete a product from the basket """
        pid = str(request.POST['product_id'])
        if pid in request.session['basket']:
            request.session['basket'][pid]['qty'] -= 1
            if request.session['basket'][pid]['qty'] <= 0:
                del request.session['basket'][pid]
        else:
            request.session['basket'][pid] = 0
        request.session.modified = True

    def get_context_data(self, **kwargs):
        kwargs = super(EbouticMain, self).get_context_data(**kwargs)
        kwargs['basket_total'] = EbouticMain.sum_basket(self.request)
Skia's avatar
Skia committed
71
        kwargs['eboutic'] = Counter.objects.filter(type="EBOUTIC").first()
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
        return kwargs

class EbouticCommand(TemplateView):
    template_name = 'eboutic/eboutic_makecommand.jinja'

    def post(self, request, *args, **kwargs):
        if 'basket' not in request.session.keys():
            return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
        if 'ask_payment' in request.POST['action']:
            if self.ask_payment(request):
                kwargs['basket'] = self.basket
        return self.render_to_response(self.get_context_data(**kwargs))

    def ask_payment(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():
            BasketItem(basket=b, product_name=Product.objects.filter(id=int(pid)).first().name,
                    quantity=infos['qty'], product_unit_price=infos['price']/100).save()
        self.basket = b
        return True

class EbouticPayWithSith(TemplateView):
    template_name = 'eboutic/eboutic_payment_result.jinja'

    def post(self, request, *args, **kwargs):
        try:
            with transaction.atomic():
                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))
                b = Basket.objects.filter(id=request.session['basket_id']).first()
                c = Customer.objects.filter(user__id=request.user.id).first()
                if b is None or c is None:
                    return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
                kwargs['not_enough'] = True
                if c.amount < b.get_total():
                    raise DataError(_("You have not enough money to buy the basket"))
                else:
                    i = Invoice()
                    i.user = b.user
                    i.payment_method = "SITH_ACCOUNT"
                    i.save()
                    for it in b.items.all():
                        InvoiceItem(invoice=i, product_name=it.product_name,
                                product_unit_price=it.product_unit_price, quantity=it.quantity).save()
                    i.validate()
                    kwargs['not_enough'] = False
                    request.session.pop('basket_id', None)
                    request.session.pop('basket', None)
        except DataError as e:
                kwargs['not_enough'] = True
        return self.render_to_response(self.get_context_data(**kwargs))

class EtransactionAutoAnswer(View):
    def get(self, request, *args, **kwargs): # TODO implement CA's API
        return HttpResponse()