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 part_name = models.CharField(max_length=255, verbose_name=_('part name'), help_text=_('eg. chapter in a novel'),
42 default='', blank=True)
43 index = models.IntegerField(verbose_name=_('index'), default=0)
44 parts_count = models.IntegerField(verbose_name=_('parts count'), default=1)
45 artist = models.CharField(max_length=255, verbose_name=_('artist'))
46 conductor = models.CharField(max_length=255, verbose_name=_('conductor'))
47 encoded_by = models.CharField(max_length=255, verbose_name=_('encoded by'))
48 date = models.CharField(max_length=255, verbose_name=_('date'))
49 project = models.ForeignKey(Project, verbose_name=_('project'))
50 url = models.URLField(max_length=255, verbose_name=_('book url'))
51 translator = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('translator'))
52 modified = models.DateTimeField(null=True, editable=False)
55 mp3_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
56 mp3_task = models.CharField(max_length=64, null=True, editable=False)
57 mp3_tags = JSONField(null=True, editable=False)
58 mp3_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
59 mp3_published_tags = JSONField(null=True, editable=False)
60 mp3_published = models.DateTimeField(null=True, editable=False)
62 ogg_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
63 ogg_task = models.CharField(max_length=64, null=True, editable=False)
64 ogg_tags = JSONField(null=True, editable=False)
65 ogg_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
66 ogg_published_tags = JSONField(null=True, editable=False)
67 ogg_published = models.DateTimeField(null=True, editable=False)
71 verbose_name = _("audiobook")
72 verbose_name_plural = _("audiobooks")
75 def __unicode__(self):
79 return self.mp3_published and self.ogg_published
81 def get_source_sha1(self):
82 source_sha1 = self.source_sha1
84 source_sha1 = type(self).objects.get(pk=self.pk).source_sha1
85 while source_sha1 == 'wait':
88 self.source_sha1 = 'wait'
90 type(self).objects.filter(pk=self.pk).update(source_sha1='wait')
92 f = open(force_bytes(self.source_file.path))
93 source_sha1 = sha1_file(f)
94 self.source_sha1 = source_sha1
96 type(self).objects.filter(pk=self.pk).update(source_sha1=source_sha1)
100 type(self).objects.filter(pk=self.pk).update(source_sha1='')
105 def new_publish_tags(self):
108 title += u' (tłum. %s)' % self.translator
110 copyright = u"%s %s. Licensed to the public under %s verify at %s" % (
111 self.date, ORGANIZATION, LICENSE, self.url)
113 comment = u"Audiobook nagrany w ramach projektu %s%s.\n%s" % (
115 u" finansowanego przez %s" % self.project.sponsors if self.project.sponsors else "",
120 'albumartist': ORGANIZATION,
121 'artist': self.artist,
123 'conductor': self.conductor,
125 'copyright': copyright,
130 'organization': ORGANIZATION,
132 #'flac_sha1': self.get_source_sha1(),
133 'project': self.project.name,
134 'funded_by': self.project.sponsors,
136 if self.source_sha1 and self.source_sha1 != 'wait':
137 tags['flac_sha1'] = self.source_sha1