Last active
October 6, 2017 16:56
-
-
Save vbmendes/289690 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from fields import DecimalInput | |
class ExampleForm(forms.Form): | |
meu_campo = DeicmalInput() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
from django import forms | |
import widgets | |
class DecimalField(forms.DecimalField): | |
widget = widgets.DecimalInput | |
def clean(self, value): | |
value = super(DecimalField, self).clean(value) | |
value = value or 0 | |
try: | |
return float(value) | |
except ValueError: | |
raise forms.ValidationError(u'Informe um valor válido, por exemplo 1.000,00 ou 2,45.') | |
except TypeError: | |
raise forms.ValidationError(u'Informe um valor válido, por exemplo 1.000,00 ou 2,45.') | |
def widget_attrs(self, widget): | |
return {'class': 'decimal'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.template.defaultfilters import floatformat | |
def add_class_to_attrs(attrs, new_class): | |
classes = attrs.get('class') | |
if classes: | |
classes = ' '.join([classes, new_class]) | |
else: | |
classes = new_class | |
attrs['class'] = classes | |
def floatdot(value,precision=-1): | |
value = floatformat(value,precision) | |
value = force_unicode(value) | |
value = value.replace('.',',') | |
return intdot(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from utils import floatdot, add_class_to_attrs | |
class DecimalInput(forms.TextInput): | |
def render(self, name, value, attrs=None): | |
attrs = attrs if attrs else {} | |
value = value or '' | |
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name) | |
if isinstance(value, float): | |
value = "%s" % floatdot(value, 2) | |
add_class_to_attrs(final_attrs, 'decimal') | |
final_attrs['alt'] = final_attrs.get('alt', 'decimal') | |
return super(DecimalInput, self).render(name, value, final_attrs) | |
def value_from_datadict(self, data, files, name): | |
value = data.get(name, None) | |
if value: | |
value = value.replace('.', '').replace(',','.') # Remove Commas and points | |
return value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment