1 # -*- coding: utf-8 -*-
4 from django.db import models
6 from django.utils.encoding import force_bytes
7 from django.utils.translation import ugettext_lazy as _
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
12 # Create your models here.
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")
22 verbose_name = _("project")
23 verbose_name_plural = _("projects")
26 def __unicode__(self):
30 def source_upload_to(intance, filename):
31 return os.path.join(FILES_SAVE_PATH, filename) # FIXME: what about really long file names?
34 class Audiobook(models.Model):
35 source_file = models.FileField(upload_to=source_upload_to, max_length=255,
36 verbose_name=_('source file'), editable=False)
37 source_sha1 = models.CharField(max_length=40, editable=False)
39 title = models.CharField(max_length=255, verbose_name=_('title'))
40 part_name = models.CharField(max_length=255, verbose_name=_('part name'), help_text=_('eg. chapter in a novel'),
41 default='', blank=True)
42 index = models.IntegerField(verbose_name=_('index'), default=0)
43 parts_count = models.IntegerField(verbose_name=_('parts count'), default=1)
44 artist = models.CharField(max_length=255, verbose_name=_('artist'))
45 conductor = models.CharField(max_length=255, verbose_name=_('conductor'))
46 encoded_by = models.CharField(max_length=255, verbose_name=_('encoded by'))
47 date = models.CharField(max_length=255, verbose_name=_('date'))
48 project = models.ForeignKey(Project, verbose_name=_('project'))
49 url = models.URLField(max_length=255, verbose_name=_('book url'))
50 translator = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('translator'))
51 modified = models.DateTimeField(null=True, editable=False)
54 mp3_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
55 mp3_task = models.CharField(max_length=64, null=True, editable=False)
56 mp3_tags = models.TextField(null=True, editable=False)
57 mp3_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
58 mp3_published_tags = models.TextField(null=True, editable=False)
59 mp3_published = models.DateTimeField(null=True, editable=False)
61 ogg_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
62 ogg_task = models.CharField(max_length=64, null=True, editable=False)
63 ogg_tags = models.TextField(null=True, editable=False)
64 ogg_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
65 ogg_published_tags = models.TextField(null=True, editable=False)
66 ogg_published = models.DateTimeField(null=True, editable=False)
69 verbose_name = _("audiobook")
70 verbose_name_plural = _("audiobooks")
73 def __unicode__(self):
76 def get_mp3_tags(self): return json.loads(self.mp3_tags) if self.mp3_tags else None
77 def get_ogg_tags(self): return json.loads(self.ogg_tags) if self.ogg_tags else None
78 def get_mp3_published_tags(self): return json.loads(self.mp3_published_tags) if self.mp3_published_tags else None
79 def get_ogg_published_tags_tags(self): return json.loads(self.ogg_published_tags) if self.ogg_published_tags else None
80 def set_mp3_tags(self, tags): self.mp3_tags = json.dumps(tags)
81 def set_ogg_tags(self, tags): self.ogg_tags = json.dumps(tags)
84 return self.mp3_published and self.ogg_published
86 def get_source_sha1(self):
87 source_sha1 = self.source_sha1
89 source_sha1 = type(self).objects.get(pk=self.pk).source_sha1
90 while source_sha1 == 'wait':
93 self.source_sha1 = 'wait'
95 type(self).objects.filter(pk=self.pk).update(source_sha1='wait')
97 f = open(force_bytes(self.source_file.path))
98 source_sha1 = sha1_file(f)
99 self.source_sha1 = source_sha1
101 type(self).objects.filter(pk=self.pk).update(source_sha1=source_sha1)
103 self.source_sha1 = ''
105 type(self).objects.filter(pk=self.pk).update(source_sha1='')
110 def new_publish_tags(self):
113 title += u' (tłum. %s)' % self.translator
115 copyright = u"%s %s. Licensed to the public under %s verify at %s" % (
116 self.date, ORGANIZATION, LICENSE, self.url)
118 comment = u"Audiobook nagrany w ramach projektu %s%s.\n%s" % (
120 u" finansowanego przez %s" % self.project.sponsors if self.project.sponsors else "",
125 'albumartist': ORGANIZATION,
126 'artist': self.artist,
128 'conductor': self.conductor,
130 'copyright': copyright,
135 'organization': ORGANIZATION,
137 #'flac_sha1': self.get_source_sha1(),
138 'project': self.project.name,
139 'funded_by': self.project.sponsors,
141 if self.source_sha1 and self.source_sha1 != 'wait':
142 tags['flac_sha1'] = self.source_sha1