<commit_before>from django.contrib import messages
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.utils.translation import pgettext, pgettext_lazy
from satchless.item import InsufficientStock

from ..forms import (
    AnonymousUserBillingForm, BillingAddressesForm,
    BillingWithoutShippingAddressForm)
from ...userprofile.forms import get_address_form
from ...userprofile.models import Address
from ...order import OrderStatus


def create_order(checkout):
    """Finalize a checkout session and create an order.

    This is a helper function.

    `checkout` is a `saleor.checkout.core.Checkout` instance.
    """
    try:
        order = checkout.create_order()
    except InsufficientStock:
        order = None
    if not order:
        return None, redirect('checkout:summary')
    checkout.clear_storage()
    checkout.cart.clear()
    user = None if checkout.user.is_anonymous else checkout.user
    order.create_history_entry(
        status=OrderStatus.NEW, user=user, comment=pgettext_lazy(
            'Order status history entry', 'Order was placed'))
    order.send_confirmation_email()
    return order, redirect('order:payment', token=order.token)


def handle_order_placement(request, checkout):
    """Try to create an order and redirect the user as necessary.

    This is a helper function.
    """
    order, redirect = create_order(checkout)
    if not order:
        msg = pgettext('Checkout warning', 'Please review your checkout.')
        messages.warning(request, msg)
    return redirect
<commit_msg>Redirect to cart from checkout if insufficient stock<commit_after>from django.contrib import messages
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.utils.translation import pgettext, pgettext_lazy
from satchless.item import InsufficientStock

from ..forms import (
    AnonymousUserBillingForm, BillingAddressesForm,
    BillingWithoutShippingAddressForm)
from ...userprofile.forms import get_address_form
from ...userprofile.models import Address
from ...order import OrderStatus


def create_order(checkout):
    """Finalize a checkout session and create an order.

    This is a helper function.

    `checkout` is a `saleor.checkout.core.Checkout` instance.
    """
    order = checkout.create_order()
    if not order:
        return None, redirect('checkout:summary')
    checkout.clear_storage()
    checkout.cart.clear()
    user = None if checkout.user.is_anonymous else checkout.user
    order.create_history_entry(
        status=OrderStatus.NEW, user=user, comment=pgettext_lazy(
            'Order status history entry', 'Order was placed'))
    order.send_confirmation_email()
    return order, redirect('order:payment', token=order.token)


def handle_order_placement(request, checkout):
    """Try to create an order and redirect the user as necessary.

    This is a helper function.
    """
    try:
        order, redirect_url = create_order(checkout)
    except InsufficientStock:
        return redirect('cart:index')
    if not order:
        msg = pgettext('Checkout warning', 'Please review your checkout.')
        messages.warning(request, msg)
    return redirect_url

{'fraction_non_alphanumeric': 0.08143121529919803, 'fraction_numerical': 0.0, 'mean_word_length': 3.8187221396731057, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2625447', 'n_tokens_mistral': 894, 'n_tokens_neox': 859, 'n_words': 271}