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, LICENSE, ORGANIZATION, PROJECT
12 from archive.utils import OverwriteStorage, sha1_file
15 class Project(models.Model):
16 """ an audiobook project, needed for specyfing sponsors """
18 name = models.CharField(max_length=128, unique=True, db_index=True, verbose_name="Nazwa")
19 sponsors = models.TextField(blank=True, null=True, verbose_name="Sponsorzy")
20 description = models.TextField(blank=True, verbose_name="Opis")
23 verbose_name = _("project")
24 verbose_name_plural = _("projects")
30 def get_description(self):
32 return self.description
33 return "Audiobook nagrany w ramach projektu %s%s." % (
35 " finansowanego przez %s" % self.sponsors if self.sponsors else "",
39 class Piece(models.Model):
40 name = models.CharField(max_length=255)
41 source_file = models.FileField(upload_to='piece')
42 is_outro = models.BooleanField(default=False)
43 min_audiobook_duration = models.IntegerField(default=0)
49 def source_upload_to(intance, filename):
50 return os.path.join(FILES_SAVE_PATH, filename) # FIXME: what about really long file names?
53 class Audiobook(models.Model):
54 source_file = models.FileField(upload_to=source_upload_to, max_length=255,
55 verbose_name=_('source file'), editable=False)
56 source_sha1 = models.CharField(max_length=40, editable=False)
58 title = models.CharField(max_length=255, verbose_name=_('title'))
59 part_name = models.CharField(max_length=255, verbose_name=_('part name'), help_text=_('eg. chapter in a novel'),
60 default='', blank=True)
61 index = models.IntegerField(verbose_name=_('index'), default=0)
62 parts_count = models.IntegerField(verbose_name=_('parts count'), default=1)
63 artist = models.CharField(max_length=255, verbose_name=_('artist'))
64 conductor = models.CharField(max_length=255, verbose_name=_('conductor'))
65 encoded_by = models.CharField(max_length=255, verbose_name=_('encoded by'))
66 date = models.CharField(max_length=255, verbose_name=_('date'))
67 project = models.ForeignKey(Project, models.PROTECT, verbose_name=_('project'))
68 url = models.URLField(max_length=255, verbose_name=_('book url'))
69 translator = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('translator'))
70 modified = models.DateTimeField(null=True, editable=False)
73 mp3_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
74 mp3_task = models.CharField(max_length=64, null=True, editable=False)
75 mp3_tags = models.TextField(null=True, editable=False)
76 mp3_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
77 mp3_published_tags = models.TextField(null=True, editable=False)
78 mp3_published = models.DateTimeField(null=True, editable=False)
80 ogg_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
81 ogg_task = models.CharField(max_length=64, null=True, editable=False)
82 ogg_tags = models.TextField(null=True, editable=False)
83 ogg_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
84 ogg_published_tags = models.TextField(null=True, editable=False)
85 ogg_published = models.DateTimeField(null=True, editable=False)
87 youtube_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
88 youtube_task = models.CharField(max_length=64, null=True, editable=False)
89 youtube_tags = models.TextField(null=True, editable=False)
90 youtube_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
91 youtube_published_tags = models.TextField(null=True, editable=False)
92 youtube_published = models.DateTimeField(null=True, editable=False)
93 youtube_id = models.CharField(max_length=255, blank=True, default='')
96 verbose_name = _("audiobook")
97 verbose_name_plural = _("audiobooks")
103 def get_mp3_tags(self): return json.loads(self.mp3_tags) if self.mp3_tags else None
104 def get_ogg_tags(self): return json.loads(self.ogg_tags) if self.ogg_tags else None
105 def get_mp3_published_tags(self): return json.loads(self.mp3_published_tags) if self.mp3_published_tags else None
106 def get_ogg_published_tags_tags(self): return json.loads(self.ogg_published_tags) if self.ogg_published_tags else None
107 def set_mp3_tags(self, tags): self.mp3_tags = json.dumps(tags)
108 def set_ogg_tags(self, tags): self.ogg_tags = json.dumps(tags)
111 return self.mp3_published and self.ogg_published
113 def get_source_sha1(self):
114 assert self.pk or self.source_sha1
115 if not self.source_sha1:
116 with advisory_lock(f'get_source_sha1_{self.pk}'):
117 with open(self.source_file.path, 'rb') as f:
118 self.source_sha1 = sha1_file(f)
119 self.save(update_fields=['source_sha1'])
120 return self.source_sha1
122 def new_publish_tags(self):
125 title += ' (tłum. %s)' % self.translator
127 copyright = "%s %s. Licensed to the public under %s verify at %s" % (
128 self.date, ORGANIZATION, LICENSE, self.url)
130 comment = "\n".join((
131 self.project.get_description(),
137 'albumartist': ORGANIZATION,
138 'artist': self.artist,
140 'conductor': self.conductor,
142 'copyright': copyright,
147 'organization': ORGANIZATION,
149 'project': self.project.name,
151 if self.project.sponsors:
152 tags['funded_by'] = self.project.sponsors
155 tags['flac_sha1'] = self.source_sha1
160 slug = self.url.rstrip('/').rsplit('/', 1)[-1]
161 apidata = requests.get(f'https://wolnelektury.pl/api/books/{slug}/').json()