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

import mutagen

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

fred's avatar
fred committed
10
REMOTE_BASE_PATH = '/srv/soma/nonstop/'
11 12 13 14 15 16 17 18 19 20 21 22 23
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',
}
24

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

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

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

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

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

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)
57
    cfwb = models.BooleanField('CFWB', default=False)
58
    nonstop_zones = models.ManyToManyField('emissions.Nonstop', blank=True)
fred's avatar
fred committed
59

fred's avatar
fred committed
60
    creation_timestamp = models.DateTimeField(auto_now_add=True, null=True)
fred's avatar
fred committed
61
    uploader = models.ForeignKey(settings.AUTH_USER_MODEL, null=True)
fred's avatar
fred committed
62 63 64 65

    class Meta:
        ordering = ['creation_timestamp']

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

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

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

    def file_exists(self):
fred's avatar
fred committed
77 78 79 80
        try:
            return os.path.exists(self.nonstopfile_set.order_by('creation_timestamp').last().get_local_filepath())
        except AttributeError:
            return False
81 82

    def sync_nonstop_zones(self):
83 84
        if not self.file_exists():
            return
85 86 87 88 89 90
        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():
91 92
            if not zone.slug in TRANCHE_SLUG_DIR_MAPPING:
                continue
93 94 95 96 97 98 99 100 101
            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
102

103 104
class NonstopFile(models.Model):
    filepath = models.CharField(_('Filepath'), max_length=255)
fred's avatar
fred committed
105
    filename = models.CharField(_('Filename'), max_length=255, null=True)
106
    creation_timestamp = models.DateTimeField(auto_now_add=True, null=True)
fred's avatar
fred committed
107
    track = models.ForeignKey(Track, null=True)
108

fred's avatar
fred committed
109 110 111 112
    @property
    def short(self):
        return self.filepath[len(REMOTE_BASE_PATH):]

fred's avatar
fred committed
113
    def set_track_filepath(self, filepath):
114
        self.filepath = os.path.join(REMOTE_BASE_PATH, 'tracks', filepath)
115 116 117 118
        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
119

120

121 122 123 124 125 126
class SomaLogLine(models.Model):
    class Meta:
        verbose_name = _('Soma log line')
        verbose_name_plural = _('Soma log lines')
        ordering = ['play_timestamp']

127
    filepath = models.ForeignKey(NonstopFile)
128
    play_timestamp = models.DateTimeField()
129
    on_air = models.NullBooleanField('On Air')