4 from django.db import models
6 from django.utils.functional import cached_property
7 from django.utils.translation import gettext_lazy as _
8 from django_pglocks import advisory_lock
10 from archive.constants import status
11 from archive.settings import FILES_SAVE_PATH, ADVERT, ORGANIZATION, PROJECT
12 from archive.utils import OverwriteStorage, sha1_file
15 class License(models.Model):
16 uri = models.CharField(max_length=255, unique=True)
17 name = models.CharField(max_length=255)
20 class Project(models.Model):
21 """ an audiobook project, needed for specyfing sponsors """
23 name = models.CharField(max_length=128, unique=True, db_index=True, verbose_name="Nazwa")
24 sponsors = models.TextField(blank=True, null=True, verbose_name="Sponsorzy")
25 description = models.TextField(blank=True, verbose_name="Opis")
28 verbose_name = _("project")
29 verbose_name_plural = _("projects")
35 def get_description(self):
37 return self.description
38 return "Audiobook nagrany w ramach projektu %s%s." % (
40 " finansowanego przez %s" % self.sponsors if self.sponsors else "",
44 class Piece(models.Model):
45 name = models.CharField(max_length=255)
46 source_file = models.FileField(upload_to='piece')
47 is_outro = models.BooleanField(default=False)
48 min_audiobook_duration = models.IntegerField(default=0)
54 def source_upload_to(intance, filename):
55 return os.path.join(FILES_SAVE_PATH, filename) # FIXME: what about really long file names?
58 class Audiobook(models.Model):
59 source_file = models.FileField(upload_to=source_upload_to, max_length=255,
60 verbose_name=_('source file'), editable=False)
61 source_sha1 = models.CharField(max_length=40, editable=False)
63 title = models.CharField(max_length=255, verbose_name=_('title'))
64 part_name = models.CharField(max_length=255, verbose_name=_('part name'), help_text=_('eg. chapter in a novel'),
65 default='', blank=True)
66 index = models.IntegerField(verbose_name=_('index'), default=0)
67 parts_count = models.IntegerField(verbose_name=_('parts count'), default=1)
68 artist = models.CharField(max_length=255, verbose_name=_('artist'))
69 conductor = models.CharField(max_length=255, verbose_name=_('conductor'))
70 encoded_by = models.CharField(max_length=255, verbose_name=_('encoded by'))
71 date = models.CharField(max_length=255, verbose_name=_('date'))
72 project = models.ForeignKey(Project, models.PROTECT, verbose_name=_('project'))
73 url = models.URLField(max_length=255, verbose_name=_('book url'))
74 translator = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('translator'))
75 modified = models.DateTimeField(null=True, editable=False)
76 license = models.ForeignKey(License, models.PROTECT, null=True, blank=True, verbose_name=_('license'))
79 mp3_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
80 mp3_task = models.CharField(max_length=64, null=True, editable=False)
81 mp3_tags = models.TextField(null=True, editable=False)
82 mp3_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
83 mp3_published_tags = models.TextField(null=True, editable=False)
84 mp3_published = models.DateTimeField(null=True, editable=False)
86 ogg_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
87 ogg_task = models.CharField(max_length=64, null=True, editable=False)
88 ogg_tags = models.TextField(null=True, editable=False)
89 ogg_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
90 ogg_published_tags = models.TextField(null=True, editable=False)
91 ogg_published = models.DateTimeField(null=True, editable=False)
93 youtube_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
94 youtube_task = models.CharField(max_length=64, null=True, editable=False)
95 youtube_tags = models.TextField(null=True, editable=False)
96 youtube_published_tags = models.TextField(null=True, editable=False)
97 youtube_published = models.DateTimeField(null=True, editable=False)
98 youtube_id = models.CharField(max_length=255, blank=True, default='')
101 verbose_name = _("audiobook")
102 verbose_name_plural = _("audiobooks")
103 ordering = ("title",)
108 def get_mp3_tags(self): return json.loads(self.mp3_tags) if self.mp3_tags else None
109 def get_ogg_tags(self): return json.loads(self.ogg_tags) if self.ogg_tags else None
110 def get_mp3_published_tags(self): return json.loads(self.mp3_published_tags) if self.mp3_published_tags else None
111 def get_ogg_published_tags_tags(self): return json.loads(self.ogg_published_tags) if self.ogg_published_tags else None
112 def set_mp3_tags(self, tags): self.mp3_tags = json.dumps(tags)
113 def set_ogg_tags(self, tags): self.ogg_tags = json.dumps(tags)
116 return self.mp3_published and self.ogg_published
118 def get_source_sha1(self):
119 assert self.pk or self.source_sha1
120 if not self.source_sha1:
121 with advisory_lock(f'get_source_sha1_{self.pk}'):
122 with open(self.source_file.path, 'rb') as f:
123 self.source_sha1 = sha1_file(f)
124 self.save(update_fields=['source_sha1'])
125 return self.source_sha1
127 def new_publish_tags(self):
130 title += ' (tłum. %s)' % self.translator
132 copyright = "%s %s. Licensed to the public under %s verify at %s" % (
133 self.date, ORGANIZATION, self.license.uri, self.url)
135 comment = "\n".join((
136 self.project.get_description(),
142 'albumartist': ORGANIZATION,
143 'artist': self.artist,
145 'conductor': self.conductor,
147 'copyright': copyright,
151 'license': self.license.uri,
152 'organization': ORGANIZATION,
154 'project': self.project.name,
156 if self.project.sponsors:
157 tags['funded_by'] = self.project.sponsors
160 tags['flac_sha1'] = self.source_sha1
165 slug = self.url.rstrip('/').rsplit('/', 1)[-1]
166 apidata = requests.get(f'https://wolnelektury.pl/api/books/{slug}/').json()