fix errors in streamer; filter out diff in station.on_air
This commit is contained in:
		@ -49,8 +49,7 @@ class TrackInline(GenericTabularInline):
 | 
			
		||||
    ct_fk_field = 'related_id'
 | 
			
		||||
    model = Track
 | 
			
		||||
    extra = 0
 | 
			
		||||
    fields = ('artist', 'title', 'info', 'position')
 | 
			
		||||
    readonly_fields = ('position',)
 | 
			
		||||
    fields = ('artist', 'title', 'info', 'position', 'in_seconds', 'tags')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@admin.register(Sound)
 | 
			
		||||
 | 
			
		||||
@ -15,6 +15,7 @@ from django.conf import settings as main_settings
 | 
			
		||||
from django.core.management.base import BaseCommand, CommandError
 | 
			
		||||
from django.utils import timezone as tz
 | 
			
		||||
from django.utils.functional import cached_property
 | 
			
		||||
from django.db import models
 | 
			
		||||
 | 
			
		||||
from aircox.models import Station, Diffusion, Track, Sound, Log #, DiffusionLog, SoundLog
 | 
			
		||||
 | 
			
		||||
@ -56,8 +57,8 @@ class Monitor:
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def _last_log(self, **kwargs):
 | 
			
		||||
        return Log.objects.station(self.station, **kwargs) \
 | 
			
		||||
    def get_last_log(self, *args, **kwargs):
 | 
			
		||||
        return Log.objects.station(self.station, *args, **kwargs) \
 | 
			
		||||
                  .select_related('diffusion', 'sound') \
 | 
			
		||||
                  .order_by('date').last()
 | 
			
		||||
 | 
			
		||||
@ -66,23 +67,23 @@ class Monitor:
 | 
			
		||||
        """
 | 
			
		||||
        Last log of monitored station
 | 
			
		||||
        """
 | 
			
		||||
        return self._last_log()
 | 
			
		||||
        return self.get_last_log()
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def last_sound(self):
 | 
			
		||||
        """
 | 
			
		||||
        Last sound log of monitored station that occurred on_air
 | 
			
		||||
        """
 | 
			
		||||
        return self._last_log(type = Log.Type.on_air,
 | 
			
		||||
                              sound__isnull = False)
 | 
			
		||||
        return self.get_last_log(type = Log.Type.on_air,
 | 
			
		||||
                                 sound__isnull = False)
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def last_diff_start(self):
 | 
			
		||||
        """
 | 
			
		||||
        Log of last triggered item (sound or diffusion)
 | 
			
		||||
        """
 | 
			
		||||
        return self._last_log(type = Log.Type.start,
 | 
			
		||||
                              diffusion__isnull = False)
 | 
			
		||||
        return self.get_last_log(type = Log.Type.start,
 | 
			
		||||
                                 diffusion__isnull = False)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def __init__(self, station, **kwargs):
 | 
			
		||||
@ -129,9 +130,11 @@ class Monitor:
 | 
			
		||||
        current_sound = self.streamer.current_sound
 | 
			
		||||
        current_source = self.streamer.current_source
 | 
			
		||||
        if not current_sound or not current_source:
 | 
			
		||||
            print('no source / no sound', current_sound, current_source)
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        log = self.last_log
 | 
			
		||||
        log = self.get_last_log(models.Q(diffusion__isnull = False) |
 | 
			
		||||
                                models.Q(sound__isnull = False))
 | 
			
		||||
 | 
			
		||||
        # sound on air changed
 | 
			
		||||
        if log.source != current_source.id or \
 | 
			
		||||
