fixes #2136: allow converting without publication
[audio.git] / apps / archive / models.py
1 # -*- coding: utf-8 -*-
2 import os.path
3
4 from django.db import models
5 from jsonfield.fields import JSONField
6 from django.utils.translation import ugettext_lazy as _
7 from archive.constants import status
8 from archive.settings import FILES_SAVE_PATH, ADVERT, LICENSE, ORGANIZATION, PROJECT
9 from archive.utils import OverwriteStorage
10
11 # Create your models here.
12
13
14 class Project(models.Model):
15     """ an audiobook project, needed for specyfing sponsors """
16
17     name = models.CharField(max_length=128, unique=True, db_index=True, verbose_name="Nazwa")
18     sponsors = models.TextField(blank=True, null=True, verbose_name="Sponsorzy")
19
20     class Meta:
21         verbose_name = _("project")
22         verbose_name_plural = _("projects")
23         ordering = ("name",)
24
25     def __unicode__(self):
26         return self.name
27
28
29 def source_upload_to(intance, filename):
30     return os.path.join(FILES_SAVE_PATH, filename) # FIXME: what about really long file names?
31
32
33 class Audiobook(models.Model):
34     source_file = models.FileField(upload_to=source_upload_to, max_length=255, 
35             verbose_name=_('source file'), editable=False)
36     source_sha1 = models.CharField(max_length=40, editable=False)
37
38     title = models.CharField(max_length=255, verbose_name=_('title'))
39     artist = models.CharField(max_length=255, verbose_name=_('artist'))
40     conductor = models.CharField(max_length=255, verbose_name=_('conductor'))
41     encoded_by = models.CharField(max_length=255, verbose_name=_('encoded by'))
42     date = models.CharField(max_length=255, verbose_name=_('date'))
43     project = models.ForeignKey(Project, verbose_name=_('project'))
44     url = models.URLField(max_length=255, verbose_name=_('book url'))
45     translator = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('translator'))
46     modified = models.DateTimeField(null=True, editable=False)
47
48     # publishing process
49     mp3_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
50     mp3_task = models.CharField(max_length=64, null=True, editable=False)
51     mp3_tags = JSONField(null=True, editable=False)
52     mp3_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
53     mp3_published_tags = JSONField(null=True, editable=False)
54     mp3_published = models.DateTimeField(null=True, editable=False)
55
56     ogg_status = models.SmallIntegerField(null=True, editable=False, choices=status.choices)
57     ogg_task = models.CharField(max_length=64, null=True, editable=False)
58     ogg_tags = JSONField(null=True, editable=False)
59     ogg_file = models.FileField(null=True, upload_to='archive/final', storage=OverwriteStorage(), editable=False)
60     ogg_published_tags = JSONField(null=True, editable=False)
61     ogg_published = models.DateTimeField(null=True, editable=False)
62
63
64     class Meta:
65         verbose_name = _("audiobook")
66         verbose_name_plural = _("audiobooks")
67         ordering = ("title",)
68
69     def __unicode__(self):
70         return self.title
71
72     def published(self):
73         return self.mp3_published and self.ogg_published
74
75     def new_publish_tags(self):
76         title = self.title
77         if self.translator:
78             title += u' (tłum. %s)' % self.translator
79
80         copyright = u"%s %s. Licensed to the public under %s verify at %s" % (
81                 self.date, ORGANIZATION, LICENSE, self.url)
82
83         comment = u"Audiobook nagrany w ramach projektu %s%s.\n%s" % (
84                     self.project.name,
85                     u" finansowanego przez %s" % self.project.sponsors if self.project.sponsors else "",
86                     ADVERT)
87
88         return {
89             'album': PROJECT,
90             'albumartist': ORGANIZATION,
91             'artist': self.artist,
92             'comment': comment,
93             'conductor': self.conductor,
94             'contact': self.url,
95             'copyright': copyright,
96             'date': self.date,
97             'genre': u'Speech',
98             'language': u'pol',
99             'license': LICENSE,
100             'organization': ORGANIZATION,
101             'title': title,
102             'flac_sha1': self.source_sha1,
103             'project': self.project.name,
104             'funded_by': self.project.sponsors,
105         }