<commit_before>from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import permission_required
from django.db import transaction
from django.db.models import F, Q
from django.forms import modelformset_factory
from django.http import HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, redirect
from django.template.context_processors import csrf
from django.template.response import TemplateResponse
from django.utils.translation import npgettext_lazy, pgettext_lazy
from django.views.decorators.http import require_POST
from django_prices.templatetags import prices_i18n

from ...core.exceptions import InsufficientStock
from ...core.utils import get_paginator_items
from ...core.utils.taxes import get_taxes_for_address
from ...order import OrderEvents, OrderEventsEmails, OrderStatus
from ...order.emails import (
    send_fulfillment_confirmation, send_fulfillment_update,
    send_order_confirmation)
from ...order.models import Fulfillment, FulfillmentLine, Order
from ...order.utils import update_order_prices, update_order_status
from ...shipping.models import ShippingMethod
from ..views import staff_member_required
from .filters import OrderFilter
from .forms import (
    AddressForm, AddVariantToOrderForm, BaseFulfillmentLineFormSet,
    CancelFulfillmentForm, CancelOrderForm, CancelOrderLineForm,
    CapturePaymentForm, ChangeQuantityForm, CreateOrderFromDraftForm,
    FulfillmentForm, FulfillmentLineForm, FulfillmentTrackingNumberForm,
    OrderCustomerForm, OrderEditDiscountForm, OrderEditVoucherForm,
    OrderMarkAsPaidForm, OrderNoteForm, OrderRemoveCustomerForm,
    OrderRemoveShippingForm, OrderRemoveVoucherForm, OrderShippingForm,
    RefundPaymentForm, VoidPaymentForm)
from .utils import (
    create_invoice_pdf, create_packing_slip_pdf, get_statics_absolute_url,
    save_address_in_order)


@staff_member_required
@permission_required('order.manage_orders')
def order_list(request):
    orders = Order.objects.prefetch_related('payments', 'lines', 'user')
    order_filter = OrderFilter(request.GET, queryset=orders)
    orders = get_paginator_items(
        order_filter.qs, settings.DASHBOARD_PAGINATE_BY,
        request.GET.get('page'))
    ctx = {
        'orders': orders, 'filter_set': order_filter,
        'is_empty': not order_filter.queryset.exists()}
    return TemplateResponse(request, 'dashboard/order/list.html', ctx)


@require_POST
@staff_member_required
@permission_required('order.manage_orders')
def order_create(request):
    display_gross_prices = request.site.settings.display_gross_prices
    order = Order.objects.create(
        status=OrderStatus.DRAFT, display_gross_prices=display_gross_prices)
    msg = pgettext_lazy(
        'Dashboard message related to an order',
        'Draft order created')
    messages.success(request, msg)
    return redirect('dashboard:order-details', order_pk=order.pk)


@staff_member_required
@permission_required('order.manage_orders')
def create_order_from_draft(request, order_pk):
    order = get_object_or_404(Order.objects.drafts(), pk=order_pk)
    status = 200
    form = CreateOrderFromDraftForm(request.POST or None, instance=order)
    if form.is_valid():
        form.save()
        msg = pgettext_lazy(
            'Dashboard message related to an order',
            'Order created from draft order')
        order.events.create(
            user=request.user,
            type=OrderEvents.PLACED_FROM_DRAFT.value)
        messages.success(request, msg)
        if form.cleaned_data.get('notify_customer'):
            send_order_confirmation.delay(order.pk)
            order.events.create(
                parameters={
                    'email': order.get_user_current_email(),
                    'email_type': OrderEventsEmails.ORDER.value},
                type=OrderEvents.EMAIL_SENT.value)
        return redirect('dashboard:order-details', order_pk=order.pk)
    elif form.errors:
        status = 400
    template = 'dashboard/order/modal/create_order.html'
    ctx = {'form': form, 'order': order}
    return TemplateResponse(request, template, ctx, status=status)


@staff_member_required
@permission_required('order.manage_orders')
def remove_draft_order(request, order_pk):
    order = get_object_or_404(Order.objects.drafts(), pk=order_pk)
    if request.method == 'POST':
        order.delete()
        msg = pgettext_lazy(
            'Dashboard message', 'Draft order successfully removed')
        messages.success(request, msg)
        return redirect('dashboard:orders')
    template = 'dashboard/order/modal/remove_order.html'
    ctx = {'order': order}
    return TemplateResponse(request, template, ctx)


@staff_member_required
@permission_required('order.manage_orders')
def order_details(request, order_pk):
    qs = Order.objects.select_related(
        'user', 'shipping_address', 'billing_address').prefetch_related(
        'payments', 'events__user', 'lines__variant__product',
        'fulfillments__lines__order_line')
    order = get_object_or_404(qs, pk=order_pk)<commit_msg>Prefetch transactions for order's payment in dashboard<commit_after>from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import permission_required
