basket_message_form_ajax.py 1.54 KB
Newer Older
Patrick's avatar
Patrick committed
1 2 3 4 5 6 7 8 9 10 11
# -*- 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

Patrick's avatar
Patrick committed
12
from repanier.models import Producer
Patrick's avatar
Patrick committed
13 14 15 16 17
from repanier.models import Customer


@never_cache
@require_GET
Patrick's avatar
Patrick committed
18
def customer_basket_message_form_ajax(request, pk):
Patrick's avatar
Patrick committed
19 20
    if request.is_ajax():
        user = request.user
21
        if user.is_anonymous:
Patrick's avatar
Patrick committed
22
            raise Http404
Patrick's avatar
Patrick committed
23 24
        to_json = []
        if request.user.is_staff:
Patrick's avatar
Patrick committed
25
            customer = Customer.objects.filter(id=pk).order_by('?').first()
Patrick's avatar
Patrick committed
26 27
        else:
            customer = request.user.customer
Patrick's avatar
Patrick committed
28
        option_dict = {'id': "#basket_message", 'html': customer.get_on_hold_movement_html()}
Patrick's avatar
Patrick committed
29 30 31
        to_json.append(option_dict)
        return HttpResponse(json.dumps(to_json, cls=DjangoJSONEncoder), content_type="application/json")
    raise Http404
Patrick's avatar
Patrick committed
32 33 34 35 36 37 38 39 40 41 42 43 44


@never_cache
@require_GET
def producer_basket_message_form_ajax(request, pk, uuid):
    if request.is_ajax():
        user = request.user
        if not (user.is_anonymous or request.user.is_staff):
            raise Http404
        producer = Producer.objects.filter(id=pk, uuid=uuid).order_by('?').first()
        if producer is None:
            raise Http404
        to_json = []
Patrick's avatar
Patrick committed
45
        producer.on_hold_movement_json(to_json)
Patrick's avatar
Patrick committed
46 47
        return HttpResponse(json.dumps(to_json, cls=DjangoJSONEncoder), content_type="application/json")
    raise Http404