2022-12-07 10:38:03 +00:00
|
|
|
import abc
|
2023-02-20 05:31:56 +00:00
|
|
|
|
2022-12-02 05:15:03 +00:00
|
|
|
from rest_framework.generics import ListAPIView
|
2021-02-05 05:29:29 +00:00
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
from assets.api.asset.asset import AssetFilterSet
|
2023-02-20 05:31:56 +00:00
|
|
|
from assets.models import Asset, Node
|
|
|
|
from common.utils import get_logger, lazyproperty
|
2021-02-05 05:29:29 +00:00
|
|
|
from perms import serializers
|
2022-12-07 10:38:03 +00:00
|
|
|
from perms.pagination import AllPermedAssetPagination
|
|
|
|
from perms.pagination import NodePermedAssetPagination
|
2022-12-21 09:36:44 +00:00
|
|
|
from perms.utils import UserPermAssetUtil
|
2022-12-02 05:15:03 +00:00
|
|
|
from .mixin import (
|
2022-12-07 10:38:03 +00:00
|
|
|
SelfOrPKUserMixin
|
2022-12-02 05:15:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
__all__ = [
|
2022-12-07 10:38:03 +00:00
|
|
|
'UserAllPermedAssetsApi',
|
2022-12-02 05:15:03 +00:00
|
|
|
'UserDirectPermedAssetsApi',
|
|
|
|
'UserFavoriteAssetsApi',
|
|
|
|
'UserPermedNodeAssetsApi',
|
|
|
|
]
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
class BaseUserPermedAssetsApi(SelfOrPKUserMixin, ListAPIView):
|
|
|
|
ordering = ('name',)
|
|
|
|
search_fields = ('name', 'address', 'comment')
|
2023-02-20 05:36:50 +00:00
|
|
|
ordering_fields = ("name", "address")
|
2022-12-07 10:38:03 +00:00
|
|
|
filterset_class = AssetFilterSet
|
|
|
|
serializer_class = serializers.AssetPermedSerializer
|
|
|
|
only_fields = serializers.AssetPermedSerializer.Meta.only_fields
|
2022-12-02 05:15:03 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
if getattr(self, 'swagger_fake_view', False):
|
|
|
|
return Asset.objects.none()
|
2022-12-07 10:38:03 +00:00
|
|
|
assets = self.get_assets()
|
2022-12-02 05:15:03 +00:00
|
|
|
assets = assets.prefetch_related('platform').only(*self.only_fields)
|
|
|
|
return assets
|
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
@abc.abstractmethod
|
|
|
|
def get_assets(self):
|
|
|
|
return Asset.objects.none()
|
2022-09-08 12:31:57 +00:00
|
|
|
|
2022-12-21 09:36:44 +00:00
|
|
|
query_asset_util: UserPermAssetUtil
|
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
@lazyproperty
|
|
|
|
def query_asset_util(self):
|
2022-12-21 09:36:44 +00:00
|
|
|
return UserPermAssetUtil(self.user)
|
2022-09-08 12:31:57 +00:00
|
|
|
|
2022-12-02 05:15:03 +00:00
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
class UserAllPermedAssetsApi(BaseUserPermedAssetsApi):
|
|
|
|
pagination_class = AllPermedAssetPagination
|
2022-12-02 05:15:03 +00:00
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
def get_assets(self):
|
2022-12-21 09:36:44 +00:00
|
|
|
return self.query_asset_util.get_all_assets()
|
2022-12-02 05:15:03 +00:00
|
|
|
|
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
class UserDirectPermedAssetsApi(BaseUserPermedAssetsApi):
|
|
|
|
def get_assets(self):
|
2022-12-21 09:36:44 +00:00
|
|
|
return self.query_asset_util.get_direct_assets()
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
class UserFavoriteAssetsApi(BaseUserPermedAssetsApi):
|
|
|
|
def get_assets(self):
|
|
|
|
return self.query_asset_util.get_favorite_assets()
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
class UserPermedNodeAssetsApi(BaseUserPermedAssetsApi):
|
|
|
|
pagination_class = NodePermedAssetPagination
|
2022-12-02 05:15:03 +00:00
|
|
|
pagination_node: Node
|
2021-02-05 05:29:29 +00:00
|
|
|
|
2022-12-07 10:38:03 +00:00
|
|
|
def get_assets(self):
|
2021-02-05 05:29:29 +00:00
|
|
|
node_id = self.kwargs.get("node_id")
|
2022-12-07 10:38:03 +00:00
|
|
|
node, assets = self.query_asset_util.get_node_all_assets(node_id)
|
2021-02-05 05:29:29 +00:00
|
|
|
self.pagination_node = node
|
|
|
|
return assets
|