Flatten the post data.
[audio.git] / src / archive / models.py
index 3be61d4..1fc033b 100644 (file)
@@ -1,8 +1,11 @@
+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 _
@@ -27,6 +30,8 @@ class Project(models.Model):
     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")
@@ -44,15 +49,13 @@ class Project(models.Model):
             " 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):
@@ -63,6 +66,7 @@ class Audiobook(models.Model):
     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'),
@@ -81,7 +85,7 @@ class Audiobook(models.Model):
     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'))
@@ -222,5 +226,26 @@ class Audiobook(models.Model):
 
     @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
+                )
+            )