
from django.utils.translation import ugettext
from django.views.decorators.http import require_POST
from django.http import JsonResponse
from django.shortcuts import render
from django.core.exceptions import ValidationError
from django.views.decorators.csrf import csrf_exempt

from cart.lib import get_cart
from cart.forms import SelectProductForm, SetQtyForm


@require_POST
def _cart_action_view(request, action_factory, form_class, message):

    form = form_class(data=request.POST)

    if not form.is_valid():
        return JsonResponse({'message': form.errors.as_json()}, status=403)

    cart = get_cart(request)

    try:
        result = action_factory(cart, form.cleaned_data)
    except ValidationError as e:
        return JsonResponse({'message': ', '.join(e.messages)}, status=403)

    return JsonResponse({
        'message': message,
        'result': result,
        'total': cart.printable_total
    })


def add(request):
    return _cart_action_view(
        request,
        action_factory=lambda cart, data: cart.add(**data),
        form_class=SelectProductForm,
        message=ugettext('Product added to cart')
    )


def remove(request):
    return _cart_action_view(
        request,
        action_factory=lambda cart, data: cart.remove(**data),
        form_class=SelectProductForm,
        message=ugettext('Product removed from cart')
    )


def get_modal(request):
    cart = get_cart(request)
    return render(request, 'cart/modal.html', {'cart': cart})


@csrf_exempt
def set_qty(request):
    return _cart_action_view(
        request,
        action_factory=lambda cart, data: cart.set_qty(**data),
        form_class=SetQtyForm,
        message=ugettext('Quantity updated')
    )

[((21, 11, 21, 28), 'cart.lib.get_cart', 'get_cart', ({(21, 20, 21, 27): 'request'}, {}), '(request)', False, 'from cart.lib import get_cart\n'), ((28, 11, 32, 6), 'django.http.JsonResponse', 'JsonResponse', ({(28, 24, 32, 5): "{'message': message, 'result': result, 'total': cart.printable_total}"}, {}), "({'message': message, 'result': result, 'total': cart.\n    printable_total})", False, 'from django.http import JsonResponse\n'), ((54, 11, 54, 28), 'cart.lib.get_cart', 'get_cart', ({(54, 20, 54, 27): 'request'}, {}), '(request)', False, 'from cart.lib import get_cart\n'), ((55, 11, 55, 61), 'django.shortcuts.render', 'render', ({(55, 18, 55, 25): 'request', (55, 27, 55, 44): '"""cart/modal.html"""', (55, 46, 55, 60): "{'cart': cart}"}, {}), "(request, 'cart/modal.html', {'cart': cart})", False, 'from django.shortcuts import render\n'), ((40, 16, 40, 49), 'django.utils.translation.ugettext', 'ugettext', ({(40, 25, 40, 48): '"""Product added to cart"""'}, {}), "('Product added to cart')", False, 'from django.utils.translation import ugettext\n'), ((49, 16, 49, 53), 'django.utils.translation.ugettext', 'ugettext', ({(49, 25, 49, 52): '"""Product removed from cart"""'}, {}), "('Product removed from cart')", False, 'from django.utils.translation import ugettext\n'), ((64, 16, 64, 44), 'django.utils.translation.ugettext', 'ugettext', ({(64, 25, 64, 43): '"""Quantity updated"""'}, {}), "('Quantity updated')", False, 'from django.utils.translation import ugettext\n')]