<commit_before>from functools import wraps
import logging

from django import forms
from django.contrib import auth, messages
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.core.urlresolvers import get_callable, reverse
from django.forms.models import inlineformset_factory, ModelForm
from django.http import HttpResponseRedirect
from django.shortcuts import redirect as django_redirect, render
from django.utils.translation import ugettext as _

import plata
from plata.shop import forms as shop_forms
from plata.shop import signals


logger = logging.getLogger('plata.shop.views')


def cart_not_empty(order, request, **kwargs):
    """Redirect to cart if later in checkout process and cart empty"""
    shop = kwargs.get('shop')
    if not order or not order.items.count():
        messages.warning(request, _('Cart is empty.'))
        return shop.redirect('plata_shop_cart')

def order_already_confirmed(order, request, **kwargs):
    """Redirect to confirmation or already paid view if the order is already confirmed"""
    shop = kwargs.get('shop')
    if order and order.status >= order.CONFIRMED:
        if not order.balance_remaining:
            return shop.redirect('plata_order_success')
        messages.warning(request,
            _('You have already confirmed this order earlier, but it is not fully paid for yet.'))
        return HttpResponseRedirect(reverse('plata_shop_confirmation') + '?confirmed=1')

def order_cart_validates(order, request, **kwargs):
    """Redirect to cart if stock is insufficient and display an error message"""
    if request.method != 'GET':
        return

    try:
        order.validate(order.VALIDATE_CART)
    except ValidationError, e:
        shop = kwargs.get('shop')
        for message in e.messages:
            messages.error(request, message)
        return shop.redirect('plata_shop_cart')

def checkout_process_decorator(*checks):
    """
    Calls all passed checkout process decorators in turn::

        @checkout_process_decorator(order_already_confirmed, order_cart_validates)
        def mymethod(self...):
            # Whatever
    """

    def _dec(fn):
        def _fn(request, *args, **kwargs):
            shop = plata.shop_instance()
            order = shop.order_from_request(request)
<commit_msg>Checkout process decorators: The shop is always available as a keyword argument<commit_after>from functools import wraps
import logging

from django import forms
from django.contrib import auth, messages
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.core.urlresolvers import get_callable, reverse
from django.forms.models import ModelForm, inlineformset_factory
from django.http import HttpResponseRedirect
from django.shortcuts import redirect as django_redirect, render
from django.utils.translation import ugettext as _

import plata
from plata.shop import forms as shop_forms
from plata.shop import signals


logger = logging.getLogger('plata.shop.views')


def cart_not_empty(order, shop, request, **kwargs):
    """Redirect to cart if later in checkout process and cart empty"""
    if not order or not order.items.count():
        messages.warning(request, _('Cart is empty.'))
        return shop.redirect('plata_shop_cart')


def order_already_confirmed(order, shop, request, **kwargs):
    """Redirect to confirmation or already paid view if the order is already confirmed"""
    if order and order.status >= order.CONFIRMED:
        if not order.balance_remaining:
            return shop.redirect('plata_order_success')
        messages.warning(request,
            _('You have already confirmed this order earlier, but it is not fully paid for yet.'))
        return HttpResponseRedirect(reverse('plata_shop_confirmation') + '?confirmed=1')


def order_cart_validates(order, shop, request, **kwargs):
    """Redirect to cart if stock is insufficient and display an error message"""
    if request.method != 'GET':
        return

    try:
        order.validate(order.VALIDATE_CART)
    except ValidationError, e:
        for message in e.messages:
            messages.error(request, message)
        return shop.redirect('plata_shop_cart')


def checkout_process_decorator(*checks):
    """
    Calls all passed checkout process decorators in turn::

        @checkout_process_decorator(order_already_confirmed, order_cart_validates)
        def mymethod(self...):
            # Whatever
    """

    def _dec(fn):
        def _fn(request, *args, **kwargs):
            shop = plata.shop_instance()
            order = shop.order_from_request(request)

{'fraction_non_alphanumeric': 0.0845719070546368, 'fraction_numerical': 0.0004186728072011723, 'mean_word_length': 3.749502982107356, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6772157', 'n_tokens_mistral': 1290, 'n_tokens_neox': 1249, 'n_words': 416}