From a963694fd0958df65ecdd422034076a2123b2004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Wed, 13 Oct 2021 19:21:52 +0200 Subject: [PATCH] Add DashboardApiView --- shynet/api/urls.py | 7 +++++++ shynet/api/views.py | 30 ++++++++++++++++++++++++++++-- shynet/shynet/urls.py | 1 + 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 shynet/api/urls.py diff --git a/shynet/api/urls.py b/shynet/api/urls.py new file mode 100644 index 0000000..11d877f --- /dev/null +++ b/shynet/api/urls.py @@ -0,0 +1,7 @@ +from django.urls import path + +from . import views + +urlpatterns = [ + path("dashboard/", views.DashboardApiView.as_view(), name="services"), +] diff --git a/shynet/api/views.py b/shynet/api/views.py index 91ea44a..e3b5290 100644 --- a/shynet/api/views.py +++ b/shynet/api/views.py @@ -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}) diff --git a/shynet/shynet/urls.py b/shynet/shynet/urls.py index 7264261..71aabe6 100644 --- a/shynet/shynet/urls.py +++ b/shynet/shynet/urls.py @@ -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")), ]