django-embed-video/embed_video/fields.py

38 lines
1.1 KiB
Python
Raw Normal View History

2013-06-01 12:05:25 +00:00
from django.db import models
from django import forms
from django.utils.translation import ugettext_lazy as _
2013-06-18 22:30:53 +00:00
from .base import detect_backend, NoIdVideoFound, UnknownBackendException
2013-06-01 12:05:25 +00:00
__all__ = ('EmbedVideoField', 'EmbedVideoFormField')
class EmbedVideoField(models.URLField):
def formfield(self, **kwargs):
defaults = {'form_class': EmbedVideoFormField}
defaults.update(kwargs)
return super(EmbedVideoField, self).formfield(**defaults)
2013-06-01 13:02:19 +00:00
def south_field_triple(self):
from south.modelsinspector import introspector
cls_name = '%s.%s' % (
self.__class__.__module__,
self.__class__.__name__
)
args, kwargs = introspector(self)
return (cls_name, args, kwargs)
2013-06-01 12:05:25 +00:00
class EmbedVideoFormField(forms.URLField):
def validate(self, url):
super(EmbedVideoFormField, self).validate(url)
try:
detect_backend(url)
2013-06-18 22:30:53 +00:00
except UnknownBackendException:
2013-06-01 12:05:25 +00:00
raise forms.ValidationError(_(u'URL could not be recognized.'))
2013-06-18 22:30:53 +00:00
except NoIdVideoFound:
raise forms.ValidationError(_(u'Video Id not found .'))
2013-06-01 12:05:25 +00:00
return url