trentpalmerdotorg/audio/models.py

102 lines
3.3 KiB
Python
Raw Normal View History

2021-02-24 20:13:54 -08:00
from django.db import models
2021-04-05 20:49:54 -07:00
from django.conf import settings
2021-02-24 20:13:54 -08:00
from tp.models import UUIDAsIDModel
from django.contrib.auth.models import User
from django.utils.text import slugify
from django.core.files.storage import FileSystemStorage
from .choices import LICENSE_CHOICES, get_license_info
import string
import random
2021-02-24 20:13:54 -08:00
def rand_slug():
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(8))
def slugify_file_name(instance, filename):
fname, dot, extension = filename.rpartition('.')
slug = slugify(fname)
return f"{slug}.{extension}"
2021-02-24 20:13:54 -08:00
2021-03-22 15:06:49 -07:00
class EpisodeAndFeed(UUIDAsIDModel):
2021-02-24 20:13:54 -08:00
user = models.ForeignKey(User, on_delete=models.CASCADE)
author = models.CharField(max_length=255)
2021-03-22 15:06:49 -07:00
title = models.CharField(max_length=255)
created_on = models.DateTimeField(auto_now_add=True)
2021-02-24 20:13:54 -08:00
slug = models.SlugField(max_length=255, unique=True)
description = models.TextField(null=False)
2021-03-22 15:06:49 -07:00
2021-02-24 20:13:54 -08:00
image = models.ImageField(
storage=FileSystemStorage(location=settings.IMAGE_ROOT),
2021-02-24 20:13:54 -08:00
upload_to=slugify_file_name,
null=True, blank=True)
image_license = models.SmallIntegerField(
choices=LICENSE_CHOICES,
default=2,
)
2021-03-22 15:06:49 -07:00
image_title = models.CharField(max_length=255, default='')
image_attribution = models.CharField(max_length=255, default='')
image_attribution_url = models.URLField(max_length=255, blank=True)
original_image_url = models.URLField(max_length=255, default='')
image_license_jurisdiction = models.TextField(null=False, default='(no jurisdiction specified)')
@property
def image_license_name(self):
return(get_license_info(self.image_license))[0]
@property
def image_license_url(self):
return(get_license_info(self.image_license))[1]
2021-03-22 15:06:49 -07:00
def __str__(self):
return str(self.title)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(rand_slug() + "-" + self.title)
super().save(*args, **kwargs)
class Meta:
abstract = True
class Feed(EpisodeAndFeed):
author_url = models.URLField(max_length=255, default='')
ebook_title = models.CharField(max_length=255, default='')
ebook_url = models.URLField(max_length=255, default='')
translator = models.CharField(max_length=255, blank=True)
translator_url = models.URLField(max_length=255, blank=True)
intro_author = models.CharField(max_length=255, blank=True)
intro_author_url = models.URLField(max_length=255, blank=True)
license_jurisdiction = models.TextField(blank=True, default='(no jurisdiction specified)')
license = models.SmallIntegerField(
choices=LICENSE_CHOICES,
default=1,
)
2021-04-05 20:49:54 -07:00
@property
def get_itpc_rss(self):
return(f'itpc://{settings.DOMAIN_NAME}/rss/{self.slug}.xml')
@property
def license_name(self):
return(get_license_info(self.license))[0]
@property
def license_url(self):
return(get_license_info(self.license))[1]
2021-02-24 20:13:54 -08:00
2021-03-22 15:06:49 -07:00
class Episode(EpisodeAndFeed):
2021-02-24 20:13:54 -08:00
feed = models.ForeignKey(Feed, on_delete=models.CASCADE)
pub_date = models.DateField()
episode_number = models.IntegerField(null=True)
2021-02-24 20:13:54 -08:00
mp3 = models.FileField(
storage=FileSystemStorage(location=settings.MP3_ROOT),
2021-02-24 20:13:54 -08:00
upload_to=slugify_file_name,
null=True, blank=True)