Support for Django up to 2.2.
[fnpdjango.git] / fnpdjango / utils / fields.py
1 # -*- coding: utf-8 -*-
2 from django.forms import CharField, ValidationError
3 from .text.textilepl import textile_pl
4
5 try:
6     from markupfield.fields import MarkupField
7 except ImportError:
8     MarkupField = None
9 else:
10     def validate_textile(value):
11         try:
12             textile_pl(value)
13         except Exception:
14             raise ValidationError('Syntax error in markup.')
15
16
17     class TextileFormField(CharField):
18         default_validators = [validate_textile]
19
20
21     class TextileField(MarkupField):
22         def formfield(self, **kwargs):
23             defaults = {'form_class': TextileFormField}
24             defaults.update(kwargs)
25             return super(MarkupField, self).formfield(**defaults)