# -*- coding: utf-8 -*-
# Akvo RSR is covered by the GNU Affero General Public License.
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.
from django.conf import settings
from django.core.validators import MinValueValidator, MaxValueValidator, MaxLengthValidator
from django.db import models
from django.utils.translation import gettext_lazy as _
from akvo.rsr.validators import string_validator
[docs]class NullCharField(models.CharField):
description = "CharField that stores NULL but returns an empty string"
[docs] def to_python(self, value):
if isinstance(value, models.CharField):
return value
if value is None:
return ''
return value
[docs] def get_db_prep_value(self, value, connection, prepared=False):
if not value:
return None
return value
[docs]class LatitudeField(models.FloatField):
description = _('Latitude coordinate.')
def __init__(self, *args, **kwargs):
super(LatitudeField, self).__init__(*args, **kwargs)
self.validators = [MinValueValidator(-90), MaxValueValidator(90)]
[docs]class LongitudeField(models.FloatField):
description = _('Longitude coordinate.')
def __init__(self, *args, **kwargs):
super(LongitudeField, self).__init__(*args, **kwargs)
self.validators = [MinValueValidator(-180), MaxValueValidator(180)]
[docs]class ValidXMLCharField(models.CharField):
description = "A CharField containing only valid XML characters"
def __init__(self, *args, **kwargs):
super(ValidXMLCharField, self).__init__(*args, **kwargs)
self.validators += [string_validator]
[docs]class ValidXMLTextField(models.TextField):
description = "A TextField containing only valid XML characters"
def __init__(self, *args, **kwargs):
super(ValidXMLTextField, self).__init__(*args, **kwargs)
self.validators += [string_validator]
[docs]class LimitedTextField(ValidXMLTextField):
description = "A TextField that honors the max_length param"
def __init__(self, *args, **kwargs):
try:
max_length = kwargs['max_length']
except KeyError:
max_length = None
super(LimitedTextField, self).__init__(*args, **kwargs)
if max_length:
self.validators += [MaxLengthValidator(max_length)]
[docs]class ProjectLimitedTextField(LimitedTextField):
description = "A TextField that honors the max_length param for 'new' projects"
[docs] def clean(self, value, model_instance):
""" Don't apply the MaxLengthValidator to "old" projects. OLD_PROJECT_MAX_ID should be lowered as projects are
brought within the field length limits and eventually the OLD_PROJECT_MAX_ID can be removed
"""
if model_instance.id and model_instance.id <= getattr(settings, 'OLD_PROJECT_MAX_ID', 0):
self.validators = [v for v in self.validators if not isinstance(v, MaxLengthValidator)]
return super(ProjectLimitedTextField, self).clean(value, model_instance)