3 from django.db import models
5 from django.utils.encoding import force_bytes
6 from django.utils.translation import ugettext_lazy as _
7 from archive.constants import status
8 from archive.settings import FILES_SAVE_PATH, ADVERT, LICENSE, ORGANIZATION, PROJECT
9 from archive.utils import OverwriteStorage, sha1_file
12 class Project(models.Model):
13 """ an audiobook project, needed for specyfing sponsors """
15 name = models.CharField(max_length=128, unique=True, db_index=True, verbose_name="Nazwa")
16 sponsors = models.TextField(blank=True, null=True, verbose_name="Sponsorzy")
19 verbose_name = _("project")
20 verbose_name_plural = _("projects")
27 def source_upload_to(intance, filename):
28 return os.path.join(FILES_SAVE_PATH, filename) # FIXME: what about really long file names?
31 class Audiobook(models.Model):
32 source_file = models.FileField(upload_to=source_upload_to, max_length=255,
33 verbose_name=_('source file'), editable=False)
34 source_sha1 = models.CharField(max_length=40, editable=False)
36 title = models.CharField(max_length=255, verbose_name=_('title'))
37 part_name = models.CharField(max_length=255, verbose_name=_('part name'), help_text=_('eg. chapter in a novel'),
38 default='', blank=True)
39 index = models.IntegerField(verbose_name=_('index'), default=0)
40 parts_count = models.IntegerField(verbose_name=_('parts count'), default=1)
41 artist = models.CharField(max_length=255, verbose_name=_('artist'))
42 conductor = models.CharField(max_length=255, verbose_name=_('conductor'))
43 encoded_by = models.CharField(max_length=255, verbose_name=_('encoded by'))
44 date = models.CharField(max_length=255, verbose_name=_('date'))
45 project = models.ForeignKey(Project, verbose_name=_('project'))
46 url = models.URLField(max_length=255, verbose_name=_('book url'))
47 translator = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('translator'))
48 modified = models.DateTimeField(null=True, editable=False)
51 mp3_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
52 mp3_task = models.CharField(max_length=64, null=True, editable=False)
53 mp3_tags = models.TextField(null=True, editable=False)
54 mp3_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
55 mp3_published_tags = models.TextField(null=True, editable=False)
56 mp3_published = models.DateTimeField(null=True, editable=False)
58 ogg_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
59 ogg_task = models.CharField(max_length=64, null=True, editable=False)
60 ogg_tags = models.TextField(null=True, editable=False)
61 ogg_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
62 ogg_published_tags = models.TextField(null=True, editable=False)
63 ogg_published = models.DateTimeField(null=True, editable=False)
66 verbose_name = _("audiobook")
67 verbose_name_plural = _("audiobooks")
73 def get_mp3_tags(self): return json.loads(self.mp3_tags) if self.mp3_tags else None
74 def get_ogg_tags(self): return json.loads(self.ogg_tags) if self.ogg_tags else None
75 def get_mp3_published_tags(self): return json.loads(self.mp3_published_tags) if self.mp3_published_tags else None
76 def get_ogg_published_tags_tags(self): return json.loads(self.ogg_published_tags) if self.ogg_published_tags else None
77 def set_mp3_tags(self, tags): self.mp3_tags = json.dumps(tags)
78 def set_ogg_tags(self, tags): self.ogg_tags = json.dumps(tags)
81 return self.mp3_published and self.ogg_published
83 def get_source_sha1(self):
84 source_sha1 = self.source_sha1
86 source_sha1 = type(self).objects.get(pk=self.pk).source_sha1
87 while source_sha1 == 'wait':
90 self.source_sha1 = 'wait'
92 type(self).objects.filter(pk=self.pk).update(source_sha1='wait')
94 f = open(force_bytes(self.source_file.path))
95 source_sha1 = sha1_file(f)
96 self.source_sha1 = source_sha1
98 type(self).objects.filter(pk=self.pk).update(source_sha1=source_sha1)
100 self.source_sha1 = ''
102 type(self).objects.filter(pk=self.pk).update(source_sha1='')
107 def new_publish_tags(self):
110 title += ' (tłum. %s)' % self.translator
112 copyright = "%s %s. Licensed to the public under %s verify at %s" % (
113 self.date, ORGANIZATION, LICENSE, self.url)
115 comment = "Audiobook nagrany w ramach projektu %s%s.\n%s" % (
117 " finansowanego przez %s" % self.project.sponsors if self.project.sponsors else "",
122 'albumartist': ORGANIZATION,
123 'artist': self.artist,
125 'conductor': self.conductor,
127 'copyright': copyright,
132 'organization': ORGANIZATION,
134 #'flac_sha1': self.get_source_sha1(),
135 'project': self.project.name,
136 'funded_by': self.project.sponsors,
138 if self.source_sha1 and self.source_sha1 != 'wait':
139 tags['flac_sha1'] = self.source_sha1