models.py 4.6 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
from django.db import models
8
from django.utils.timezone import now
9 10
from django.utils.translation import ugettext_lazy as _

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

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

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

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

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

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

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

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

    class Meta:
        ordering = ['creation_timestamp']

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

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

fred's avatar
fred committed
75 76 77 78
    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
79 80 81 82
        try:
            return os.path.exists(self.nonstopfile_set.order_by('creation_timestamp').last().get_local_filepath())
        except AttributeError:
            return False
fred's avatar
fred committed
83 84

    def sync_nonstop_zones(self):
85 86 87 88 89 90 91 92 93
        current_zones = self.nonstop_zones.all()
        if current_zones.count():
            if not self.added_to_nonstop_timestamp:
                self.added_to_nonstop_timestamp = now()
                self.save()
        else:
            self.added_to_nonstop_timestamp = None
            self.save()

94 95
        if not self.file_exists():
            return
fred's avatar
fred committed
96 97 98 99 100
        nonstop_file = self.nonstopfile_set.order_by('creation_timestamp').last()
        filename = nonstop_file.filename
        from emissions.models import Nonstop

        for zone in Nonstop.objects.all():
101 102
            if not zone.slug in TRANCHE_SLUG_DIR_MAPPING:
                continue
fred's avatar
fred committed
103 104 105 106 107 108 109 110 111
            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
112

113 114
class NonstopFile(models.Model):
    filepath = models.CharField(_('Filepath'), max_length=255)
fred's avatar
fred committed
115
    filename = models.CharField(_('Filename'), max_length=255, null=True)
116
    creation_timestamp = models.DateTimeField(auto_now_add=True, null=True)
fred's avatar
fred committed
117
    track = models.ForeignKey(Track, null=True)
118

fred's avatar
fred committed
119 120 121 122
    @property
    def short(self):
        return self.filepath[len(REMOTE_BASE_PATH):]

fred's avatar
fred committed
123
    def set_track_filepath(self, filepath):
fred's avatar
fred committed
124
        self.filepath = os.path.join(REMOTE_BASE_PATH, 'tracks', filepath)
fred's avatar
fred committed
125 126 127
        self.filename = os.path.basename(filepath)

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

130

131 132 133 134 135 136
class SomaLogLine(models.Model):
    class Meta:
        verbose_name = _('Soma log line')
        verbose_name_plural = _('Soma log lines')
        ordering = ['play_timestamp']

137
    filepath = models.ForeignKey(NonstopFile)
138
    play_timestamp = models.DateTimeField()
fred's avatar
fred committed
139
    on_air = models.NullBooleanField('On Air')