class Video(OrderingBaseModel, TendenciBaseModel): """ Videos plugin to add embedding based on video url. Uses embed.ly """ title = models.CharField(max_length=200) slug = models.SlugField(_('URL Path'), unique=True, max_length=200) category = models.ForeignKey(Category, null=True, on_delete=models.SET_NULL) video_type = models.ForeignKey(VideoType, null=True, blank=True, on_delete=models.SET_NULL) 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() release_dt = models.DateTimeField(_("Release Date"), null=True) tags = TagField(blank=True, help_text='Tag 1, Tag 2, ...') perms = GenericRelation(ObjectPermission, object_id_field="object_id", content_type_field="content_type") objects = VideoManager() def __str__(self): return self.title def save(self, *args, **kwargs): model = self.__class__ if self.pk is None: # Append try: last = model.objects.order_by('-position')[0] self.position = last.position + 1 except IndexError: # First row self.position = 0 return super(Video, self).save(*args, **kwargs) class Meta: #permissions = (("view_video","Can view video"),) ordering = ('position', ) verbose_name = get_setting('module', 'videos', 'label') or "Video" verbose_name_plural = get_setting('module', 'videos', 'label_plural') or "Videos" app_label = 'videos' def get_absolute_url(self): return reverse('video.details', args=[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 """ url_pattern = r'https:\/\/www\.youtube\.com\/watch\?v=(\w+)' share_pattern = r'https:\/\/youtu\.be\/(\w+)' def repl(x): return 'https://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 height = kwargs.get('height') or 400 return get_oembed_code(self.video_url, width, height) def thumbnail(self): return get_oembed_thumbnail(self.video_url, 600, 400) def is_youtube_video(self): return 'www.youtube.com' in self.video_url def youtube_video_id(self): if self.is_youtube_video(): return get_embed_ready_url(self.video_url).replace( 'https://www.youtube.com/embed/', '') return None
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)