Skip to content

Commit

Permalink
Merge pull request #1005 from ita-social-projects/#999-BE-Analytics-o…
Browse files Browse the repository at this point in the history
…f-companies

#999 be analytics of companies
  • Loading branch information
Andrewakiv authored Dec 10, 2024
2 parents 1102419 + 6b5cbfb commit c9bd83d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 deletions.
7 changes: 7 additions & 0 deletions BackEnd/administration/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,10 @@ class FeedbackSerializer(serializers.Serializer):
required=True,
error_messages={"required": "Please select a category."},
)


class StatisticsSerializer(serializers.Serializer):
companies_count = serializers.IntegerField()
investors_count = serializers.IntegerField()
startups_count = serializers.IntegerField()
blocked_companies_count = serializers.IntegerField()
49 changes: 49 additions & 0 deletions BackEnd/administration/tests/test_profile_statistics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from rest_framework.test import APITestCase
from rest_framework import status

from administration.factories import AdminUserFactory, AdminProfileFactory


class TestProfileStatisticsStaff(APITestCase):
def setUp(self):
self.user = AdminUserFactory()
self.client.force_authenticate(self.user)
self.test_startup_user = AdminUserFactory(is_staff=False)
self.test_investor_user = AdminUserFactory(is_staff=False)
self.test_blocked_company_user = AdminUserFactory(is_staff=False)
self.startup_company = AdminProfileFactory(
person_id=self.test_startup_user.id, is_registered=False
)
self.investor_company = AdminProfileFactory(
person_id=self.test_investor_user.id, is_startup=False
)
self.blocked_company = AdminProfileFactory(
person_id=self.test_blocked_company_user.id, status="blocked"
)

def test_get_profile_statistics(self):
response = self.client.get("/api/admin/profiles/statistics/")
data = {
"companies_count": 3,
"investors_count": 2,
"startups_count": 2,
"blocked_companies_count": 1,
}
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, data)


class TestProfileStatisticsNotStaff(APITestCase):
def setUp(self):
self.user = AdminUserFactory(is_staff=False)
self.client.force_authenticate(self.user)

def test_get_profile_statistics(self):
response = self.client.get("/api/admin/profiles/statistics/")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)


class TestProfileStatisticsUnauthorized(APITestCase):
def test_get_profile_statistics(self):
response = self.client.get("/api/admin/profiles/statistics/")
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
6 changes: 6 additions & 0 deletions BackEnd/administration/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
ContactsView,
ProfilesListView,
ProfileDetailView,
ProfileStatisticsView,
UsersListView,
UserDetailView,
AutoModerationHoursView,
Expand All @@ -19,6 +20,11 @@
path("users/", UsersListView.as_view(), name="users-list"),
path("users/<pk>/", UserDetailView.as_view(), name="user-detail"),
path("profiles/", ProfilesListView.as_view(), name="profile-list"),
path(
"profiles/statistics/",
ProfileStatisticsView.as_view(),
name="profile-statistics",
),
path("profiles/<pk>/", ProfileDetailView.as_view(), name="profile-detail"),
path(
"automoderation/",
Expand Down
24 changes: 22 additions & 2 deletions BackEnd/administration/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from django.http import JsonResponse
from django.views import View
from django.db.models import Count, Q
from django_filters.rest_framework import DjangoFilterBackend

from drf_spectacular.utils import (
extend_schema,
OpenApiExample,
OpenApiResponse,
)
from rest_framework.generics import (
ListAPIView,
RetrieveAPIView,
RetrieveUpdateDestroyAPIView,
RetrieveUpdateAPIView,
CreateAPIView,
Expand All @@ -21,6 +25,7 @@
AdminUserDetailSerializer,
AutoModerationHoursSerializer,
ModerationEmailSerializer,
StatisticsSerializer,
)
from administration.pagination import ListPagination
from administration.models import AutoModeration, ModerationEmail
Expand All @@ -30,8 +35,6 @@
from .serializers import FeedbackSerializer
from utils.administration.send_email_feedback import send_email_feedback
from utils.administration.send_email_notification import send_email_to_user

from django_filters.rest_framework import DjangoFilterBackend
from .filters import UsersFilter


Expand Down Expand Up @@ -104,6 +107,23 @@ class ProfileDetailView(RetrieveUpdateDestroyAPIView):
)


class ProfileStatisticsView(RetrieveAPIView):
"""
Count of companies
"""

permission_classes = [IsStaffUser]
serializer_class = StatisticsSerializer

def get_object(self):
return Profile.objects.aggregate(
companies_count=Count("pk"),
investors_count=Count("pk", filter=Q(is_registered=True)),
startups_count=Count("pk", filter=Q(is_startup=True)),
blocked_companies_count=Count("pk", filter=Q(status="blocked")),
)


@extend_schema(
request=AutoModerationHoursSerializer,
responses={
Expand Down
2 changes: 1 addition & 1 deletion BackEnd/profiles/tests/test_ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ def test_get_list_of_profiles_completeness_order_asc(self):
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(
[
"Bakery",
"Winery",
"Bakery",
"Delivery company",
"Catering service",
"Retail company",
Expand Down

0 comments on commit c9bd83d

Please sign in to comment.