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 _
11 REMOTE_BASE_PATH = '/srv/soma/nonstop/'
12 LOCAL_BASE_PATH = '/media/nonstop/'
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',
26 class Artist(models.Model):
27 name = models.CharField(_('Name'), max_length=255)
32 def get_absolute_url(self):
33 return reverse('artist-view', kwargs={'pk': self.id})
35 def recent_diffusions(self):
36 return SomaLogLine.objects.filter(filepath__track__artist=self
37 ).exclude(on_air=False).order_by('-play_timestamp')
40 class Album(models.Model):
41 name = models.CharField(_('Name'), max_length=255)
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)
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)
67 ordering = ['creation_timestamp']
69 def get_absolute_url(self):
70 return reverse('track-view', kwargs={'pk': self.id})
72 def recent_diffusions(self):
73 return SomaLogLine.objects.filter(filepath__track=self
74 ).exclude(on_air=False).order_by('-play_timestamp')
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()
82 return nfile.get_local_filepath()
85 def file_exists(self):
86 file_path = self.file_path()
90 return os.path.exists(file_path)
91 except AttributeError:
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()
101 self.added_to_nonstop_timestamp = None
104 if not self.file_exists():
106 nonstop_file = self.nonstopfile_set.order_by('creation_timestamp').last()
107 filename = nonstop_file.filename
108 from emissions.models import Nonstop
110 for zone in Nonstop.objects.all():
111 if not zone.slug in TRANCHE_SLUG_DIR_MAPPING:
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)
119 if os.path.exists(zone_path):
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)
131 return self.filepath[len(REMOTE_BASE_PATH):]
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)
137 def get_local_filepath(self):
140 return os.path.join(LOCAL_BASE_PATH, self.short)
143 class SomaLogLine(models.Model):
145 verbose_name = _('Soma log line')
146 verbose_name_plural = _('Soma log lines')
147 ordering = ['play_timestamp']
149 filepath = models.ForeignKey(NonstopFile)
150 play_timestamp = models.DateTimeField()
151 on_air = models.NullBooleanField('On Air')
154 class Jingle(models.Model):
156 verbose_name = _('Jingle')
157 verbose_name_plural = _('Jingles')
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)
164 class Stream(models.Model):
166 verbose_name = _('Stream')
167 verbose_name_plural = _('Streams')
169 label = models.CharField(_('Label'), max_length=100)
170 url = models.URLField(_('URL'), max_length=255)