def get_product_delivery_time(request, product, for_cart=False):
    """Returns the delivery time object for the product.

    If the ``for_cart`` parameter is False, the default delivery time for
    product is calculated. This is at the moment the first valid (iow with the
    hightest priority) shipping method.

    If the ``for_cart parameter`` is True, the delivery time for the product
    within the cart is calculated. This can differ because the shop customer has
    the opportunity to select a shipping method within the cart. If this
    shipping method is valid for the given product this one is taken, if not
    the default one - the default one is the first valid shipping method.
    """
    # TODO: Need a reasonable chaching here
    if for_cart:
        cache_key = "%s-shipping-delivery-time-cart-%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, request.user.id)
    else:
        cache_key = "%s-shipping-delivery-time" % settings.CACHE_MIDDLEWARE_KEY_PREFIX

    shippings = None  # cache.get(cache_key)
    if shippings is None:
        shippings = {}

    product_key = "product-%s" % product.id
    shipping = shippings.get(product_key)
    if shipping is not None:
        return shipping

    # if the product is a product with variants we switch to the default
    # variant to calculate the delivery time. Please note that in this case
    # the default variant is also displayed.
    if product.sub_type == PRODUCT_WITH_VARIANTS:
        variant = product.get_default_variant()
        if variant is not None:
            product = variant

    delivery_time = product.get_manual_delivery_time()
    if not delivery_time:
        if for_cart:
            sm = get_selected_shipping_method(request)
            # Within the cart we have to take care of the selected shipping
            # method.
            sms = get_valid_shipping_methods(request, product)
            if sm in sms:
                try:
                    delivery_time = sm.delivery_time
                except __HOLE__:
                    delivery_time = None
            else:
                sm = get_default_shipping_method(request)
                try:
                    delivery_time = sm.delivery_time
                except AttributeError:
                    delivery_time = None
        else:
            # For the product we take the standard shipping method, which is the
            # first valid shipping method at the moment.
            try:
                shipping_method = get_first_valid_shipping_method(request, product)
                delivery_time = shipping_method.delivery_time
            except AttributeError:
                delivery_time = None

    if delivery_time is None:
        delivery_time = lfs.core.utils.get_default_shop(request).delivery_time or \
                        DeliveryTime(min=1, max=2, unit=DELIVERY_TIME_UNIT_DAYS)

    # Calculate the total delivery time if the product is not on stock.
    if (product.stock_amount <= 0) and (product.order_time):

        # Calculate how much days are left until the product is going to be
        # delivered.
        if product.ordered_at:
            order_delta = datetime.now().date() - product.ordered_at
            order_delta = order_delta.days
        else:
            order_delta = 0

        # Calculate the rest of the origin order time.
        order_time_left = product.order_time.subtract_days(order_delta)

        # Calculate the total delivery time.
        delivery_time += order_time_left
        delivery_time = delivery_time.as_reasonable_unit()

    delivery_time = delivery_time.round()
    shippings[product_key] = delivery_time
    cache.set(cache_key, shippings)

    return delivery_time
dataset/ETHPy150Open diefenbach/django-lfs/lfs/shipping/utils.py/get_product_delivery_time