This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.py
191 lines (149 loc) · 6.32 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from django.db import models
from django.core.urlresolvers import reverse
from embedly import Embedly
from django.contrib.contenttypes.fields import GenericRelation
from tendenci.apps.perms.object_perms import ObjectPermission
from tagging.fields import TagField
from tendenci.apps.perms.models import TendenciBaseModel
from tinymce import models as tinymce_models
from videos.managers import VideoManager
from tendenci.apps.site_settings.utils import get_setting
client = Embedly("438be524153e11e18f884040d3dc5c07")
class Category(models.Model):
name = models.CharField(max_length=200, unique=True)
slug = models.SlugField(unique=True)
def __unicode__(self):
return self.name
class Meta:
verbose_name_plural = "Categories"
app_label = 'videos'
def get_absolute_url(self):
return reverse('video.category', args=[self.slug])
class VideoType(models.Model):
name = models.CharField(max_length=200, unique=True)
slug = models.SlugField(unique=True)
def __unicode__(self):
return self.name
class Meta:
verbose_name_plural = "Video Types"
app_label = 'videos'
class Video(TendenciBaseModel):
"""
Videos plugin to add embedding based on video url. Uses embed.ly
"""
title = models.CharField(max_length=200)
slug = models.SlugField(unique=True, max_length=200)
category = models.ForeignKey(Category)
video_type = models.ForeignKey(VideoType, null=True, blank=True)
image = models.ImageField(upload_to='uploads/videos/%y/%m', blank=True)
video_url = models.CharField(max_length=500, help_text='Youtube, Vimeo, etc..')
description = tinymce_models.HTMLField()
tags = TagField(blank=True, help_text='Tag 1, Tag 2, ...')
ordering = models.IntegerField(blank=True, null=True)
perms = GenericRelation(ObjectPermission,
object_id_field="object_id",
content_type_field="content_type")
objects = VideoManager()
def __unicode__(self):
return self.title
def save(self, *args, **kwargs):
model = self.__class__
if self.ordering is None:
# Append
try:
last = model.objects.order_by('-ordering')[0]
self.ordering = last.ordering + 1
except IndexError:
# First row
self.ordering = 0
return super(Video, self).save(*args, **kwargs)
class Meta:
permissions = (("view_video","Can view video"),)
ordering = ('ordering',)
verbose_name = get_setting('module', 'videos', 'label') or "Video"
verbose_name_plural = get_setting('module', 'videos', 'label_plural') or "Videos"
app_label = 'videos'
@models.permalink
def get_absolute_url(self):
return ("video.details", [self.slug])
def video_embed_url(self):
"""
Returns a youtube embed URL
Attempts to convert common YouTube URL patterns
to the URL embed pattern.
TODO: Contribute more video service embed URL's
"""
import re
url_pattern = r'http:\/\/www\.youtube\.com\/watch\?v=(\w+)'
share_pattern = r'http:\/\/youtu\.be\/(\w+)'
repl = lambda x: 'http://www.youtube.com/embed/%s' % x.group(1)
if re.match(url_pattern, self.video_url):
return re.sub(url_pattern, repl, self.video_url)
if re.match(share_pattern, self.video_url):
return re.sub(share_pattern, repl, self.video_url)
return self.video_url
def embed_code(self, **kwargs):
width = kwargs.get('width') or 600
return get_oembed_code(self.video_url, width, 400)
def thumbnail(self):
return get_oembed_thumbnail(self.video_url, 600, 400)
class OembedlyCache(models.Model):
"For better performance all oembed queries are cached in this model"
url = models.CharField(max_length=800)
width = models.IntegerField(db_index=True)
height = models.IntegerField(db_index=True)
code = models.TextField()
thumbnail = models.CharField(max_length=800)
class Meta:
app_label = 'videos'
def __unicode__(self):
return self.url
@staticmethod
def get_thumbnail(url, width, height):
try:
return OembedlyCache.objects.filter(url=url, width=width, height=height)[0].thumbnail
except IndexError:
try:
result = client.oembed(url, format='json', maxwidth=width, maxheight=height)
thumbnail = result['thumbnail_url']
code = result['html']
except KeyError:
return False
except Exception, e:
return False
obj = OembedlyCache(url=url, width=width, height=height, thumbnail=thumbnail, code=code)
obj.save()
return thumbnail
@staticmethod
def get_code(url, width, height):
try:
instance = OembedlyCache.objects.filter(url=url, width=width, height=height)[0]
code = instance.code
# find and replace https: with blank for embed to become protocol independent
if 'https:' in code:
code = code.replace('https:', '')
if 'http:' in code:
code = code.replace('http:', '')
instance.code = code
instance.save()
return code
except IndexError:
try:
result = client.oembed(url, format='json', maxwidth=width, maxheight=height)
thumbnail = result['thumbnail_url']
code = result['html']
if 'https:' in code:
code = code.replace('https:', '')
if 'http:' in code:
code = code.replace('http:', '')
except KeyError:
return 'Unable to embed code for <a href="%s">%s</a>' % (url, url)
except Exception, e:
return 'Unable to embed code for <a href="%s">%s</a><br>Error: %s' % (url, url, e)
obj = OembedlyCache(url=url, width=width, height=height, code=code, thumbnail=thumbnail)
obj.save()
return code
def get_oembed_code(url, width, height):
return OembedlyCache.get_code(url, width, height)
def get_oembed_thumbnail(url, width, height):
return OembedlyCache.get_thumbnail(url, width, height)