forked from rc/aircox
code quality
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
from django.http import Http404
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.utils import timezone as tz
|
||||
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.exceptions import ValidationError
|
||||
@ -9,23 +8,34 @@ from rest_framework.permissions import IsAdminUser
|
||||
from rest_framework.response import Response
|
||||
|
||||
from aircox.models import Sound, Station
|
||||
from aircox.serializers import SoundSerializer
|
||||
|
||||
from . import controllers
|
||||
from .serializers import *
|
||||
from .serializers import (
|
||||
PlaylistSerializer,
|
||||
QueueSourceSerializer,
|
||||
RequestSerializer,
|
||||
SourceSerializer,
|
||||
StreamerSerializer,
|
||||
)
|
||||
|
||||
|
||||
__all__ = ['Streamers', 'BaseControllerAPIView',
|
||||
'RequestViewSet', 'StreamerViewSet', 'SourceViewSet',
|
||||
'PlaylistSourceViewSet', 'QueueSourceViewSet']
|
||||
__all__ = [
|
||||
"Streamers",
|
||||
"BaseControllerAPIView",
|
||||
"RequestViewSet",
|
||||
"StreamerViewSet",
|
||||
"SourceViewSet",
|
||||
"PlaylistSourceViewSet",
|
||||
"QueueSourceViewSet",
|
||||
]
|
||||
|
||||
|
||||
class Streamers:
|
||||
date = None
|
||||
""" next update datetime """
|
||||
"""Next update datetime."""
|
||||
streamers = None
|
||||
""" stations by station id """
|
||||
"""Stations by station id."""
|
||||
timeout = None
|
||||
""" timedelta to next update """
|
||||
"""Timedelta to next update."""
|
||||
|
||||
def __init__(self, timeout=None):
|
||||
self.timeout = timeout or tz.timedelta(seconds=2)
|
||||
@ -34,14 +44,19 @@ class Streamers:
|
||||
# FIXME: cf. TODO in aircox.controllers about model updates
|
||||
stations = Station.objects.active()
|
||||
if self.streamers is None or force:
|
||||
self.streamers = {station.pk: controllers.Streamer(station)
|
||||
for station in stations}
|
||||
self.streamers = {
|
||||
station.pk: controllers.Streamer(station)
|
||||
for station in stations
|
||||
}
|
||||
return
|
||||
|
||||
streamers = self.streamers
|
||||
self.streamers = {station.pk: controllers.Streamer(station)
|
||||
if station.pk in streamers else streamers[station.pk]
|
||||
for station in stations}
|
||||
self.streamers = {
|
||||
station.pk: controllers.Streamer(station)
|
||||
if station.pk in streamers
|
||||
else streamers[station.pk]
|
||||
for station in stations
|
||||
}
|
||||
|
||||
def fetch(self):
|
||||
if self.streamers is None:
|
||||
@ -81,7 +96,7 @@ class BaseControllerAPIView(viewsets.ViewSet):
|
||||
streamers.fetch()
|
||||
id = int(request.station.pk if station_pk is None else station_pk)
|
||||
if id not in streamers:
|
||||
raise Http404('station not found')
|
||||
raise Http404("station not found")
|
||||
return streamers[id]
|
||||
|
||||
def get_serializer(self, **kwargs):
|
||||
@ -108,13 +123,13 @@ class StreamerViewSet(BaseControllerAPIView):
|
||||
return Response(self.serialize(self.streamer))
|
||||
|
||||
def list(self, request, pk=None):
|
||||
return Response({
|
||||
'results': self.serialize(streamers.values(), many=True)
|
||||
})
|
||||
return Response(
|
||||
{"results": self.serialize(streamers.values(), many=True)}
|
||||
)
|
||||
|
||||
def dispatch(self, request, *args, pk=None, **kwargs):
|
||||
if pk is not None:
|
||||
kwargs.setdefault('station_pk', pk)
|
||||
kwargs.setdefault("station_pk", pk)
|
||||
self.streamer = self.get_streamer(request, **kwargs)
|
||||
self.object = self.streamer
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
@ -128,10 +143,11 @@ class SourceViewSet(BaseControllerAPIView):
|
||||
return (s for s in self.streamer.sources if isinstance(s, self.model))
|
||||
|
||||
def get_source(self, pk):
|
||||
source = next((source for source in self.get_sources()
|
||||
if source.id == pk), None)
|
||||
source = next(
|
||||
(source for source in self.get_sources() if source.id == pk), None
|
||||
)
|
||||
if source is None:
|
||||
raise Http404('source `%s` not found' % pk)
|
||||
raise Http404("source `%s` not found" % pk)
|
||||
return source
|
||||
|
||||
def retrieve(self, request, pk=None):
|
||||
@ -139,9 +155,9 @@ class SourceViewSet(BaseControllerAPIView):
|
||||
return Response(self.serialize())
|
||||
|
||||
def list(self, request):
|
||||
return Response({
|
||||
'results': self.serialize(self.get_sources(), many=True)
|
||||
})
|
||||
return Response(
|
||||
{"results": self.serialize(self.get_sources(), many=True)}
|
||||
)
|
||||
|
||||
def _run(self, pk, action):
|
||||
source = self.object = self.get_source(pk)
|
||||
@ -149,21 +165,21 @@ class SourceViewSet(BaseControllerAPIView):
|
||||
source.fetch()
|
||||
return Response(self.serialize(source))
|
||||
|
||||
@action(detail=True, methods=['POST'])
|
||||
@action(detail=True, methods=["POST"])
|
||||
def sync(self, request, pk):
|
||||
return self._run(pk, lambda s: s.sync())
|
||||
|
||||
@action(detail=True, methods=['POST'])
|
||||
@action(detail=True, methods=["POST"])
|
||||
def skip(self, request, pk):
|
||||
return self._run(pk, lambda s: s.skip())
|
||||
|
||||
@action(detail=True, methods=['POST'])
|
||||
@action(detail=True, methods=["POST"])
|
||||
def restart(self, request, pk):
|
||||
return self._run(pk, lambda s: s.restart())
|
||||
|
||||
@action(detail=True, methods=['POST'])
|
||||
@action(detail=True, methods=["POST"])
|
||||
def seek(self, request, pk):
|
||||
count = request.POST['seek']
|
||||
count = request.POST["seek"]
|
||||
return self._run(pk, lambda s: s.seek(count))
|
||||
|
||||
|
||||
@ -179,13 +195,14 @@ class QueueSourceViewSet(SourceViewSet):
|
||||
def get_sound_queryset(self):
|
||||
return Sound.objects.station(self.request.station).archive()
|
||||
|
||||
@action(detail=True, methods=['POST'])
|
||||
@action(detail=True, methods=["POST"])
|
||||
def push(self, request, pk):
|
||||
if not request.data.get('sound_id'):
|
||||
if not request.data.get("sound_id"):
|
||||
raise ValidationError('missing "sound_id" POST data')
|
||||
|
||||
sound = get_object_or_404(self.get_sound_queryset(),
|
||||
pk=request.data['sound_id'])
|
||||
sound = get_object_or_404(
|
||||
self.get_sound_queryset(), pk=request.data["sound_id"]
|
||||
)
|
||||
return self._run(
|
||||
pk, lambda s: s.push(sound.file.path) if sound.file.path else None)
|
||||
|
||||
pk, lambda s: s.push(sound.file.path) if sound.file.path else None
|
||||
)
|
||||
|
Reference in New Issue
Block a user