import os import uuid from django.db import models from django.urls import reverse from django.template.defaultfilters import slugify from django.conf import settings # optional external dependencies try: from licenses.models import License except: License = None from taggit.managers import TaggableManager from mutagen.mp3 import MP3 from media.models import LuxAudio, LuxImage def get_show_upload_folder(instance, pathname): "A standardized pathname for uploaded files and images." root, ext = os.path.splitext(pathname) return "{0}/podcasts/{1}/{2}{3}".format( settings.PODCASTING_IMG_PATH, instance.slug, slugify(root), ext ) def get_episode_upload_folder(instance, pathname): "A standardized pathname for uploaded files and images." root, ext = os.path.splitext(pathname) if instance.shows.count() == 1: return "{0}/podcasts/{1}/episodes/{2}{3}".format( settings.PODCASTING_IMG_PATH, instance.shows.all()[0].slug, slugify(root), ext ) else: return "{0}/podcasts/episodes/{1}/{2}{3}".format( settings.PODCASTING_IMG_PATH, instance.slug, slugify(root), ext ) MIME_CHOICES = ( ("mp3", "audio/mpeg"), ("mp4", "audio/mp4"), ("ogg", "audio/ogg"), ) #audio = MP3("example.mp3") #print(audio.info.length) class Podcast(models.Model): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) title = models.CharField(max_length=255) subtitle = models.CharField(max_length=255, blank=True, null=True) slug = models.SlugField() publisher = models.CharField(max_length=255) publisher_email = models.CharField(max_length=255) description = models.TextField() keywords = models.CharField(max_length=255, blank=True, help_text="A comma-delimited list of words for searches, up to 12;") license = models.TextField(blank=True, null=True) featured_image = models.ForeignKey(LuxImage, on_delete=models.CASCADE, null=True, blank=True, help_text=("square JPEG (.jpg) or PNG (.png) image at a size of 1400x1400 pixels.")) class Meta: verbose_name = "Podcast" verbose_name_plural = "Podcasts" ordering = ("title", "slug") def __str__(self): return self.title def get_absolute_url(self): return reverse("podcasts:list", kwargs={"slug": self.slug}) def ttl(self): return "1440" class Episode(models.Model): """ An individual podcast episode and it's unique attributes. """ id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) title = models.CharField(max_length=255) subtitle = models.CharField(max_length=255, blank=True, null=True) date_created = models.DateTimeField(auto_now_add=True, editable=False) date_updated = models.DateTimeField(auto_now=True, editable=False) pub_date = models.DateTimeField(null=True, blank=True) podcast = models.ForeignKey(Podcast, on_delete=models.PROTECT) enable_comments = models.BooleanField(default=True) slug = models.SlugField() PUB_STATUS = ( (0, 'Draft'), (1, 'Published'), ) status = models.IntegerField(choices=PUB_STATUS, default=0) description = models.TextField() featured_image = models.ForeignKey(LuxImage, on_delete=models.CASCADE, null=True, blank=True, help_text=("square JPEG (.jpg) or PNG (.png) image at a size of 1400x1400 pixels.")) # iTunes specific fields keywords = models.CharField(max_length=255, blank=True, help_text="A comma-delimited list of words for searches, up to 12;") class Meta: verbose_name = "Episode" verbose_name_plural = "Episodes" ordering = ("-pub_date", "slug") def __str__(self): return self.title def get_absolute_url(self): return reverse("podcasting_episode_detail", kwargs={"show_slug": self.shows.all()[0].slug, "slug": self.slug}) def get_next(self): next = self.__class__.objects.filter(published__gt=self.published) try: return next[0] except IndexError: return False def get_prev(self): prev = self.__class__.objects.filter(published__lt=self.published).order_by("-published") try: return prev[0] except IndexError: return False def get_explicit_display(self): return "no" def seconds_total(self): try: return self.minutes * 60 + self.seconds except: return 0