]> git.0d.be Git - django-panik-nonstop.git/blob - nonstop/models.py
5321888186a1f3c3c899fcc49a6f94a10052d2c3
[django-panik-nonstop.git] / nonstop / models.py
1 import os
2
3 import mutagen
4
5 from django.conf import settings
6 from django.core.urlresolvers import reverse
7 from django.db import models
8 from django.utils.timezone import now
9 from django.utils.translation import ugettext_lazy as _
10
11 REMOTE_BASE_PATH = '/srv/soma/nonstop/'
12 LOCAL_BASE_PATH = '/media/nonstop/'
13
14 TRANCHE_SLUG_DIR_MAPPING = {
15     'acouphene': 'Acouphene',
16     'biodiversite': 'Biodiversite',
17     'l-heure-de-pointe': 'Heure_de_pointe',
18     'hop-bop-co': 'Hop_Bop_and_co',
19     'la-panique': 'la_panique',
20     'le-mange-disque': 'Mange_Disque',
21     'matin-tranquille': 'Matins_tranquilles',
22     'reveries': 'Reveries',
23     'up-beat-tempo': 'Up_Beat_Tempo',
24 }
25
26 class Artist(models.Model):
27     name = models.CharField(_('Name'), max_length=255)
28
29     class Meta:
30         ordering = ['name']
31
32     def get_absolute_url(self):
33         return reverse('artist-view', kwargs={'pk': self.id})
34
35     def recent_diffusions(self):
36         return SomaLogLine.objects.filter(filepath__track__artist=self
37                 ).exclude(on_air=False).order_by('-play_timestamp')
38
39
40 class Album(models.Model):
41     name = models.CharField(_('Name'), max_length=255)
42
43
44 LANGUAGES = [
45     ('en', _('English')),
46     ('fr', _('French')),
47     ('nl', _('Dutch'))
48 ]
49
50 class Track(models.Model):
51     title = models.CharField(_('Title'), max_length=255)
52     artist = models.ForeignKey(Artist, null=True)
53     album = models.ForeignKey(Album, null=True)
54     instru = models.BooleanField(_('Instru'), default=False)
55     language = models.CharField(max_length=3,
56             choices=LANGUAGES, blank=True)
57     sabam = models.BooleanField('SABAM', default=True)
58     cfwb = models.BooleanField('CFWB', default=False)
59     nonstop_zones = models.ManyToManyField('emissions.Nonstop', blank=True)
60
61     creation_timestamp = models.DateTimeField(auto_now_add=True, null=True)
62     added_to_nonstop_timestamp = models.DateTimeField(null=True)
63     uploader = models.ForeignKey(settings.AUTH_USER_MODEL, null=True)
64     duration = models.DurationField(_('Duration'), null=True)
65
66     class Meta:
67         ordering = ['creation_timestamp']
68
69     def get_absolute_url(self):
70         return reverse('track-view', kwargs={'pk': self.id})
71
72     def recent_diffusions(self):
73         return SomaLogLine.objects.filter(filepath__track=self
74                 ).exclude(on_air=False).order_by('-play_timestamp')
75
76     def file_path(self):
77         nfile = None
78         for nfile in self.nonstopfile_set.all().order_by('creation_timestamp'):
79             if os.path.exists(nfile.get_local_filepath()):
80                 return nfile.get_local_filepath()
81         if nfile:
82            return nfile.get_local_filepath()
83         return None
84
85     def file_exists(self):
86         file_path = self.file_path()
87         if not file_path:
88             return False
89         try:
90             return os.path.exists(file_path)
91         except AttributeError:
92             return False
93
94     def sync_nonstop_zones(self):
95         current_zones = self.nonstop_zones.all()
96         if current_zones.count():
97             if not self.added_to_nonstop_timestamp:
98                 self.added_to_nonstop_timestamp = now()
99                 self.save()
100         else:
101             self.added_to_nonstop_timestamp = None
102             self.save()
103
104         if not self.file_exists():
105             return
106         nonstop_file = self.nonstopfile_set.order_by('creation_timestamp').last()
107         filename = nonstop_file.filename
108         from emissions.models import Nonstop
109
110         for zone in Nonstop.objects.all():
111             if not zone.slug in TRANCHE_SLUG_DIR_MAPPING:
112                 continue
113             zone_dir = TRANCHE_SLUG_DIR_MAPPING[zone.slug]
114             zone_path = os.path.join(LOCAL_BASE_PATH, 'Tranches', zone_dir, filename)
115             if zone in current_zones:
116                 if not os.path.exists(zone_path):
117                     os.symlink(os.path.join('..', '..', nonstop_file.short), zone_path)
118             else:
119                 if os.path.exists(zone_path):
120                     os.unlink(zone_path)
121
122
123 class NonstopFile(models.Model):
124     filepath = models.CharField(_('Filepath'), max_length=255)
125     filename = models.CharField(_('Filename'), max_length=255, null=True)
126     creation_timestamp = models.DateTimeField(auto_now_add=True, null=True)
127     track = models.ForeignKey(Track, null=True)
128
129     @property
130     def short(self):
131         return self.filepath[len(REMOTE_BASE_PATH):]
132
133     def set_track_filepath(self, filepath):
134         self.filepath = os.path.join(REMOTE_BASE_PATH, 'tracks', filepath)
135         self.filename = os.path.basename(filepath)
136
137     def get_local_filepath(self):
138         if not self.short:
139             return None
140         return os.path.join(LOCAL_BASE_PATH, self.short)
141
142
143 class SomaLogLine(models.Model):
144     class Meta:
145         verbose_name = _('Soma log line')
146         verbose_name_plural = _('Soma log lines')
147         ordering = ['play_timestamp']
148
149     filepath = models.ForeignKey(NonstopFile)
150     play_timestamp = models.DateTimeField()
151     on_air = models.NullBooleanField('On Air')
152
153
154 class Jingle(models.Model):
155     class Meta:
156         verbose_name = _('Jingle')
157         verbose_name_plural = _('Jingles')
158
159     label = models.CharField(_('Label'), max_length=100)
160     filepath = models.CharField(_('File Path'), max_length=255)
161     duration = models.DurationField(_('Duration'), null=True, blank=True)
162
163
164 class Stream(models.Model):
165     class Meta:
166         verbose_name = _('Stream')
167         verbose_name_plural = _('Streams')
168
169     label = models.CharField(_('Label'), max_length=100)
170     url = models.URLField(_('URL'), max_length=255)