2021-02-08 06:59:20 +00:00
|
|
|
from django.conf import settings
|
2020-08-16 15:08:58 +00:00
|
|
|
from rest_framework.request import Request
|
|
|
|
|
2021-02-08 06:59:20 +00:00
|
|
|
from assets.pagination import AssetPaginationBase
|
2021-02-05 05:29:29 +00:00
|
|
|
from perms.models import UserAssetGrantedTreeNodeRelation
|
2020-09-27 08:02:44 +00:00
|
|
|
from common.utils import get_logger
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
|
2021-02-08 06:59:20 +00:00
|
|
|
class GrantedAssetPaginationBase(AssetPaginationBase):
|
2022-08-22 10:32:33 +00:00
|
|
|
_user: object
|
|
|
|
|
2021-02-08 06:59:20 +00:00
|
|
|
def init_attrs(self, queryset, request: Request, view=None):
|
|
|
|
super().init_attrs(queryset, request, view)
|
|
|
|
self._user = view.user
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NodeGrantedAssetPagination(GrantedAssetPaginationBase):
|
|
|
|
def get_count_from_nodes(self, queryset):
|
2020-09-27 08:02:44 +00:00
|
|
|
node = getattr(self._view, 'pagination_node', None)
|
|
|
|
if node:
|
2022-08-22 10:32:33 +00:00
|
|
|
logger.debug(f'Hit node.assets_amount[{node.assets_amount}] -> '
|
|
|
|
f'{self._request.get_full_path()}')
|
2020-09-27 08:02:44 +00:00
|
|
|
return node.assets_amount
|
|
|
|
else:
|
2022-08-22 10:32:33 +00:00
|
|
|
logger.warn(f'Not hit node.assets_amount[{node}] because {self._view} '
|
|
|
|
f'not has `pagination_node` -> {self._request.get_full_path()}')
|
2021-02-08 06:59:20 +00:00
|
|
|
return None
|
2020-08-16 15:08:58 +00:00
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
class AllGrantedAssetPagination(GrantedAssetPaginationBase):
|
|
|
|
def get_count_from_nodes(self, queryset):
|
2021-02-08 06:59:20 +00:00
|
|
|
if settings.PERM_SINGLE_ASSET_TO_UNGROUP_NODE:
|
|
|
|
return None
|
2021-03-03 07:36:42 +00:00
|
|
|
values = UserAssetGrantedTreeNodeRelation.objects.filter(
|
2021-02-05 05:29:29 +00:00
|
|
|
user=self._user, node_parent_key=''
|
2021-03-03 07:36:42 +00:00
|
|
|
).values_list('node_assets_amount', flat=True)
|
|
|
|
if not values:
|
|
|
|
return None
|
|
|
|
|
|
|
|
assets_amount = sum(values)
|
2021-02-05 05:29:29 +00:00
|
|
|
logger.debug(f'Hit all assets amount {assets_amount} -> {self._request.get_full_path()}')
|
|
|
|
return assets_amount
|