order_ajax.py 2.87 KB
Newer Older
Patrick's avatar
Patrick committed
1 2 3 4 5 6 7 8 9 10 11 12 13
# -*- coding: utf-8
from __future__ import unicode_literals

import json

from django.core.serializers.json import DjangoJSONEncoder
from django.http import Http404
from django.http import HttpResponse
from django.views.decorators.cache import never_cache
from django.views.decorators.http import require_GET

from repanier.const import PERMANENCE_OPENED, DECIMAL_ZERO
from repanier.models import Customer, ProducerInvoice, CustomerInvoice, Purchase, OfferItem
Patrick's avatar
Patrick committed
14
from repanier.tools import create_or_update_one_cart_item, sint, sboolean, display_selected_value
Patrick's avatar
Patrick committed
15 16 17 18 19 20 21 22


@never_cache
@require_GET
def order_ajax(request):
    if not request.is_ajax():
        raise Http404
    user = request.user
23
    if not user.is_authenticated:
Patrick's avatar
Patrick committed
24
        raise Http404
25 26 27
    customer = Customer.objects.filter(
        user_id=user.id, is_active=True, may_order=True
    ).order_by('?').first()
Patrick's avatar
Patrick committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    if customer is None:
        raise Http404
    offer_item_id = sint(request.GET.get('offer_item', 0))
    value_id = sint(request.GET.get('value', 0))
    basket = sboolean(request.GET.get('basket', False))
    qs = CustomerInvoice.objects.filter(
        permanence__offeritem=offer_item_id,
        customer_id=customer.id,
        status=PERMANENCE_OPENED).order_by('?')
    result = None
    if qs.exists():
        qs = ProducerInvoice.objects.filter(
            permanence__offeritem=offer_item_id,
            producer__offeritem=offer_item_id,
            status=PERMANENCE_OPENED
        ).order_by('?')
        if qs.exists():
Patrick's avatar
Patrick committed
45
            result = create_or_update_one_cart_item(
Patrick's avatar
Patrick committed
46 47 48
                customer=customer,
                offer_item_id=offer_item_id,
                value_id=value_id,
49 50
                basket=basket,
                batch_job=False
Patrick's avatar
Patrick committed
51
            )
Patrick's avatar
Patrick committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

            if result is None:
                # Select one purchase
                purchase = Purchase.objects.filter(
                    customer_id=customer.id,
                    offer_item_id=offer_item_id,
                    is_box_content=False
                ).select_related(
                    "offer_item"
                ).order_by('?').first()
                to_json = []
                if purchase is not None:
                    option_dict = display_selected_value(
                        purchase.offer_item,
                        purchase.quantity_ordered)
                    to_json.append(option_dict)
                else:
                    offer_item = OfferItem.objects.filter(
                        id=offer_item_id
                    ).select_related(
                        "product"
                    ).order_by('?').first()
                    option_dict = display_selected_value(
                        offer_item,
                        DECIMAL_ZERO)
                    to_json.append(option_dict)
                result = json.dumps(to_json, cls=DjangoJSONEncoder)
Patrick's avatar
Patrick committed
79
    return HttpResponse(result, content_type="application/json")