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 tp.storage_backends import PublicImageStorage, PublicMP3Storage
|
2021-03-17 11:28:51 -07:00
|
|
|
from .choices import LICENSE_CHOICES, get_license_info
|
2021-02-24 20:13:54 -08:00
|
|
|
import string, random
|
|
|
|
|
|
|
|
|
|
|
|
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-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=PublicImageStorage(),
|
|
|
|
upload_to=slugify_file_name,
|
|
|
|
null=True, blank=True)
|
|
|
|
|
2021-03-16 11:22:44 -07:00
|
|
|
image_license = models.SmallIntegerField(
|
|
|
|
choices=LICENSE_CHOICES,
|
|
|
|
default=2,
|
|
|
|
)
|
2021-03-22 15:06:49 -07:00
|
|
|
|
2021-03-16 11:22:44 -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='')
|
2021-03-17 11:28:51 -07:00
|
|
|
image_license_jurisdiction = models.TextField(null=False, default='(no jurisdiction specified)')
|
2021-03-16 11:22:44 -07:00
|
|
|
|
2021-03-16 14:21:58 -07:00
|
|
|
@property
|
|
|
|
def image_license_name(self):
|
2021-03-17 11:28:51 -07:00
|
|
|
return(get_license_info(self.image_license))[0]
|
2021-03-16 14:21:58 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def image_license_url(self):
|
2021-03-17 11:28:51 -07:00
|
|
|
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):
|
2021-03-17 11:28:51 -07:00
|
|
|
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')
|
|
|
|
|
2021-03-17 11:28:51 -07:00
|
|
|
@property
|
|
|
|
def license_name(self):
|
2021-03-17 12:32:08 -07:00
|
|
|
return(get_license_info(self.license))[0]
|
2021-03-17 11:28:51 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def license_url(self):
|
2021-03-17 12:32:08 -07:00
|
|
|
return(get_license_info(self.license))[1]
|
2021-03-16 14:21:58 -07:00
|
|
|
|
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-03-16 14:43:22 -07:00
|
|
|
|
2021-02-24 20:13:54 -08:00
|
|
|
mp3 = models.FileField(
|
|
|
|
storage=PublicMP3Storage(),
|
|
|
|
upload_to=slugify_file_name,
|
|
|
|
null=True, blank=True)
|