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")), ]