c75a874c8ce1b2458d31266d4dfac47d3758c326
[audio.git] / src / archive / models.py
1 # -*- coding: utf-8 -*-
2 import os.path
3
4 from django.db import models
5 from time import sleep
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
12
13 # Create your models here.
14
15
16 class Project(models.Model):
17     """ an audiobook project, needed for specyfing sponsors """
18
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")
21
22     class Meta:
23         verbose_name = _("project")
24         verbose_name_plural = _("projects")
25         ordering = ("name",)
26
27     def __unicode__(self):
28         return self.name
29
30
31 def source_upload_to(intance, filename):
32     return os.path.join(FILES_SAVE_PATH, filename) # FIXME: what about really long file names?
33
34
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)
39
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)
53
54     # publishing process
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)
61
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)
68
69
70     class Meta:
71         verbose_name = _("audiobook")
72         verbose_name_plural = _("audiobooks")
73         ordering = ("title",)
74
75     def __unicode__(self):
76         return self.title
77
78     def published(self):
79         return self.mp3_published and self.ogg_published
80
81     def get_source_sha1(self):
82         source_sha1 = self.source_sha1
83         if self.pk:
84             source_sha1 = type(self).objects.get(pk=self.pk).source_sha1
85             while source_sha1 == 'wait':
86                 sleep(10)
87         if not source_sha1:
88             self.source_sha1 = 'wait'
89             if self.pk:
90                 type(self).objects.filter(pk=self.pk).update(source_sha1='wait')
91             try:
92                 f = open(force_bytes(self.source_file.path))
93                 source_sha1 = sha1_file(f)
94                 self.source_sha1 = source_sha1
95                 if self.pk:
96                     type(self).objects.filter(pk=self.pk).update(source_sha1=source_sha1)
97             except:
98                 self.source_sha1 = ''
99                 if self.pk:
100                     type(self).objects.filter(pk=self.pk).update(source_sha1='')
101                 return None
102         return source_sha1
103
104
105     def new_publish_tags(self):
106         title = self.title
107         if self.translator:
108             title += u' (tłum. %s)' % self.translator
109
110         copyright = u"%s %s. Licensed to the public under %s verify at %s" % (
111                 self.date, ORGANIZATION, LICENSE, self.url)
112
113         comment = u"Audiobook nagrany w ramach projektu %s%s.\n%s" % (
114                     self.project.name,
115                     u" finansowanego przez %s" % self.project.sponsors if self.project.sponsors else "",
116                     ADVERT)
117
118         tags = {
119             'album': PROJECT,
120             'albumartist': ORGANIZATION,
121             'artist': self.artist,
122             'comment': comment,
123             'conductor': self.conductor,
124             'contact': self.url,
125             'copyright': copyright,
126             'date': self.date,
127             'genre': u'Speech',
128             'language': u'pol',
129             'license': LICENSE,
130             'organization': ORGANIZATION,
131             'title': title,
132             #'flac_sha1': self.get_source_sha1(),
133             'project': self.project.name,
134             'funded_by': self.project.sponsors,
135         }
136         if self.source_sha1 and self.source_sha1 != 'wait':
137             tags['flac_sha1'] = self.source_sha1
138         return tags
139