django-bursar / bursar /

from django import forms
from django.db import models
from django.db.models.fields import DecimalField
from widgets import CurrencyWidget

class CurrencyField(DecimalField):

    def __init__(self, *args, **kwargs):
        self.places = kwargs.pop('display_decimal', 2)
        super(CurrencyField, self).__init__(*args, **kwargs)

    def formfield(self, **kwargs):
        defaults = {
            'max_digits': self.max_digits,
            'decimal_places': self.decimal_places,
            'form_class': forms.DecimalField,
            'widget': CurrencyWidget,
        return super(CurrencyField, self).formfield(**defaults)

class RoundedDecimalField(forms.Field):
    def clean(self, value):
        Normalize the field according to cart normalizing rules.
        cartplaces = config_value('SHOP', 'CART_PRECISION')
        roundfactor = config_value('SHOP', 'CART_ROUNDING')    

        if not value or value == '':
            value = Decimal(0)

            value = round_decimal(val=value, places=cartplaces, roundfactor=roundfactor, normalize=True)
        except RoundedDecimalError:
            raise forms.ValidationError(_('%(value)s is not a valid number') % {'value' : value})

        return value

class PositiveRoundedDecimalField(RoundedDecimalField):
    Normalize the field according to cart normalizing rules and force it to be positive.
    def clean(self, value):
        value = super(PositiveRoundedDecimalField, self).clean(value)
        if value<0:
            log.debug('bad val: %s', value)
            raise forms.ValidationError(_('Please enter a positive number'))

        return value
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.