<commit_before>from django import forms
from django.conf import settings
from django.conf.urls import url as re_path
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.sites.shortcuts import get_current_site
from django.core.mail import send_mail
from django.urls import reverse
from django.db import transaction
from django.shortcuts import get_object_or_404, redirect
from django.template.loader import render_to_string
from django.template.response import TemplateResponse
from django.utils import timezone

from conference.models import StripePayment

from .fares import (
    FARE_CODE_GROUPS,
    FARE_CODE_REGEXES,
    FARE_TICKET_TYPES,
    FARE_CODE_TYPES,
    FareIsNotAvailable,
    get_available_fares,
    is_fare_code_valid,
    is_early_bird_sold_out,
)
from .invoicing import create_invoices_for_order
from .orders import (
    Order,
    OrderCalculation,
    calculate_order_price_including_discount,
    create_order,
    is_business_order,
    is_non_conference_ticket_order,
)
from .payments import PaymentError, prepare_for_payment, verify_payment
from .decorators import full_profile_required


GLOBAL_MAX_PER_FARE_TYPE = 6
ORDER_CONFIRMATION_EMAIL_SUBJECT = "%s: Order confirmation" % settings.CONFERENCE_NAME


@full_profile_required
def cart_step1_choose_type_of_order(request):
    """
    This view is not login required because we want to display some summary of
    ticket prices here as well.
    """
    special_fares = get_available_fares_for_type(TicketType.other)
    context = {"show_special": bool(special_fares)}
    return TemplateResponse(
        request, "conference/cart/step_1_choose_type_of_order.html", context
    )


@login_required
@full_profile_required
def cart_step2_pick_tickets(request, type_of_tickets):
    """
    Only submit this form if user is authenticated, otherwise display some
    support info.
    """

    assert type_of_tickets in TicketType.ALL

    available_fares = get_available_fares_for_type(type_of_tickets)
    context = {
        "CartActions": CartActions,
        "available_fares": available_fares,
        "global_max_per_fare_type": GLOBAL_MAX_PER_FARE_TYPE,
        "calculation": OrderCalculation(0, 0, 0),  # empty calculation
        "currency": "EUR",
        "fares_info": {},  # empty fares info
    }

    if request.method == "POST":
        discount_code, fares_info = extract_order_parameters_from_request(
            post_data=request.POST,
        )
        context["fares_info"] = fares_info

        if sum(fares_info.values()) == 0:
            messages.error(request, "Please select some tickets :)")
            return redirect(".")

        try:
            calculation, coupon = calculate_order_price_including_discount(
                for_user=request.user,
                for_date=timezone.now().date(),<commit_msg>Strip the discount code prior to using it. (#1308)<commit_after>from django import forms
from django.conf import settings
from django.conf.urls import url as re_path
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.sites.shortcuts import get_current_site
from django.core.mail import send_mail
from django.urls import reverse
from django.db import transaction
from django.shortcuts import get_object_or_404, redirect
from django.template.loader import render_to_string
from django.template.response import TemplateResponse
from django.utils import timezone

from conference.models import StripePayment

from .fares import (
    FARE_CODE_GROUPS,
    FARE_CODE_REGEXES,
    FARE_TICKET_TYPES,
    FARE_CODE_TYPES,
    FareIsNotAvailable,
    get_available_fares,
    is_fare_code_valid,
    is_early_bird_sold_out,
)
from .invoicing import create_invoices_for_order
from .orders import (
    Order,
    OrderCalculation,
    calculate_order_price_including_discount,
    create_order,
    is_business_order,
    is_non_conference_ticket_order,
)
from .payments import PaymentError, prepare_for_payment, verify_payment
from .decorators import full_profile_required


GLOBAL_MAX_PER_FARE_TYPE = 6
ORDER_CONFIRMATION_EMAIL_SUBJECT = "%s: Order confirmation" % settings.CONFERENCE_NAME


@full_profile_required
def cart_step1_choose_type_of_order(request):
    """
    This view is not login required because we want to display some summary of
    ticket prices here as well.
    """
    special_fares = get_available_fares_for_type(TicketType.other)
    context = {"show_special": bool(special_fares)}
    return TemplateResponse(
        request, "conference/cart/step_1_choose_type_of_order.html", context
    )


@login_required
@full_profile_required
def cart_step2_pick_tickets(request, type_of_tickets):
    """
    Only submit this form if user is authenticated, otherwise display some
    support info.
    """

    assert type_of_tickets in TicketType.ALL

    available_fares = get_available_fares_for_type(type_of_tickets)
    context = {
        "CartActions": CartActions,
        "available_fares": available_fares,
        "global_max_per_fare_type": GLOBAL_MAX_PER_FARE_TYPE,
        "calculation": OrderCalculation(0, 0, 0),  # empty calculation
        "currency": "EUR",
        "fares_info": {},  # empty fares info
    }

    if request.method == "POST":
        discount_code, fares_info = extract_order_parameters_from_request(
            post_data=request.POST,
        )
        if discount_code is not None:
            discount_code = discount_code.strip()
        context["fares_info"] = fares_info

        if sum(fares_info.values()) == 0:
            messages.error(request, "Please select some tickets :)")
            return redirect(".")

        try:
            calculation, coupon = calculate_order_price_including_discount(
                for_user=request.user,
                for_date=timezone.now().date(),
{'fraction_non_alphanumeric': 0.06904156064461409, 'fraction_numerical': 0.004410517387616625, 'mean_word_length': 3.884838442419221, 'pattern_counts': {'":': 16, '<': 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': '7085416', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1763, 'n_words': 414}