diff --git a/static/js/video.js b/static/js/video.js new file mode 100644 index 0000000..716009b --- /dev/null +++ b/static/js/video.js @@ -0,0 +1,24 @@ +function init() { + + var hash = document.location.hash; + var res = hash.match(/t=([0-9]+)/); + if (res) { + var vids = document.getElementsByTagName('video'); + // only first video + var vid = vids[0]; + vid.currentTime = res[1]; + vid.autoplay = true; + } +} + +init(); + +function vidTimeInUrl(el) { + var vids = document.getElementsByTagName('video'); + // only first video + var vid = vids[0]; + var currentTime = vid.currentTime; + var href = el.href + "#t=" + parseInt(currentTime); + el.href = href; + return true; +} diff --git a/videodinges/models.py b/videodinges/models.py index 9d4437a..994c484 100644 --- a/videodinges/models.py +++ b/videodinges/models.py @@ -58,6 +58,9 @@ class Video(models.Model): def __str__(self): return self.title + class Meta: + indexes = [models.Index(fields=['slug']), models.Index(fields=['created_at'])] + class Transcoding(models.Model): id = models.AutoField(primary_key=True) video = models.ForeignKey(Video, on_delete=models.CASCADE, related_name='transcodings') diff --git a/videodinges/settings.py b/videodinges/settings.py index 99afb1a..099943e 100644 --- a/videodinges/settings.py +++ b/videodinges/settings.py @@ -66,6 +66,12 @@ TEMPLATES = [ ], }, }, + { + 'BACKEND': 'django.template.backends.jinja2.Jinja2', + 'DIRS': [os.path.join(BASE_DIR, 'videodinges', 'templates')], + 'APP_DIRS': False, + 'OPTIONS': {}, + }, ] WSGI_APPLICATION = 'videodinges.wsgi.application' diff --git a/videodinges/templates/video.html.j2 b/videodinges/templates/video.html.j2 new file mode 100644 index 0000000..4bd7555 --- /dev/null +++ b/videodinges/templates/video.html.j2 @@ -0,0 +1,31 @@ + + + {# #} + {% if og_image %} + + {% endif %} + {{ title }} + + +

{{ title }}

+
+

+{% for quality in qualities %} +{{ quality }} versie{% if not loop.last %} | {% endif %} +{% endfor %} +

+

+{{ description|safe }} +

+
+
0 comments total
+
+ + + + \ No newline at end of file diff --git a/videodinges/urls.py b/videodinges/urls.py index 54630d6..c1121d3 100644 --- a/videodinges/urls.py +++ b/videodinges/urls.py @@ -16,10 +16,11 @@ Including another URLconf from django.conf.urls import url from django.contrib import admin -from . import testviews +from . import testviews, views urlpatterns = [ url(r'^admin/', admin.site.urls), + url(r'^(?P[\w-]+).html', views.video) ] for i in testviews.__all__: diff --git a/videodinges/views.py b/videodinges/views.py new file mode 100644 index 0000000..585b9de --- /dev/null +++ b/videodinges/views.py @@ -0,0 +1,66 @@ +from collections import defaultdict +from typing import List, Dict, Any + +from django.http import HttpResponse, HttpRequest, Http404 +from django.shortcuts import render +from . import models + +def video(request: HttpRequest, slug: str) -> HttpResponse: + try: + video = models.Video.objects.get(slug=slug) + except models.Video.DoesNotExist: + raise Http404('Video not found') + + template_data = dict( + og_image=video.og_image.file.url if video.og_image else None, + title=video.title, + poster=video.poster.file.url if video.poster else None, + description=video.description, + slug=video.slug + ) + + qualities = _get_qualities(video) + try: + quality = qualities[request.GET['quality']] + except: + quality = next(iter(qualities.values())) + + template_data.update( + width=quality[0].quality_obj.width, + height=quality[0].quality_obj.height, + ) + + template_data['sources'] = [ + { + 'src': _url_for(transcoding), + 'type': transcoding.type, + } + for transcoding in quality ] + + template_data['used_codecs'] = [ + models.get_short_name_of_transcoding_type(transcoding.type) + for transcoding in quality + ] + + template_data['qualities'] = qualities.keys() + + return render(request, 'video.html.j2', template_data, using='jinja2') + +def _get_dict_from_models_with_fields(model, *fields: str) -> Dict[str, Any]: + ret = {} + for field in fields: + ret[field] = model.__dict__[field] + return ret + +def _get_qualities(video: models.Video) -> Dict[str, List[models.Transcoding]]: + transcodings: List[models.Transcoding] = video.transcodings.order_by('quality').all() + qualities = defaultdict(list) + for transcoding in transcodings: + qualities[transcoding.quality_obj.name].append(transcoding) + return qualities + +def _url_for(transcoding: models.Transcoding) -> str: + if transcoding.url: + return transcoding.url + elif transcoding.upload: + return transcoding.upload.file.url