Demo entry 2622414

python

   

Submitted by anonymous on Sep 03, 2015 at 18:15
Language: Python. Code size: 1.1 kB.

def round_to_base(x, base=10):
    """Round interger using base, i.e. to base digit. If base=5, then 16->15, 18->20."""
    return int(base * round(float(x)/base))

def set_artwork_prices(artwork):
    """
    Calculate prices on artwork using currency rates.
    If price is lower than 1000, round integer to nearest 5, else  - > to nearest 10(default)
    """
    from collectables.models import Currency
    if artwork.is_dirty() and artwork.get_dirty_fields().get('price_usd') or not artwork.price_gbp:
        if artwork.price_usd:
            gbp_price = artwork.price_usd/Currency.objects.get(short_name__iexact='usd').rate
            eur_price = Currency.objects.get(short_name__iexact='eur').rate * gbp_price
            if artwork.price_gbp > 1000:
                gbp_price = round_to_base(gbp_price)
                eur_price = round_to_base(eur_price)
            else:
                gbp_price = round_to_base(gbp_price, 5)
                eur_price = round_to_base(eur_price, 5)
            artwork.price_gbp = gbp_price
            artwork.price_eur = eur_price
    return artwork

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).