models.py 4.08 KB
Newer Older
fred's avatar
fred committed
1 2 3 4
import os

import mutagen

fred's avatar
fred committed
5
from django.core.urlresolvers import reverse
6 7 8
from django.db import models
from django.utils.translation import ugettext_lazy as _

fred's avatar
fred committed
9
REMOTE_BASE_PATH = '/srv/soma/nonstop/'
10 11 12 13 14 15 16 17 18 19 20 21 22
LOCAL_BASE_PATH = '/media/nonstop/'

TRANCHE_SLUG_DIR_MAPPING = {
    'acouphene': 'Acouphene',
    'biodiversite': 'Biodiversite',
    'l-heure-de-pointe': 'Heure_de_pointe',
    'hop-bop-co': 'Hop_Bop_and_co',
    'la-panique': 'la_panique',
    'le-mange-disque': 'Mange_Disque',
    'matin-tranquille': 'Matins_tranquilles',
    'reveries': 'Reveries',
    'up-beat-tempo': 'Up_Beat_Tempo',
}
23

fred's avatar
fred committed
24 25 26
class Artist(models.Model):
    name = models.CharField(_('Name'), max_length=255)

fred's avatar
fred committed
27 28 29
    class Meta:
        ordering = ['name']

fred's avatar
fred committed
30 31 32 33
    def get_absolute_url(self):
        return reverse('artist-view', kwargs={'pk': self.id})

    def recent_diffusions(self):
34 35
        return SomaLogLine.objects.filter(filepath__track__artist=self
                ).exclude(on_air=False).order_by('-play_timestamp')
fred's avatar
fred committed
36

fred's avatar
fred committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

class Album(models.Model):
    name = models.CharField(_('Name'), max_length=255)


LANGUAGES = [
    ('en', _('English')),
    ('fr', _('French')),
    ('nl', _('Dutch'))
]

class Track(models.Model):
    title = models.CharField(_('Title'), max_length=255)
    artist = models.ForeignKey(Artist, null=True)
    album = models.ForeignKey(Album, null=True)
    instru = models.BooleanField(_('Instru'), default=False)
    language = models.CharField(max_length=3,
            choices=LANGUAGES, blank=True)
    sabam = models.BooleanField('SABAM', default=True)
56
    cfwb = models.BooleanField('CFWB', default=False)
57
    nonstop_zones = models.ManyToManyField('emissions.Nonstop', blank=True)
fred's avatar
fred committed
58

fred's avatar
fred committed
59 60 61 62 63
    creation_timestamp = models.DateTimeField(auto_now_add=True, null=True)

    class Meta:
        ordering = ['creation_timestamp']

fred's avatar
fred committed
64 65 66
    def get_absolute_url(self):
        return reverse('track-view', kwargs={'pk': self.id})

fred's avatar
fred committed
67
    def recent_diffusions(self):
68 69
        return SomaLogLine.objects.filter(filepath__track=self
                ).exclude(on_air=False).order_by('-play_timestamp')
fred's avatar
fred committed
70

71 72 73 74 75 76 77
    def file_path(self):
        return self.nonstopfile_set.all().order_by('creation_timestamp').last().get_local_filepath()

    def file_exists(self):
        return os.path.exists(self.nonstopfile_set.order_by('creation_timestamp').last().get_local_filepath())

    def sync_nonstop_zones(self):
78 79
        if not self.file_exists():
            return
80 81 82 83 84 85
        nonstop_file = self.nonstopfile_set.order_by('creation_timestamp').last()
        filename = nonstop_file.filename
        current_zones = self.nonstop_zones.all()
        from emissions.models import Nonstop

        for zone in Nonstop.objects.all():
86 87
            if not zone.slug in TRANCHE_SLUG_DIR_MAPPING:
                continue
88 89 90 91 92 93 94 95 96
            zone_dir = TRANCHE_SLUG_DIR_MAPPING[zone.slug]
            zone_path = os.path.join(LOCAL_BASE_PATH, 'Tranches', zone_dir, filename)
            if zone in current_zones:
                if not os.path.exists(zone_path):
                    os.symlink(os.path.join('..', '..', nonstop_file.short), zone_path)
            else:
                if os.path.exists(zone_path):
                    os.unlink(zone_path)

fred's avatar
fred committed
97

98 99
class NonstopFile(models.Model):
    filepath = models.CharField(_('Filepath'), max_length=255)
fred's avatar
fred committed
100
    filename = models.CharField(_('Filename'), max_length=255, null=True)
101
    creation_timestamp = models.DateTimeField(auto_now_add=True, null=True)
fred's avatar
fred committed
102
    track = models.ForeignKey(Track, null=True)
103

fred's avatar
fred committed
104 105 106 107
    @property
    def short(self):
        return self.filepath[len(REMOTE_BASE_PATH):]

fred's avatar
fred committed
108
    def set_track_filepath(self, filepath):
109
        self.filepath = os.path.join(REMOTE_BASE_PATH, 'tracks', filepath)
110 111 112 113
        self.filename = os.path.basename(filepath)

    def get_local_filepath(self):
        return os.path.join(LOCAL_BASE_PATH, self.short)
fred's avatar
fred committed
114

115

116 117 118 119 120 121
class SomaLogLine(models.Model):
    class Meta:
        verbose_name = _('Soma log line')
        verbose_name_plural = _('Soma log lines')
        ordering = ['play_timestamp']

122
    filepath = models.ForeignKey(NonstopFile)
123
    play_timestamp = models.DateTimeField()
124
    on_air = models.NullBooleanField('On Air')