Year validator for a Django (1.2+) IntegerField.
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.core.exceptions import ValidationError | |
import time | |
import re | |
def validate_year(value): | |
""" | |
Validator function for model.IntegerField() | |
* Validates a valid four-digit year. | |
* Must be a current or future year. | |
In your model: | |
year = models.IntegerField(_(u'Year'), help_text=_(u'Current or future year in YYYY format.'), validators=[validate_year], unique=True) | |
""" | |
# Matches any 4-digit number: | |
year_re = re.compile('^\d{4}$') | |
# If year does not match our regex: | |
if not year_re.match(str(value)): | |
raise ValidationError(u'%s is not a valid year.' % value) | |
# Check not before this year: | |
year = int(value) | |
thisyear = time.localtime()[0] | |
if year < thisyear: | |
raise ValidationError(u'%s is a year in the past; please enter a current or future year.' % value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment