From c290b8ebed8d33e46d56a6cba9f8b5844c7a75e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20L=C3=A9ger?= Date: Mon, 9 Dec 2024 17:17:16 -0500 Subject: [PATCH] feat(organization)!: simplify organization detection for retrieving usage metrics (#5332) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### 📣 Summary Simplified the logic for detecting the organization when retrieving project usage, storage, and NLP minutes. Improved code readability and maintainability. ### 📖 Description This PR refactors the organization detection logic used to retrieve metrics such as project usage, storage, and NLP minutes. The simplified makes the code easier to read and maintain. The detection is now based on whether the organization is a Multi-Member Organization (MMO). Additionally, it assumes that all projects belong directly to the organization rather than its individual members. --- kobo/apps/organizations/views.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/kobo/apps/organizations/views.py b/kobo/apps/organizations/views.py index f45950fa57..d8fc3fd0f7 100644 --- a/kobo/apps/organizations/views.py +++ b/kobo/apps/organizations/views.py @@ -1,11 +1,4 @@ -from django.db.models import ( - QuerySet, - Case, - When, - Value, - CharField, - OuterRef, -) +from django.db.models import Case, CharField, OuterRef, QuerySet, Value, When from django.db.models.expressions import Exists from django.utils.http import http_date from rest_framework import viewsets @@ -102,7 +95,9 @@ def assets(self, request: Request, *args, **kwargs): ### Additional Information For more details, please refer to `/api/v2/assets/`. """ - organization = self.get_object() # Call check permissions + + # `get_object()` checks permissions + organization = self.get_object() # Permissions check is done by `OrganizationAssetViewSet` permission classes asset_view = OrganizationAssetViewSet.as_view({'get': 'list'}) @@ -154,8 +149,8 @@ def service_usage(self, request, pk=None, *args, **kwargs): > } ### CURRENT ENDPOINT """ - self.get_object() # check permissions + self.get_object() # This call is necessary to check permissions serializer = ServiceUsageSerializer( get_database_user(request.user), context=self.get_serializer_context(), @@ -210,6 +205,7 @@ def asset_usage(self, request, pk=None, *args, **kwargs): ### CURRENT ENDPOINT """ + # `get_object()` checks permissions organization = self.get_object() user_id = get_database_user(request.user).pk