Add project icon, description.
[audio.git] / src / archive / models.py
index 3be61d4..869641c 100644 (file)
@@ -1,3 +1,4 @@
+import io
 import json
 import os.path
 
@@ -27,6 +28,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")
@@ -45,16 +48,6 @@ class Project(models.Model):
         )
 
 
-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 source_upload_to(intance, filename):
     return os.path.join(FILES_SAVE_PATH, filename) # FIXME: what about really long file names?
 
@@ -63,6 +56,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 +75,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 +216,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
+                )
+            )