diff --git a/apps/map/migrations/0046_auto_20180331_0604.py b/apps/map/migrations/0046_auto_20180331_0604.py new file mode 100644 index 0000000..1cc75db --- /dev/null +++ b/apps/map/migrations/0046_auto_20180331_0604.py @@ -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, + ), + ] diff --git a/apps/map/models.py b/apps/map/models.py index 10f0cbd..808465a 100644 --- a/apps/map/models.py +++ b/apps/map/models.py @@ -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() diff --git a/apps/map/templates/map/detail.html b/apps/map/templates/map/detail.html index 0d08b4f..b90f820 100644 --- a/apps/map/templates/map/detail.html +++ b/apps/map/templates/map/detail.html @@ -212,7 +212,13 @@ dd ul { padding-left: 0; margin-left: 0; }
- + {% if case_study.is_video_youtube %} + + {% elif case_study.is_video_vimeo %} + + {% else %} + {{ case_study.video }} + {% endif }
diff --git a/apps/map/validators.py b/apps/map/validators.py index 4d3ab80..77959ff 100644 --- a/apps/map/validators.py +++ b/apps/map/validators.py @@ -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}).+'