steeldonutcollection/backend/models.py
2022-05-05 17:40:57 +02:00

44 lines
1.2 KiB
Python

from django.db import models
from django.urls import reverse
from datetime import timedelta
from json import loads
from .s3 import build_presigned_url
class Video(models.Model):
id = models.CharField(max_length=11, primary_key=True)
title = models.CharField(max_length=100)
published = models.DateTimeField()
description = models.TextField(blank=True, null=True)
path = models.CharField(max_length=1024)
thumbnail_path = models.CharField(max_length=1024)
original_json = models.JSONField(blank=True, null=True)
def get_video_url(self):
return build_presigned_url(self.path)
def get_thumbnail_url(self):
return build_presigned_url(self.thumbnail_path, timedelta(minutes=5))
def get_absolute_url(self):
return reverse("video", kwargs={"pk": self.id})
@property
def tags(self):
return loads(self.original_json)["tags"]
def __str__(self):
return self.title
class Playlist(models.Model):
id = models.CharField(max_length=34, primary_key=True)
title = models.CharField(max_length=100)
videos = models.ManyToManyField(Video)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("playlist", kwargs={"pk": self.id})