Skip to content

Instantly share code, notes, and snippets.

@armonge
Created May 29, 2012 19:04
Show Gist options
  • Save armonge/2830057 to your computer and use it in GitHub Desktop.
Save armonge/2830057 to your computer and use it in GitHub Desktop.
django youtube field
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
@goinnn
Copy link

goinnn commented May 7, 2013

Great! I added some details... in my fork.

@gladson
Copy link

gladson commented May 20, 2013

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