Add DashboardApiView

This commit is contained in:
Paweł Jastrzębski 2021-10-13 19:21:52 +02:00
parent 90b2896ded
commit a963694fd0
3 changed files with 36 additions and 2 deletions

7
shynet/api/urls.py Normal file
View File

@ -0,0 +1,7 @@
from django.urls import path
from . import views
urlpatterns = [
path("dashboard/", views.DashboardApiView.as_view(), name="services"),
]

View File

@ -1,3 +1,29 @@
from django.shortcuts import render
from django.http import JsonResponse
from django.db.models import Q
from django.db.models.query import QuerySet
from django.views.generic import View
# Create your views here.
from dashboard.mixins import DateRangeMixin
from core.models import Service
from .mixins import ApiTokenRequiredMixin
class DashboardApiView(ApiTokenRequiredMixin, DateRangeMixin, View):
def get(self, request, *args, **kwargs):
services = Service.objects.filter(
Q(owner=request.user) | Q(collaborators__in=[request.user])
).distinct()
start = self.get_start_date()
end = self.get_end_date()
services_data = [s.get_core_stats(start, end) for s in services]
for service_data in services_data:
for key, value in service_data.items():
if isinstance(value, QuerySet):
service_data[key] = list(value)
for key, value in service_data['compare'].items():
if isinstance(value, QuerySet):
service_data['compare'][key] = list(value)
return JsonResponse(data={'services': services_data})

View File

@ -25,4 +25,5 @@ urlpatterns = [
path("dashboard/", include(("dashboard.urls", "dashboard"), namespace="dashboard")),
path("healthz/", include("health_check.urls")),
path("", include(("core.urls", "core"), namespace="core")),
path("api/", include(("api.urls", "api"), namespace="api")),
]