Allow Vimeo as well as YouTube URLs. Closes #26.

This commit is contained in:
Anna Sidwell 2018-03-31 17:18:35 +11:00
parent 0f0f78bd24
commit 5f2941d205
4 changed files with 62 additions and 9 deletions

View File

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-03-31 06:04
from __future__ import unicode_literals
import apps.map.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('map', '0045_auto_20180331_0517'),
]
operations = [
migrations.AlterField(
model_name='casestudy',
name='video',
field=models.URLField(blank=True, default='', help_text='Copy the URL to a YouTube or Vimeo video that relates to the case study.', max_length=80, validators=[apps.map.validators.YouTubeOrVimeoValidator()], verbose_name='Video URL'),
preserve_default=False,
),
]

View File

@ -423,13 +423,11 @@ class CaseStudy(models.Model):
# 1.16.1
video = models.URLField(
verbose_name=_("YouTube Video"),
help_text=_("Copy the URL to a related YouTube™ video that relates to the case study."),
max_length=43,
validators=[validators.YoutubeURLValidator()],
blank=True,
default=None,
null=True,
verbose_name=_("Video URL"),
help_text=_("Copy the URL to a YouTube or Vimeo video that relates to the case study."),
max_length=80,
validators=[validators.YouTubeOrVimeoValidator()],
blank=True
)
# 1.16.2
@ -1030,10 +1028,23 @@ class CaseStudy(models.Model):
# Continue normal save method by calling original save method.
super(CaseStudy, self).save(*args, **kwargs)
def get_video_id(self):
def is_video_youtube(self):
return self.video.count("youtube.com") > 0
def get_youtube_id(self):
"""Gets the 11 character YouTube video ID from the video field."""
return parse.parse_qs(parse.urlparse(self.video).query)["v"][0]
def is_video_vimeo(self):
return self.video.count("vimeo.com") > 0
def get_vimeo_id(self):
"""Gets the 11 number video ID from the video field."""
return parse.urlparse(self.video).path
def get_negative_case_reasons_no_other(self):
"""Return a list of negative case reasons, minus the 'other' choice (if selected)"""
choices = self.get_negative_case_reasons_list()

View File

@ -212,7 +212,13 @@ dd ul { padding-left: 0; margin-left: 0; }
<figure>
<div class="embed-responsive embed-responsive-16by9">
<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/{{ case_study.get_video_id }}?rel=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>
{% if case_study.is_video_youtube %}
<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/{{ case_study.get_youtube_id }}?rel=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>
{% elif case_study.is_video_vimeo %}
<iframe src="https://player.vimeo.com/video/{{ case_study.get_vimeo_id }}" width="560" height="315" frameborder="0" allowfullscreen></iframe>
{% else %}
{{ case_study.video }}
{% endif }
</div>
<figcaption>

View File

@ -1,5 +1,19 @@
from django.core.validators import RegexValidator
# Supported formats:
# - http://(www.)youtube.com/watch?v=Dhjiu89G3
# - http://(www.)youtube.com/watch/Dhjiu89G3
# - http://youtu.be/Dhjiu89G3
class YoutubeURLValidator(RegexValidator):
regex = r'https?:\/\/(((www.)?youtube.com\/((watch\?v=)|(watch\/)))|(youtu.be\/))([A-z0-9]{1,11}).+'
# Supported Vimeo formats:
# - http://(www.)vimeo.com/258651879
# - http://player.vimeo.com/video/258651879
class VimeoURLValidator(RegexValidator):
regex = r'https?:\/\/(player|www.)?vimeo.com\/([0-9]{1,11}).+'
class YouTubeOrVimeoValidator(RegexValidator):
regex = r'https?:\/\/(player.|www.)?(vimeo\.com|youtu(be\.com|\.be))\/(video\/|embed\/|watch\?v=|v\/)?([A-Za-z0-9]{1,11}).+'