forked from rc/aircox
cfr #121 Co-authored-by: Christophe Siraut <d@tobald.eu.org> Co-authored-by: bkfox <thomas bkfox net> Co-authored-by: Thomas Kairos <thomas@bkfox.net> Reviewed-on: rc/aircox#131 Co-authored-by: Chris Tactic <ctactic@noreply.git.radiocampus.be> Co-committed-by: Chris Tactic <ctactic@noreply.git.radiocampus.be>
This commit is contained in:
@ -1,11 +1,8 @@
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from django.conf import settings as conf
|
||||
from django.contrib.auth.models import Group
|
||||
from django.db import models
|
||||
from django.db.models import F
|
||||
from django.db.models.functions import Concat, Substr
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from aircox.conf import settings
|
||||
@ -13,13 +10,11 @@ from aircox.conf import settings
|
||||
from .page import Page, PageQuerySet
|
||||
from .station import Station
|
||||
|
||||
logger = logging.getLogger("aircox")
|
||||
|
||||
|
||||
__all__ = (
|
||||
"ProgramQuerySet",
|
||||
"Program",
|
||||
"ProgramChildQuerySet",
|
||||
"ProgramQuerySet",
|
||||
"Stream",
|
||||
)
|
||||
|
||||
@ -32,6 +27,16 @@ class ProgramQuerySet(PageQuerySet):
|
||||
def active(self):
|
||||
return self.filter(active=True)
|
||||
|
||||
def editor(self, user):
|
||||
"""Return programs for which user is an editor.
|
||||
|
||||
Superuser is considered as editor of all groups.
|
||||
"""
|
||||
if user.is_superuser:
|
||||
return self
|
||||
groups = self.request.user.groups.all()
|
||||
return self.filter(editors_group__in=groups)
|
||||
|
||||
|
||||
class Program(Page):
|
||||
"""A Program can either be a Streamed or a Scheduled program.
|
||||
@ -58,9 +63,12 @@ class Program(Page):
|
||||
default=True,
|
||||
help_text=_("update later diffusions according to schedule changes"),
|
||||
)
|
||||
editors_group = models.ForeignKey(Group, models.CASCADE, verbose_name=_("editors"))
|
||||
|
||||
objects = ProgramQuerySet.as_manager()
|
||||
detail_url_name = "program-detail"
|
||||
list_url_name = "program-list"
|
||||
edit_url_name = "program-edit"
|
||||
|
||||
@property
|
||||
def path(self):
|
||||
@ -80,11 +88,10 @@ class Program(Page):
|
||||
def excerpts_path(self):
|
||||
return os.path.join(self.path, settings.SOUND_ARCHIVES_SUBDIR)
|
||||
|
||||
def __init__(self, *kargs, **kwargs):
|
||||
super().__init__(*kargs, **kwargs)
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
if self.slug:
|
||||
self.__initial_path = self.path
|
||||
self.__initial_cover = self.cover
|
||||
|
||||
@classmethod
|
||||
def get_from_path(cl, path):
|
||||
@ -116,27 +123,21 @@ class Program(Page):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
def save(self, *kargs, **kwargs):
|
||||
from .sound import Sound
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.editors_group_id:
|
||||
from aircox import permissions
|
||||
|
||||
super().save(*kargs, **kwargs)
|
||||
saved = permissions.program.init(self)
|
||||
if saved:
|
||||
return
|
||||
|
||||
# TODO: move in signals
|
||||
path_ = getattr(self, "__initial_path", None)
|
||||
abspath = path_ and os.path.join(conf.MEDIA_ROOT, path_)
|
||||
if path_ is not None and path_ != self.path and os.path.exists(abspath) and not os.path.exists(self.abspath):
|
||||
logger.info(
|
||||
"program #%s's dir changed to %s - update it.",
|
||||
self.id,
|
||||
self.title,
|
||||
)
|
||||
|
||||
shutil.move(abspath, self.abspath)
|
||||
Sound.objects.filter(path__startswith=path_).update(file=Concat("file", Substr(F("file"), len(path_))))
|
||||
super().save()
|
||||
|
||||
|
||||
class ProgramChildQuerySet(PageQuerySet):
|
||||
def station(self, station=None, id=None):
|
||||
# lookup `__program` is due to parent being a page subclass (page is
|
||||
# concrete).
|
||||
return (
|
||||
self.filter(parent__program__station=station)
|
||||
if id is None
|
||||
@ -146,6 +147,10 @@ class ProgramChildQuerySet(PageQuerySet):
|
||||
def program(self, program=None, id=None):
|
||||
return self.parent(program, id)
|
||||
|
||||
def editor(self, user):
|
||||
programs = Program.objects.editor(user)
|
||||
return self.filter(parent__program__in=programs)
|
||||
|
||||
|
||||
class Stream(models.Model):
|
||||
"""When there are no program scheduled, it is possible to play sounds in
|
||||
|
Reference in New Issue
Block a user