4 from django.db import models
6 from django.utils.translation import gettext_lazy as _
7 from django_pglocks import advisory_lock
8 from archive.constants import status
9 from archive.settings import FILES_SAVE_PATH, ADVERT, LICENSE, ORGANIZATION, PROJECT
10 from archive.utils import OverwriteStorage, sha1_file
13 class Project(models.Model):
14 """ an audiobook project, needed for specyfing sponsors """
16 name = models.CharField(max_length=128, unique=True, db_index=True, verbose_name="Nazwa")
17 sponsors = models.TextField(blank=True, null=True, verbose_name="Sponsorzy")
20 verbose_name = _("project")
21 verbose_name_plural = _("projects")
28 def source_upload_to(intance, filename):
29 return os.path.join(FILES_SAVE_PATH, filename) # FIXME: what about really long file names?
32 class Audiobook(models.Model):
33 source_file = models.FileField(upload_to=source_upload_to, max_length=255,
34 verbose_name=_('source file'), editable=False)
35 source_sha1 = models.CharField(max_length=40, editable=False)
37 title = models.CharField(max_length=255, verbose_name=_('title'))
38 part_name = models.CharField(max_length=255, verbose_name=_('part name'), help_text=_('eg. chapter in a novel'),
39 default='', blank=True)
40 index = models.IntegerField(verbose_name=_('index'), default=0)
41 parts_count = models.IntegerField(verbose_name=_('parts count'), default=1)
42 artist = models.CharField(max_length=255, verbose_name=_('artist'))
43 conductor = models.CharField(max_length=255, verbose_name=_('conductor'))
44 encoded_by = models.CharField(max_length=255, verbose_name=_('encoded by'))
45 date = models.CharField(max_length=255, verbose_name=_('date'))
46 project = models.ForeignKey(Project, models.PROTECT, verbose_name=_('project'))
47 url = models.URLField(max_length=255, verbose_name=_('book url'))
48 translator = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('translator'))
49 modified = models.DateTimeField(null=True, editable=False)
52 mp3_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
53 mp3_task = models.CharField(max_length=64, null=True, editable=False)
54 mp3_tags = models.TextField(null=True, editable=False)
55 mp3_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
56 mp3_published_tags = models.TextField(null=True, editable=False)
57 mp3_published = models.DateTimeField(null=True, editable=False)
59 ogg_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
60 ogg_task = models.CharField(max_length=64, null=True, editable=False)
61 ogg_tags = models.TextField(null=True, editable=False)
62 ogg_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
63 ogg_published_tags = models.TextField(null=True, editable=False)
64 ogg_published = models.DateTimeField(null=True, editable=False)
67 verbose_name = _("audiobook")
68 verbose_name_plural = _("audiobooks")
74 def get_mp3_tags(self): return json.loads(self.mp3_tags) if self.mp3_tags else None
75 def get_ogg_tags(self): return json.loads(self.ogg_tags) if self.ogg_tags else None
76 def get_mp3_published_tags(self): return json.loads(self.mp3_published_tags) if self.mp3_published_tags else None
77 def get_ogg_published_tags_tags(self): return json.loads(self.ogg_published_tags) if self.ogg_published_tags else None
78 def set_mp3_tags(self, tags): self.mp3_tags = json.dumps(tags)
79 def set_ogg_tags(self, tags): self.ogg_tags = json.dumps(tags)
82 return self.mp3_published and self.ogg_published
84 def get_source_sha1(self):
85 assert self.pk or self.source_sha1
86 if not self.source_sha1:
87 with advisory_lock(f'get_source_sha1_{self.pk}'):
88 with open(self.source_file.path, 'rb') as f:
89 self.source_sha1 = sha1_file(f)
90 self.save(update_fields=['source_sha1'])
91 return self.source_sha1
93 def new_publish_tags(self):
96 title += ' (tłum. %s)' % self.translator
98 copyright = "%s %s. Licensed to the public under %s verify at %s" % (
99 self.date, ORGANIZATION, LICENSE, self.url)
101 comment = "Audiobook nagrany w ramach projektu %s%s.\n%s" % (
103 " finansowanego przez %s" % self.project.sponsors if self.project.sponsors else "",
108 'albumartist': ORGANIZATION,
109 'artist': self.artist,
111 'conductor': self.conductor,
113 'copyright': copyright,
118 'organization': ORGANIZATION,
120 'project': self.project.name,
121 'funded_by': self.project.sponsors,
124 tags['flac_sha1'] = self.source_sha1