Created
May 29, 2012 19:04
-
-
Save armonge/2830057 to your computer and use it in GitHub Desktop.
django youtube field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urlparse | |
import re | |
from django.db import models | |
from django import forms | |
def validate_youtube_url(value): | |
'''El patron lo saque de http://stackoverflow.com/questions/2964678/jquery-youtube-url-validation-with-regex''' | |
pattern = r'^http:\/\/(?:www\.)?youtube.com\/watch\?(?=.*v=\w+)(?:\S+)?$' | |
if value[:16] == 'http://youtu.be/': | |
if re.match(r'\w+', value[16:]) is None: | |
raise forms.ValidationError(_('Not a valid Youtube URL')) | |
elif re.match(pattern, value) is None: | |
raise forms.ValidationError(_('Not a valid Youtube URL')) | |
class YoutubeUrl(unicode): | |
@property | |
def video_id(self): | |
parsed_url = urlparse.urlparse(self) | |
if parsed_url.query == '': | |
return parsed_url.path | |
return urlparse.parse_qs(parsed_url.query)['v'][0] | |
@property | |
def embed_url(self): | |
return 'http://youtube.com/embed/%s/' % self.video_id | |
@property | |
def thumb(self): | |
return "http://img.youtube.com/vi/%s/2.jpg" % self.video_id | |
class YoutubeUrlField(models.URLField): | |
__metaclass__ = models.SubfieldBase | |
def __init__(self, *args, **kwargs): | |
super(YoutubeUrlField, self).__init__(*args, **kwargs) | |
self.validators.append(validate_youtube_url) | |
def to_python(self, value): | |
url = super(YoutubeUrlField, self).to_python(value) | |
return YoutubeUrl(url) | |
def get_prep_value(self, value): | |
return unicode(value) | |
try: | |
from south.modelsinspector import add_introspection_rules | |
add_introspection_rules([], ["^fields\.YoutubeUrlField"]) | |
except ImportError: | |
pass |
it is also possible to create validation for vimeo:
https://github.com/kaleidos/django-kvideos/blob/master/kvideos/models.py#L28-L40
And create "django youtube and vimeo field"
I do not have this knowledge for this.
Thanks!!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great! I added some details... in my fork.