2019-06-03 14:59:18 +00:00
|
|
|
'''
|
|
|
|
Custom model fields for the Save Point Radio project.
|
|
|
|
'''
|
|
|
|
|
|
|
|
from django.db import models
|
2019-06-06 19:45:56 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-06-03 14:59:18 +00:00
|
|
|
|
2019-06-06 19:45:56 +00:00
|
|
|
from core.validators import RadioIRIValidator
|
|
|
|
|
|
|
|
from .forms import RadioIRIFormField
|
2019-06-03 14:59:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RadioIRIField(models.TextField):
|
|
|
|
'''
|
|
|
|
A custom URL model field that allows schemes that match those from
|
|
|
|
Liquidsoap. This is necessary due to a bug in how Django currently
|
|
|
|
handles custom URLFields:
|
|
|
|
|
|
|
|
https://code.djangoproject.com/ticket/25594
|
|
|
|
https://stackoverflow.com/questions/41756572/
|
|
|
|
'''
|
2019-06-03 18:41:23 +00:00
|
|
|
|
2019-06-06 19:45:56 +00:00
|
|
|
default_validators = [RadioIRIValidator()]
|
2019-06-03 14:59:18 +00:00
|
|
|
description = _("Long IRI")
|
|
|
|
|
|
|
|
def __init__(self, verbose_name=None, name=None, **kwargs):
|
|
|
|
# This is a limit for Internet Explorer URLs
|
|
|
|
kwargs.setdefault('max_length', 2000)
|
|
|
|
super().__init__(verbose_name, name, **kwargs)
|
|
|
|
|
|
|
|
def deconstruct(self):
|
|
|
|
name, path, args, kwargs = super().deconstruct()
|
|
|
|
if kwargs.get("max_length") == 2000:
|
|
|
|
del kwargs['max_length']
|
|
|
|
return name, path, args, kwargs
|
|
|
|
|
|
|
|
def formfield(self, **kwargs):
|
|
|
|
return super().formfield(**{
|
|
|
|
'form_class': RadioIRIFormField,
|
|
|
|
**kwargs,
|
|
|
|
})
|