1 # -*- coding: utf-8 -*-
4 from django.db import models
6 from jsonfield.fields import JSONField
7 from django.utils.encoding import force_bytes
8 from django.utils.translation import ugettext_lazy as _
9 from archive.constants import status
10 from archive.settings import FILES_SAVE_PATH, ADVERT, LICENSE, ORGANIZATION, PROJECT
11 from archive.utils import OverwriteStorage, sha1_file
13 # Create your models here.
16 class Project(models.Model):
17 """ an audiobook project, needed for specyfing sponsors """
19 name = models.CharField(max_length=128, unique=True, db_index=True, verbose_name="Nazwa")
20 sponsors = models.TextField(blank=True, null=True, verbose_name="Sponsorzy")
23 verbose_name = _("project")
24 verbose_name_plural = _("projects")
27 def __unicode__(self):
31 def source_upload_to(intance, filename):
32 return os.path.join(FILES_SAVE_PATH, filename) # FIXME: what about really long file names?
35 class Audiobook(models.Model):
36 source_file = models.FileField(upload_to=source_upload_to, max_length=255,
37 verbose_name=_('source file'), editable=False)
38 source_sha1 = models.CharField(max_length=40, editable=False)
40 title = models.CharField(max_length=255, verbose_name=_('title'))
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 = JSONField(null=True, editable=False)
54 mp3_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
55 mp3_published_tags = JSONField(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 = JSONField(null=True, editable=False)
61 ogg_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
62 ogg_published_tags = JSONField(null=True, editable=False)
63 ogg_published = models.DateTimeField(null=True, editable=False)
67 verbose_name = _("audiobook")
68 verbose_name_plural = _("audiobooks")
71 def __unicode__(self):
75 return self.mp3_published and self.ogg_published
77 def get_source_sha1(self):
78 source_sha1 = self.source_sha1
80 source_sha1 = type(self).objects.get(pk=self.pk).source_sha1
81 while source_sha1 == 'wait':
84 self.source_sha1 = 'wait'
86 type(self).objects.filter(pk=self.pk).update(source_sha1='wait')
88 f = open(force_bytes(self.source_file.path))
89 source_sha1 = sha1_file(f)
90 self.source_sha1 = source_sha1
92 type(self).objects.filter(pk=self.pk).update(source_sha1=source_sha1)
96 type(self).objects.filter(pk=self.pk).update(source_sha1='')
101 def new_publish_tags(self):
104 title += u' (tłum. %s)' % self.translator
106 copyright = u"%s %s. Licensed to the public under %s verify at %s" % (
107 self.date, ORGANIZATION, LICENSE, self.url)
109 comment = u"Audiobook nagrany w ramach projektu %s%s.\n%s" % (
111 u" finansowanego przez %s" % self.project.sponsors if self.project.sponsors else "",
116 'albumartist': ORGANIZATION,
117 'artist': self.artist,
119 'conductor': self.conductor,
121 'copyright': copyright,
126 'organization': ORGANIZATION,
128 #'flac_sha1': self.get_source_sha1(),
129 'project': self.project.name,
130 'funded_by': self.project.sponsors,
132 if self.source_sha1 and self.source_sha1 != 'wait':
133 tags['flac_sha1'] = self.source_sha1