From 680e7d766826421060ea562e7cbe0a79c96e93c8 Mon Sep 17 00:00:00 2001 From: Andrewakiv Date: Thu, 5 Dec 2024 15:58:41 +0200 Subject: [PATCH] companies stats --- BackEnd/administration/serializers.py | 22 +++++++++++++++++ BackEnd/administration/urls.py | 6 +++++ BackEnd/administration/views.py | 35 +++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/BackEnd/administration/serializers.py b/BackEnd/administration/serializers.py index 24cda9beb..f84ec9c08 100644 --- a/BackEnd/administration/serializers.py +++ b/BackEnd/administration/serializers.py @@ -1,4 +1,5 @@ from django.contrib.auth import get_user_model +from django.db.models import Q from rest_framework import serializers from utils.administration.feedback_category import FeedbackCategory from authentication.models import CustomUser @@ -213,3 +214,24 @@ class FeedbackSerializer(serializers.Serializer): required=True, error_messages={"required": "Please select a category."}, ) + + +class StatisticsSerializer(serializers.Serializer): + companies_count = serializers.SerializerMethodField() + investors_count = serializers.SerializerMethodField() + startups_count = serializers.SerializerMethodField() + blocked_companies_count = serializers.SerializerMethodField() + + def get_companies_count(self, obj) -> int: + return Profile.objects.filter( + Q(is_registered=True) | Q(is_startup=True) + ).count() + + def get_investors_count(self, obj) -> int: + return Profile.objects.filter(is_registered=True).count() + + def get_startups_count(self, obj) -> int: + return Profile.objects.filter(is_startup=True).count() + + def get_blocked_companies_count(self, obj) -> int: + return Profile.objects.filter(status='blocked').count() diff --git a/BackEnd/administration/urls.py b/BackEnd/administration/urls.py index d860830c3..ec746ec3e 100644 --- a/BackEnd/administration/urls.py +++ b/BackEnd/administration/urls.py @@ -4,6 +4,7 @@ ContactsView, ProfilesListView, ProfileDetailView, + ProfileStatisticsView, UsersListView, UserDetailView, AutoModerationHoursView, @@ -19,6 +20,11 @@ path("users//", UserDetailView.as_view(), name="user-detail"), path("profiles/", ProfilesListView.as_view(), name="profile-list"), path("profiles//", ProfileDetailView.as_view(), name="profile-detail"), + path( + "companies/satistics/", + ProfileStatisticsView.as_view(), + name="profile-satistics", + ), path( "automoderation/", AutoModerationHoursView.as_view(), diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 7f137a5bc..c3bab2558 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -1,5 +1,6 @@ from django.http import JsonResponse from django.views import View +from django.db.models import Q from drf_spectacular.utils import ( extend_schema, OpenApiExample, @@ -8,6 +9,7 @@ from rest_framework.generics import ( ListAPIView, + RetrieveAPIView, RetrieveUpdateDestroyAPIView, RetrieveUpdateAPIView, CreateAPIView, @@ -22,6 +24,7 @@ AdminUserDetailSerializer, AutoModerationHoursSerializer, ModerationEmailSerializer, + StatisticsSerializer, ) from administration.pagination import ListPagination from administration.models import AutoModeration, ModerationEmail @@ -34,6 +37,8 @@ from django_filters.rest_framework import DjangoFilterBackend from .filters import UsersFilter +from rest_framework.permissions import AllowAny + class UsersListView(ListAPIView): """ @@ -104,6 +109,36 @@ class ProfileDetailView(RetrieveUpdateDestroyAPIView): ) +class ProfileStatisticsView(RetrieveAPIView): + """ + Count of companies + """ + queryset = Profile.objects.all() + permission_classes = [AllowAny] + serializer_class = StatisticsSerializer + + def get_object(self): + return {} + + # def get_serializer_context(self): + # context = super().get_serializer_context() + # context["companies_count"] = ( + # self.get_queryset() + # .filter(Q(is_registered=True) | Q(is_startup=True)) + # .count() + # ) + # context["investors_count"] = ( + # self.get_queryset().filter(is_registered=True).count() + # ) + # context["startups_count"] = ( + # self.get_queryset().filter(is_startup=True).count() + # ) + # context["blocked_companies_count"] = ( + # self.get_queryset().filter(status='blocked').count() + # ) + # return context + + @extend_schema( request=AutoModerationHoursSerializer, responses={