import io
from os import unlink
-from tempfile import NamedTemporaryFile
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.template import Template, Context
class YouTube(models.Model):
+ name = models.CharField(max_length=255)
title_template = models.CharField(max_length=1024, blank=True)
description_template = models.TextField(blank=True)
category = models.IntegerField(null=True, blank=True, choices=[
verbose_name = _("YouTube configuration")
verbose_name_plural = _("YouTube configurations")
+ def __str__(self):
+ return self.name
+
def get_context(self, audiobook):
return Context(dict(
audiobook=audiobook,
class ThumbnailTemplate(models.Model):
+ youtube = models.ForeignKey(YouTube, models.CASCADE)
order = models.SmallIntegerField()
is_active = models.BooleanField()
background = models.FileField(upload_to='youtube/thumbnail')
definition = models.TextField()
+ authors = models.CharField(max_length=255, blank=True)
+ epochs = models.CharField(max_length=255, blank=True)
+ kinds = models.CharField(max_length=255, blank=True)
genres = models.CharField(max_length=255, blank=True)
collections = models.CharField(max_length=255, blank=True)
def generate(self, audiobook):
try:
+ title = audiobook.book['title']
+ if audiobook.book.get('parent'):
+ parent_title = audiobook.book['parent']['title']
+ if not title.startswith(parent_title):
+ title = ", ".join((parent_title, title))
+
img = create_thumbnail(
self.background.path,
self.definition,
{
"author": ', '.join((a['name'] for a in audiobook.book['authors'])),
- "title": audiobook.book['title'],
- "part": (audiobook.youtube_volume or audiobook.part_name).strip(),
+ "title": title,
+ "part": (audiobook.youtube_volume or audiobook.part_name).strip() if audiobook.youtube_volume_count > 1 else '',
},
lambda name: Font.objects.get(name=name).truetype.path
)
return buf
def is_for_audiobook(self, audiobook):
- if self.genres:
- book_genres = set([g['slug'] for g in audiobook.book['genres']])
- template_genres = set([g.strip() for g in self.genres.split(',')])
- if not book_genres.intersection(template_genres):
- return False
+ for category in 'authors', 'epochs', 'kinds', 'genres':
+ if getattr(self, category):
+ book_slugs = set([g['slug'] for g in audiobook.book[category]])
+ template_slugs = set([g.strip() for g in getattr(self, category).split(',')])
+ if not book_slugs.intersection(template_slugs):
+ return False
if self.collections:
template_collections = set([g.strip() for g in self.collections.split(',')])