@ -167,6 +170,7 @@ class Monitor:
 | 
			
		||||
        Log tracks for the given sound log (for streamed programs).
 | 
			
		||||
        Called by self.trace
 | 
			
		||||
        """
 | 
			
		||||
        # TODO take restart in account
 | 
			
		||||
        tracks = Track.objects.get_for(object = log.sound) \
 | 
			
		||||
                              .filter(in_seconds = True)
 | 
			
		||||
        if not tracks.exists():
 | 
			
		||||
 | 
			
		||||
@ -235,7 +235,7 @@ class Station(Nameable):
 | 
			
		||||
        """
 | 
			
		||||
        return Log.objects.on_air(self, *args, **kwargs)
 | 
			
		||||
 | 
			
		||||
    def on_air(self, date = None, count = 0):
 | 
			
		||||
    def on_air(self, date = None, count = 0, no_cache = False):
 | 
			
		||||
        """
 | 
			
		||||
        Return a queryset of what happened on air, based on logs and
 | 
			
		||||
        diffusions informations. The queryset is sorted by -date.
 | 
			
		||||
@ -261,16 +261,18 @@ class Station(Nameable):
 | 
			
		||||
        if date and date > datetime.date.today():
 | 
			
		||||
            return []
 | 
			
		||||
 | 
			
		||||
        now = tz.now()
 | 
			
		||||
        if date:
 | 
			
		||||
            logs = Log.objects.at(self, date)
 | 
			
		||||
            diffs = Diffusion.objects \
 | 
			
		||||
                             .at(self, date,
 | 
			
		||||
                                 type = Diffusion.Type.normal) \
 | 
			
		||||
                             .at(self, date, type = Diffusion.Type.normal) \
 | 
			
		||||
                             .filter(start__lte = now) \
 | 
			
		||||
                             .order_by('-start')
 | 
			
		||||
        else:
 | 
			
		||||
            logs = Log.objects
 | 
			
		||||
            diffs = Diffusion.objects.filter(type = Diffusion.Type.normal,
 | 
			
		||||
                                             start__lte = tz.now()) \
 | 
			
		||||
            diffs = Diffusion.objects \
 | 
			
		||||
                             .filter(type = Diffusion.Type.normal,
 | 
			
		||||
                                     start__lte = now) \
 | 
			
		||||
                             .order_by('-start')[:count]
 | 
			
		||||
 | 
			
		||||
        q = models.Q(diffusion__isnull = False) | \
 | 
			
		||||
@ -1199,24 +1201,22 @@ class Port (models.Model):
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class LogManager(models.Manager):
 | 
			
		||||
    def station(self, station, qs = None, **kwargs):
 | 
			
		||||
        qs = self if qs is None else qs
 | 
			
		||||
        return qs.filter(station = station, **kwargs)
 | 
			
		||||
    def station(self, station, *args, **kwargs):
 | 
			
		||||
        return self.filter(*args, station = station, **kwargs)
 | 
			
		||||
 | 
			
		||||
    def _at(self, date = None, qs = None, **kwargs):
 | 
			
		||||
    def _at(self, date = None, *args, **kwargs):
 | 
			
		||||
        start, end = utils.date_range(date)
 | 
			
		||||
        qs = self if qs is None else qs
 | 
			
		||||
        # return qs.filter(models.Q(end__gte = start) |
 | 
			
		||||
        #                 models.Q(date__lte = end))
 | 
			
		||||
        return qs.filter(date__gte = start, date__lte = end, **kwargs)
 | 
			
		||||
        return self.filter(*args, date__gte = start, date__lte = end, **kwargs)
 | 
			
		||||
 | 
			
		||||
    def at(self, station = None, date = None, qs = None, **kwargs):
 | 
			
		||||
    def at(self, station = None, date = None, *args, **kwargs):
 | 
			
		||||
        """
 | 
			
		||||
        Return a queryset of logs that have the given date
 | 
			
		||||
        in their range.
 | 
			
		||||
        """
 | 
			
		||||
        qs = self._at(date, qs, **kwargs)
 | 
			
		||||
        return self.station(station, qs) if station else qs
 | 
			
		||||
        qs = self._at(date, *args, **kwargs)
 | 
			
		||||
        return qs.filter(station = station) if station else qs
 | 
			
		||||
 | 
			
		||||
    # TODO: rename on_air + rename Station.on_air into sth like regular_on_air
 | 
			
		||||
    def on_air(self, station, date = None, **kwargs):
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user