Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
ideas/fields//models: introduce CustomIntegerField that raises valida…
Browse files Browse the repository at this point in the history
…tion error for decimals with only zeros and use that in models
  • Loading branch information
Rine authored and fuzzylogic2000 committed Feb 23, 2021
1 parent a69db99 commit 037e7c9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
22 changes: 22 additions & 0 deletions apps/ideas/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import re

from django.db import models
from django.forms import fields


class CustomIntegerField(models.IntegerField):

def formfield(self, **kwargs):
return super().formfield(**{
'form_class': CustomIntegerFormField,
**kwargs,
})


class CustomIntegerFormField(fields.IntegerField):
"""
re_decimal is a regex that is sliced from the entered values, the original
regex (zeros after a decimal point) is overwritten here, in order to raise
a validation error for decimal values with only zeros after decimal
"""
re_decimal = re.compile(r'')
3 changes: 2 additions & 1 deletion apps/ideas/models/sections/applicant_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django_countries.fields import CountryField
from multiselectfield import MultiSelectField

from apps.ideas import fields as custom_fields
from apps.ideas.countries import EuropeanCountries

NON_PROFIT = 'NP'
Expand Down Expand Up @@ -131,7 +132,7 @@ class ApplicantSection(models.Model):
blank=True,
verbose_name=_('Lead organization email')
)
year_of_registration = models.IntegerField(
year_of_registration = custom_fields.CustomIntegerField(
blank=True,
null=True,
validators=[
Expand Down
8 changes: 5 additions & 3 deletions apps/ideas/models/sections/finances_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.db import models
from django.utils.translation import ugettext as _

from apps.ideas import fields as custom_fields

TOTAL_BUDGET_HELP = _('Please indicate your overall budget. '
'The total budget may (but does not '
'have to) include the applicant’s own '
Expand Down Expand Up @@ -29,14 +31,14 @@


class FinancesSection(models.Model):
total_budget = models.IntegerField(
total_budget = custom_fields.CustomIntegerField(
verbose_name=_('Total budget'),
help_text=TOTAL_BUDGET_HELP,
validators=[
MinValueValidator(0)
]
)
budget_requested = models.IntegerField(
budget_requested = custom_fields.CustomIntegerField(
verbose_name=_('Funding requested from Civic Europe'),
help_text=BUDGET_REQUESTED_HELP,
validators=[
Expand All @@ -49,7 +51,7 @@ class FinancesSection(models.Model):
verbose_name=_('Major expenses'),
help_text=MAJOR_EXPENSES_HELP
)
duration = models.IntegerField(
duration = custom_fields.CustomIntegerField(
verbose_name=_('Duration of idea (number of months)'),
help_text=DURATION_HELP
)
Expand Down

0 comments on commit 037e7c9

Please sign in to comment.