local changes from server
[audio.git] / apps / 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     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)
49
50     # publishing process
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)
57
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)
64
65
66     class Meta:
67         verbose_name = _("audiobook")
68         verbose_name_plural = _("audiobooks")
69         ordering = ("title",)
70
71     def __unicode__(self):
72         return self.title
73
74     def published(self):
75         return self.mp3_published and self.ogg_published
76
77     def get_source_sha1(self):
78         source_sha1 = self.source_sha1
79         if self.pk:
80             source_sha1 = type(self).objects.get(pk=self.pk).source_sha1
81             while source_sha1 == 'wait':
82                 sleep(10)
83         if not source_sha1:
84             self.source_sha1 = 'wait'
85             if self.pk:
86                 type(self).objects.filter(pk=self.pk).update(source_sha1='wait')
87             try:
88                 f = open(force_bytes(self.source_file.path))
89                 source_sha1 = sha1_file(f)
90                 self.source_sha1 = source_sha1
91                 if self.pk:
92                     type(self).objects.filter(pk=self.pk).update(source_sha1=source_sha1)
93             except:
94                 self.source_sha1 = ''
95                 if self.pk:
96                     type(self).objects.filter(pk=self.pk).update(source_sha1='')
97                 return None
98         return source_sha1
99
100
101     def new_publish_tags(self):
102         title = self.title
103         if self.translator:
104             title += u' (tłum. %s)' % self.translator
105
106         copyright = u"%s %s. Licensed to the public under %s verify at %s" % (
107                 self.date, ORGANIZATION, LICENSE, self.url)
108
109         comment = u"Audiobook nagrany w ramach projektu %s%s.\n%s" % (
110                     self.project.name,
111                     u" finansowanego przez %s" % self.project.sponsors if self.project.sponsors else "",
112                     ADVERT)
113
114         tags = {
115             'album': PROJECT,
116             'albumartist': ORGANIZATION,
117             'artist': self.artist,
118             'comment': comment,
119             'conductor': self.conductor,
120             'contact': self.url,
121             'copyright': copyright,
122             'date': self.date,
123             'genre': u'Speech',
124             'language': u'pol',
125             'license': LICENSE,
126             'organization': ORGANIZATION,
127             'title': title,
128             #'flac_sha1': self.get_source_sha1(),
129             'project': self.project.name,
130             'funded_by': self.project.sponsors,
131         }
132         if self.source_sha1 and self.source_sha1 != 'wait':
133             tags['flac_sha1'] = self.source_sha1
134         return tags
135