mirror of
https://github.com/Hopiu/django-embed-video.git
synced 2026-05-02 10:14:41 +00:00
embed video form and model fields
This commit is contained in:
parent
642c4df241
commit
da22d8dffa
1 changed files with 26 additions and 0 deletions
26
embed_video/fields.py
Normal file
26
embed_video/fields.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
from django.db import models
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from .base import detect_backend
|
||||
|
||||
__all__ = ('EmbedVideoField', 'EmbedVideoFormField')
|
||||
|
||||
|
||||
class EmbedVideoField(models.URLField):
|
||||
def formfield(self, **kwargs):
|
||||
defaults = {'form_class': EmbedVideoFormField}
|
||||
defaults.update(kwargs)
|
||||
return super(EmbedVideoField, self).formfield(**defaults)
|
||||
|
||||
|
||||
class EmbedVideoFormField(forms.URLField):
|
||||
def validate(self, url):
|
||||
super(EmbedVideoFormField, self).validate(url)
|
||||
|
||||
try:
|
||||
detect_backend(url)
|
||||
except:
|
||||
raise forms.ValidationError(_(u'URL could not be recognized.'))
|
||||
|
||||
return url
|
||||
Loading…
Reference in a new issue