<commit_before>import logging

from django.conf import settings
from django.contrib import messages, auth
from django.contrib.auth.decorators import login_required
from django.db import transaction
from django.http import Http404, HttpResponseForbidden, HttpResponseRedirect
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils.translation import pgettext_lazy
from payments import PaymentStatus, RedirectNeeded

from . import OrderStatus
from .forms import (
    PaymentDeleteForm, PaymentMethodsForm, PasswordForm, OrderNoteForm)
from .models import Order, OrderNote, Payment
from .utils import attach_order_to_user, check_order_status
from ..checkout.forms import NoteForm
from ..core.utils import get_client_ip, build_absolute_uri
from ..registration.forms import LoginForm
from ..userprofile.models import User

logger = logging.getLogger(__name__)


def details(request, token):
    orders = Order.objects.prefetch_related('groups__lines__product')
    orders = orders.select_related(
        'billing_address', 'shipping_address', 'user')
    order = get_object_or_404(orders, token=token)
    groups = order.groups.all()
    notes = order.notes.filter(is_public=True)
    ctx = {'order': order, 'groups': groups, 'notes': notes}
    if order.status == OrderStatus.OPEN:
        note = OrderNote(order=order, user=request.user)
        note_form = OrderNoteForm(request.POST or None, instance=note)
        ctx.update({'note_form': note_form})
        if request.method == 'POST':
            if note_form.is_valid():
                note_form.save()
                return redirect('order:details', token=order.token)
    return TemplateResponse(request, 'order/details.html', ctx)


def payment(request, token):
    orders = Order.objects.prefetch_related('groups__lines__product')
    orders = orders.select_related(
        'billing_address', 'shipping_address', 'user')
    order = get_object_or_404(orders, token=token)
    groups = order.groups.all()
    payments = order.payments.all()
    form_data = request.POST or None
    try:
        waiting_payment = order.payments.get(status=PaymentStatus.WAITING)
    except Payment.DoesNotExist:
        waiting_payment = None
        waiting_payment_form = None
    else:
        form_data = None
        waiting_payment_form = PaymentDeleteForm(<commit_msg>Fix order details fails for not logged in user and order open<commit_after>import logging

from django.conf import settings
from django.contrib import messages, auth
from django.contrib.auth.decorators import login_required
from django.db import transaction
from django.http import Http404, HttpResponseForbidden, HttpResponseRedirect
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils.translation import pgettext_lazy
from payments import PaymentStatus, RedirectNeeded

from . import OrderStatus
from .forms import (
    PaymentDeleteForm, PaymentMethodsForm, PasswordForm, OrderNoteForm)
from .models import Order, OrderNote, Payment
from .utils import attach_order_to_user, check_order_status
from ..checkout.forms import NoteForm
from ..core.utils import get_client_ip, build_absolute_uri
from ..registration.forms import LoginForm
from ..userprofile.models import User

logger = logging.getLogger(__name__)


def details(request, token):
    orders = Order.objects.prefetch_related('groups__lines__product')
    orders = orders.select_related(
        'billing_address', 'shipping_address', 'user')
    order = get_object_or_404(orders, token=token)
    groups = order.groups.all()
    notes = order.notes.filter(is_public=True)
    ctx = {'order': order, 'groups': groups, 'notes': notes}
    if order.status == OrderStatus.OPEN:
        user = request.user if request.user.is_authenticated else None
        note = OrderNote(order=order, user=user)
        note_form = OrderNoteForm(request.POST or None, instance=note)
        ctx.update({'note_form': note_form})
        if request.method == 'POST':
            if note_form.is_valid():
                note_form.save()
                return redirect('order:details', token=order.token)
    return TemplateResponse(request, 'order/details.html', ctx)


def payment(request, token):
    orders = Order.objects.prefetch_related('groups__lines__product')
    orders = orders.select_related(
        'billing_address', 'shipping_address', 'user')
    order = get_object_or_404(orders, token=token)
    groups = order.groups.all()
    payments = order.payments.all()
    form_data = request.POST or None
    try:
        waiting_payment = order.payments.get(status=PaymentStatus.WAITING)
    except Payment.DoesNotExist:
        waiting_payment = None
        waiting_payment_form = None
    else:
        form_data = None
        waiting_payment_form = PaymentDeleteForm(
{'fraction_non_alphanumeric': 0.08684263550272013, 'fraction_numerical': 0.0048357848075760625, 'mean_word_length': 4.165452653485952, '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': '315389', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1362, 'n_words': 360}