from django.db import transaction
from django.db.models import F, Q
from django.forms import modelformset_factory
from django.http import HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, redirect
from django.template.context_processors import csrf
from django.template.response import TemplateResponse
from django.utils.translation import npgettext_lazy, pgettext_lazy
from django.views.decorators.http import require_POST
from django_prices.templatetags import prices_i18n

from ...core.exceptions import InsufficientStock
from ...core.utils import get_paginator_items
from ...core.utils.taxes import get_taxes_for_address
from ...order import OrderEvents, OrderEventsEmails, OrderStatus
from ...order.emails import (
    send_fulfillment_confirmation, send_fulfillment_update,
    send_order_confirmation)
from ...order.models import Fulfillment, FulfillmentLine, Order
from ...order.utils import update_order_prices, update_order_status
from ...shipping.models import ShippingMethod
from ..views import staff_member_required
from .filters import OrderFilter
from .forms import (
    AddressForm, AddVariantToOrderForm, BaseFulfillmentLineFormSet,
    CancelFulfillmentForm, CancelOrderForm, CancelOrderLineForm,
    CapturePaymentForm, ChangeQuantityForm, CreateOrderFromDraftForm,
    FulfillmentForm, FulfillmentLineForm, FulfillmentTrackingNumberForm,
    OrderCustomerForm, OrderEditDiscountForm, OrderEditVoucherForm,
    OrderMarkAsPaidForm, OrderNoteForm, OrderRemoveCustomerForm,
    OrderRemoveShippingForm, OrderRemoveVoucherForm, OrderShippingForm,
    RefundPaymentForm, VoidPaymentForm)
from .utils import (
    create_invoice_pdf, create_packing_slip_pdf, get_statics_absolute_url,
    save_address_in_order)


@staff_member_required
@permission_required('order.manage_orders')
def order_list(request):
    orders = Order.objects.prefetch_related('payments', 'lines', 'user')
    order_filter = OrderFilter(request.GET, queryset=orders)
    orders = get_paginator_items(
        order_filter.qs, settings.DASHBOARD_PAGINATE_BY,
        request.GET.get('page'))
    ctx = {
        'orders': orders, 'filter_set': order_filter,
        'is_empty': not order_filter.queryset.exists()}
    return TemplateResponse(request, 'dashboard/order/list.html', ctx)


@require_POST
@staff_member_required
@permission_required('order.manage_orders')
def order_create(request):
    display_gross_prices = request.site.settings.display_gross_prices
    order = Order.objects.create(
        status=OrderStatus.DRAFT, display_gross_prices=display_gross_prices)
    msg = pgettext_lazy(
        'Dashboard message related to an order',
        'Draft order created')
    messages.success(request, msg)
    return redirect('dashboard:order-details', order_pk=order.pk)


@staff_member_required
@permission_required('order.manage_orders')
def create_order_from_draft(request, order_pk):
    order = get_object_or_404(Order.objects.drafts(), pk=order_pk)
    status = 200
    form = CreateOrderFromDraftForm(request.POST or None, instance=order)
    if form.is_valid():
        form.save()
        msg = pgettext_lazy(
            'Dashboard message related to an order',
            'Order created from draft order')
        order.events.create(
            user=request.user,
            type=OrderEvents.PLACED_FROM_DRAFT.value)
        messages.success(request, msg)
        if form.cleaned_data.get('notify_customer'):
            send_order_confirmation.delay(order.pk)
            order.events.create(
                parameters={
                    'email': order.get_user_current_email(),
                    'email_type': OrderEventsEmails.ORDER.value},
                type=OrderEvents.EMAIL_SENT.value)
        return redirect('dashboard:order-details', order_pk=order.pk)
    elif form.errors:
        status = 400
    template = 'dashboard/order/modal/create_order.html'
    ctx = {'form': form, 'order': order}
    return TemplateResponse(request, template, ctx, status=status)


@staff_member_required
@permission_required('order.manage_orders')
def remove_draft_order(request, order_pk):
    order = get_object_or_404(Order.objects.drafts(), pk=order_pk)
    if request.method == 'POST':
        order.delete()
        msg = pgettext_lazy(
            'Dashboard message', 'Draft order successfully removed')
        messages.success(request, msg)
        return redirect('dashboard:orders')
    template = 'dashboard/order/modal/remove_order.html'
    ctx = {'order': order}
    return TemplateResponse(request, template, ctx)


@staff_member_required
@permission_required('order.manage_orders')
def order_details(request, order_pk):
    qs = Order.objects.select_related(
        'user', 'shipping_address', 'billing_address').prefetch_related(
        'payments__transactions', 'events__user', 'lines__variant__product',
        'fulfillments__lines__order_line')
    order = get_object_or_404(qs, pk=order_pk)
{'fraction_non_alphanumeric': 0.08748658929093923, 'fraction_numerical': 0.0039012971813127866, 'mean_word_length': 4.445565586829527, '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': '2207492', 'n_tokens_mistral': 3026, 'n_tokens_neox': 2968, 'n_words': 617}