def calculate_packing(request, id, quantity=None, with_properties=False, as_string=False, template_name="lfs/catalog/packing_result.html"):
    product = Product.objects.get(pk=id)

    if quantity is None:
        try:
            quantity = request.POST.get("quantity")
            if isinstance(quantity, unicode):
                # atof() on unicode string fails in some environments, like Czech
                quantity = quantity.encode("utf-8")
            quantity = locale.atof(quantity)
        except (AttributeError, TypeError, ValueError):
            quantity = 1

    packing_amount, packing_unit = product.get_packing_info()

    try:
        packs = math.ceil(quantity / packing_amount)
        real_quantity = packs * packing_amount
        price = product.get_price_gross(request, with_properties=with_properties, amount=quantity)
        price += _calculate_property_price(request)
        price *= real_quantity
    except TypeError:
        packs = 0.0
        real_quantity = 0.0
        price = 0.0

    html = render_to_string(template_name, RequestContext(request, {
        "price": price,
        "product": product,
        "packs": int(packs),
        "real_quantity": real_quantity,
        "unit": packing_unit,
    }))

    if as_string:
        return html

    result = json.dumps({
        "html": html,
    }, cls=LazyEncoder)

    return HttpResponse(result, content_type='application/json')
Calculates the actual amount of pieces to buy on base on packing
dataset/ETHPy150Open diefenbach/django-lfs/lfs/catalog/views.py/calculate_packing