<commit_before># django imports
from django.core.exceptions import ObjectDoesNotExist
from django.core.cache import cache
from django.core.urlresolvers import reverse

# lfs imports
from lfs.cart.models import CartItem
from lfs.cart.models import Cart
from lfs.payment import utils as payment_utils
from lfs.shipping import utils as shipping_utils

def get_cart_max_delivery_time(request, cart):
    """Returns the delivery time object with the maximal delivery time of all
    products within the cart. Takes the selected shipping method into account.

    This is used within the cart to display the maximal delivery time.
    """
    max_delivery_time = None
    for item in cart.items():
        # Calculate the delivery time of the product. Takes the selected
        # shipping method into account.
        delivery_time = shipping_utils.get_product_delivery_time(
            request, item.product.slug, for_cart=True)
        if (max_delivery_time is None) or \
           (delivery_time.as_hours() > max_delivery_time.as_hours()):
            max_delivery_time = delivery_time
    return max_delivery_time

# TODO: Remove cart from signature?
def get_cart_price(request, cart, total=False, cached=True):
    """Returns price of the given cart.
    """
    return get_cart_costs(request, cart, total, cached)["price"]

def get_cart_costs(request, cart, total=False, cached=True):
    """Returns a dictionary with price and tax of the given cart:

        returns {
            "price" : the cart's price,
            "tax" : the cart's tax,
        }
    """
    if cart is None:
        return {"price" : 0, "tax" : 0}

    cache_key = "cart-costs-%s-%s" % (total, cart.id)

    if cached:
        cart_costs = cache.get(cache_key)
    else:
        cart_costs = None

    if cart_costs is None:

        items = cart.items()

        cart_price = 0
        cart_tax = 0
        for item in items:
            cart_price += item.get_price()
            cart_tax += item.get_tax()

        if len(items) > 0 and total:
            # Shipping
            shipping_method = shipping_utils.get_selected_shipping_method(request)
            shipping_costs = shipping_utils.get_shipping_costs(request, shipping_method)
            cart_price += shipping_costs["price"]
            cart_tax += shipping_costs["tax"]

            # Payment
            payment_method = payment_utils.get_selected_payment_method(request)
            payment_costs = payment_utils.get_payment_costs(request, payment_method)
            cart_price += payment_costs["price"]
            cart_tax += payment_costs["tax"]
            
            # Discounts
            import lfs.discounts.utils
            discounts = lfs.discounts.utils.get_valid_discounts(request)
            for discount in discounts:
                cart_price = cart_price - discount["price"]

        cart_costs = {"price" : cart_price, "tax" : cart_tax}
        cache.set(cache_key, cart_costs)

    return cart_costs

def get_or_create_cart(request):
    """Returns the cart of the current user. If no cart exists it creates a new
    one first.
    """
    cart = get_cart(request)
    if cart is None:
        cart = create_cart(request)

    return cart

def create_cart(request):
    """Creates a cart for the current session and/or user.
    """
    cart = Cart(session = request.session.session_key)
    if request.user.is_authenticated():
        cart.user = request.user

    cart.save()
    return cart<commit_msg>Bugfix: Subtract voucher price from cart price.<commit_after># django imports
from django.core.exceptions import ObjectDoesNotExist
from django.core.cache import cache
from django.core.urlresolvers import reverse

# lfs imports
from lfs.cart.models import CartItem
from lfs.cart.models import Cart
from lfs.payment import utils as payment_utils
from lfs.shipping import utils as shipping_utils
from lfs.voucher.models import Voucher

def get_cart_max_delivery_time(request, cart):
    """Returns the delivery time object with the maximal delivery time of all
    products within the cart. Takes the selected shipping method into account.

    This is used within the cart to display the maximal delivery time.
    """
    max_delivery_time = None
    for item in cart.items():
        # Calculate the delivery time of the product. Takes the selected
        # shipping method into account.
        delivery_time = shipping_utils.get_product_delivery_time(
            request, item.product.slug, for_cart=True)
        if (max_delivery_time is None) or \
           (delivery_time.as_hours() > max_delivery_time.as_hours()):
            max_delivery_time = delivery_time
    return max_delivery_time

# TODO: Remove cart from signature?
def get_cart_price(request, cart, total=False, cached=True):
    """Returns price of the given cart.
    """
    return get_cart_costs(request, cart, total, cached)["price"]

def get_cart_costs(request, cart, total=False, cached=True):
    """Returns a dictionary with price and tax of the given cart:

        returns {
            "price" : the cart's price,
            "tax" : the cart's tax,
        }
    """
    if cart is None:
        return {"price" : 0, "tax" : 0}

    cache_key = "cart-costs-%s-%s" % (total, cart.id)

    if cached:
        cart_costs = cache.get(cache_key)
    else:
        cart_costs = None

    if cart_costs is None:

        items = cart.items()

        cart_price = 0
        cart_tax = 0
        for item in items:
            cart_price += item.get_price()
            cart_tax += item.get_tax()

        if len(items) > 0 and total:
            # Shipping
            shipping_method = shipping_utils.get_selected_shipping_method(request)
            shipping_costs = shipping_utils.get_shipping_costs(request, shipping_method)
            cart_price += shipping_costs["price"]
            cart_tax += shipping_costs["tax"]

            # Payment
            payment_method = payment_utils.get_selected_payment_method(request)
            payment_costs = payment_utils.get_payment_costs(request, payment_method)
            cart_price += payment_costs["price"]
            cart_tax += payment_costs["tax"]

            # Discounts
            import lfs.discounts.utils
            discounts = lfs.discounts.utils.get_valid_discounts(request)
            for discount in discounts:
                cart_price = cart_price - discount["price"]

            # Vouchers
            try:
                voucher_number = lfs.voucher.utils.get_current_voucher_number(request)
                voucher = Voucher.objects.get(number=voucher_number)
            except Voucher.DoesNotExist:
                pass
            else:
                voucher_value = voucher.get_price_gross(cart)
                cart_price = cart_price - voucher_value

        cart_costs = {"price" : cart_price, "tax" : cart_tax}
        cache.set(cache_key, cart_costs)

    return cart_costs

def get_or_create_cart(request):
    """Returns the cart of the current user. If no cart exists it creates a new
    one first.
    """
    cart = get_cart(request)
    if cart is None:
        cart = create_cart(request)

    return cart

def create_cart(request):
    """Creates a cart for the current session and/or user.
    """
    cart = Cart(session = request.session.session_key)
    if request.user.is_authenticated():
        cart.user = request.user

    cart.save()
    return cart
{'fraction_non_alphanumeric': 0.08128875707737934, 'fraction_numerical': 0.0013480722566729577, 'mean_word_length': 2.566826923076923, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5477649', 'n_tokens_mistral': 2190, 'n_tokens_neox': 2138, 'n_words': 606}