forked from rc/aircox
update
This commit is contained in:
@ -1,11 +1,11 @@
|
||||
from . import api, admin
|
||||
from . import admin
|
||||
|
||||
from .base import BaseView
|
||||
from .base import BaseView, BaseAPIView
|
||||
from .home import HomeView
|
||||
|
||||
from .article import ArticleDetailView, ArticleListView
|
||||
from .episode import EpisodeDetailView, EpisodeListView, DiffusionListView
|
||||
from .log import LogListView
|
||||
from .log import LogListView, LogListAPIView
|
||||
from .page import PageDetailView, PageListView
|
||||
from .program import ProgramDetailView, ProgramListView, \
|
||||
ProgramPageDetailView, ProgramPageListView
|
||||
|
@ -1,48 +0,0 @@
|
||||
import datetime
|
||||
|
||||
from django.utils import timezone as tz
|
||||
|
||||
from rest_framework.generics import ListAPIView
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.decorators import action
|
||||
|
||||
from ..models import Log
|
||||
from ..serializers import LogInfo, LogInfoSerializer
|
||||
from .log import LogListMixin
|
||||
|
||||
|
||||
class BaseAPIView:
|
||||
@property
|
||||
def station(self):
|
||||
return self.request.station
|
||||
|
||||
def get_queryset(self):
|
||||
return super().get_queryset().station(self.station)
|
||||
|
||||
|
||||
class LogListAPIView(LogListMixin, BaseAPIView, ListAPIView):
|
||||
"""
|
||||
Return logs list, including diffusions. By default return logs of
|
||||
the last 30 minutes.
|
||||
|
||||
Available GET parameters:
|
||||
- "date": return logs for a specified date (
|
||||
- "full": (staff user only) don't merge diffusion and logs
|
||||
"""
|
||||
serializer_class = LogInfoSerializer
|
||||
queryset = Log.objects.all()
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
self.date = self.get_date()
|
||||
if self.date is None:
|
||||
self.min_date = tz.now() - tz.timedelta(minutes=30)
|
||||
return super().get(*args, **kwargs)
|
||||
|
||||
def get_object_list(self, logs, full):
|
||||
return [LogInfo(obj) for obj in super().get_object_list(logs, full)]
|
||||
|
||||
def get_serializer(self, queryset, *args, **kwargs):
|
||||
full = bool(self.request.GET.get('full'))
|
||||
return super().get_serializer(self.get_object_list(queryset, full),
|
||||
*args, **kwargs)
|
||||
|
@ -59,3 +59,12 @@ class BaseView(TemplateResponseMixin, ContextMixin):
|
||||
return super().get_context_data(**kwargs)
|
||||
|
||||
|
||||
class BaseAPIView:
|
||||
@property
|
||||
def station(self):
|
||||
return self.request.station
|
||||
|
||||
def get_queryset(self):
|
||||
return super().get_queryset().station(self.station)
|
||||
|
||||
|
||||
|
@ -4,8 +4,13 @@ import datetime
|
||||
from django.views.generic import ListView
|
||||
from django.utils import timezone as tz
|
||||
|
||||
from rest_framework.generics import ListAPIView
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.decorators import action
|
||||
|
||||
from ..models import Diffusion, Log
|
||||
from .base import BaseView
|
||||
from ..serializers import LogInfo, LogInfoSerializer
|
||||
from .base import BaseView, BaseAPIView
|
||||
from .mixins import GetDateMixin
|
||||
|
||||
|
||||
@ -68,3 +73,30 @@ class LogListView(BaseView, LogListMixin, ListView):
|
||||
return super().get_context_data(**kwargs)
|
||||
|
||||
|
||||
# Logs are accessible through API only with this list view
|
||||
class LogListAPIView(LogListMixin, BaseAPIView, ListAPIView):
|
||||
"""
|
||||
Return logs list, including diffusions. By default return logs of
|
||||
the last 30 minutes.
|
||||
|
||||
Available GET parameters:
|
||||
- "date": return logs for a specified date (
|
||||
- "full": (staff user only) don't merge diffusion and logs
|
||||
"""
|
||||
serializer_class = LogInfoSerializer
|
||||
queryset = Log.objects.all()
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
self.date = self.get_date()
|
||||
if self.date is None:
|
||||
self.min_date = tz.now() - tz.timedelta(minutes=30)
|
||||
return super().get(*args, **kwargs)
|
||||
|
||||
def get_object_list(self, logs, full):
|
||||
return [LogInfo(obj) for obj in super().get_object_list(logs, full)]
|
||||
|
||||
def get_serializer(self, queryset, *args, **kwargs):
|
||||
full = bool(self.request.GET.get('full'))
|
||||
return super().get_serializer(self.get_object_list(queryset, full),
|
||||
*args, **kwargs)
|
||||
|
||||
|
Reference in New Issue
Block a user