+import io
import json
import os.path
+from urllib.parse import urljoin
from django.db import models
from time import sleep
+from django.contrib.sites.models import Site
from django.utils.functional import cached_property
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _
name = models.CharField(max_length=128, unique=True, db_index=True, verbose_name="Nazwa")
sponsors = models.TextField(blank=True, null=True, verbose_name="Sponsorzy")
description = models.TextField(blank=True, verbose_name="Opis")
+ youtube = models.ForeignKey('youtube.YouTube', models.PROTECT)
+ icon = models.FileField(upload_to='archive/project', blank=True, null=True)
class Meta:
verbose_name = _("project")
" finansowanego przez %s" % self.sponsors if self.sponsors else "",
)
-
-class Piece(models.Model):
- name = models.CharField(max_length=255)
- source_file = models.FileField(upload_to='piece')
- is_outro = models.BooleanField(default=False)
- min_audiobook_duration = models.IntegerField(default=0)
-
- def __str__(self):
- return self.name
+ def get_icon_url(self):
+ if not self.icon:
+ return ''
+ return urljoin(
+ 'https://' + Site.objects.get_current().domain,
+ self.icon.url
+ )
def source_upload_to(intance, filename):
source_file = models.FileField(upload_to=source_upload_to, max_length=255,
verbose_name=_('source file'), editable=False)
source_sha1 = models.CharField(max_length=40, editable=False)
+ duration = models.FloatField(null=True, editable=False)
title = models.CharField(max_length=255, verbose_name=_('title'))
part_name = models.CharField(max_length=255, verbose_name=_('part name'), help_text=_('eg. chapter in a novel'),
encoded_by = models.CharField(max_length=255, verbose_name=_('encoded by'))
date = models.CharField(max_length=255, verbose_name=_('date'))
project = models.ForeignKey(Project, models.PROTECT, verbose_name=_('project'))
- slug = models.SlugField(max_length=120, help_text=_('WL catalogue slug of the book.'))
+ slug = models.SlugField(max_length=120, blank=True, help_text=_('WL catalogue slug of the book.'))
translator = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('translator'))
modified = models.DateTimeField(null=True, editable=False)
license = models.ForeignKey(License, models.PROTECT, null=True, blank=True, verbose_name=_('license'))
@cached_property
def book(self):
- apidata = requests.get(f'https://wolnelektury.pl/api/books/{self.slug}/').json()
+ if self.slug:
+ apidata = requests.get(f'https://wolnelektury.pl/api/books/{self.slug}/').json()
+ else:
+ return {}
return apidata
+
+ @property
+ def document(self):
+ from librarian.document import WLDocument, parser
+ from lxml import etree
+
+ xml_url = self.book.get('xml', None)
+ if xml_url is None:
+ return None
+
+ return WLDocument(
+ etree.parse(
+ io.BytesIO(
+ requests.get(xml_url).content
+ )
+ ,parser = parser
+ )
+ )