diff --git a/apps/perms/serializers/asset_permission.py b/apps/perms/serializers/asset_permission.py index d60ce28b9..a256a7a3c 100644 --- a/apps/perms/serializers/asset_permission.py +++ b/apps/perms/serializers/asset_permission.py @@ -57,8 +57,8 @@ class AssetPermissionSerializer(BulkOrgResourceModelSerializer): def setup_eager_loading(cls, queryset): """ Perform necessary eager loading of data. """ queryset = queryset.annotate( - users_amount=Count('users'), user_groups_amount=Count('user_groups'), - assets_amount=Count('assets'), nodes_amount=Count('nodes'), - system_users_amount=Count('system_users') + users_amount=Count('users', distinct=True), user_groups_amount=Count('user_groups', distinct=True), + assets_amount=Count('assets', distinct=True), nodes_amount=Count('nodes', distinct=True), + system_users_amount=Count('system_users', distinct=True) ) return queryset diff --git a/apps/perms/serializers/database_app_permission.py b/apps/perms/serializers/database_app_permission.py index a8813d8a6..0442a6122 100644 --- a/apps/perms/serializers/database_app_permission.py +++ b/apps/perms/serializers/database_app_permission.py @@ -13,20 +13,35 @@ __all__ = [ ] -class DatabaseAppPermissionSerializer(BulkOrgResourceModelSerializer): +class AmountMixin: + @classmethod + def setup_eager_loading(cls, queryset): + """ Perform necessary eager loading of data. """ + queryset = queryset.annotate( + users_amount=Count('users', distinct=True), user_groups_amount=Count('user_groups', distinct=True), + database_apps_amount=Count('database_apps', distinct=True), + system_users_amount=Count('system_users', distinct=True) + ) + return queryset + + +class DatabaseAppPermissionSerializer(AmountMixin, BulkOrgResourceModelSerializer): class Meta: model = models.DatabaseAppPermission list_serializer_class = AdaptedBulkListSerializer fields = [ - 'id', 'name', 'users', 'user_groups', - 'database_apps', 'system_users', 'comment', 'is_active', - 'date_start', 'date_expired', 'is_valid', - 'created_by', 'date_created' + 'id', 'name', 'users', 'user_groups', 'database_apps', 'system_users', + 'comment', 'is_active', 'date_start', 'date_expired', 'is_valid', + 'created_by', 'date_created', 'users_amount', 'user_groups_amount', + 'database_apps_amount', 'system_users_amount', + ] + read_only_fields = [ + 'created_by', 'date_created', 'users_amount', 'user_groups_amount', + 'database_apps_amount', 'system_users_amount', ] - read_only_fields = ['created_by', 'date_created'] -class DatabaseAppPermissionListSerializer(BulkOrgResourceModelSerializer): +class DatabaseAppPermissionListSerializer(AmountMixin, BulkOrgResourceModelSerializer): is_expired = serializers.BooleanField() class Meta: @@ -36,13 +51,3 @@ class DatabaseAppPermissionListSerializer(BulkOrgResourceModelSerializer): 'date_start', 'date_expired', 'is_valid', 'database_apps_amount', 'system_users_amount', 'created_by', 'date_created', 'is_expired' ] - - @classmethod - def setup_eager_loading(cls, queryset): - """ Perform necessary eager loading of data. """ - queryset = queryset.annotate( - users_amount=Count('users', distinct=True), user_groups_amount=Count('user_groups', distinct=True), - database_apps_amount=Count('database_apps', distinct=True), - system_users_amount=Count('system_users', distinct=True) - ) - return queryset