2022-12-07 10:38:03 +00:00
|
|
|
import abc
|
2023-02-20 05:31:56 +00:00
|
|
|
|
2023-10-26 11:19:09 +00:00
|
|
|
from rest_framework.generics import ListAPIView, RetrieveAPIView
|
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
|
2023-03-27 06:37:04 +00:00
|
|
|
from common.utils import get_logger, lazyproperty, is_uuid
|
2023-10-26 11:19:09 +00:00
|
|
|
from orgs.utils import tmp_to_root_org
|
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
|
2023-10-26 11:19:09 +00:00
|
|
|
from perms.utils import UserPermAssetUtil, PermAssetDetailUtil
|
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',
|
2023-10-26 11:19:09 +00:00
|
|
|
'UserPermedAssetRetrieveApi',
|
2022-12-02 05:15:03 +00:00
|
|
|
]
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
2023-10-26 11:19:09 +00:00
|
|
|
class UserPermedAssetRetrieveApi(SelfOrPKUserMixin, RetrieveAPIView):
|
|
|
|
serializer_class = serializers.AssetPermedDetailSerializer
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
with tmp_to_root_org():
|
|
|
|
asset_id = self.kwargs.get('pk')
|
|
|
|
util = PermAssetDetailUtil(self.user, asset_id)
|
|
|
|
asset = util.asset
|
|
|
|
asset.permed_accounts = util.get_permed_accounts_for_user()
|
|
|
|
asset.permed_protocols = util.get_permed_protocols_for_user()
|
|
|
|
return asset
|
|
|
|
|
|
|
|
|
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
|
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()
|
2023-02-23 09:44:38 +00:00
|
|
|
assets = self.serializer_class.setup_eager_loading(assets)
|
2022-12-02 05:15:03 +00:00
|
|
|
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):
|
2023-03-27 06:37:04 +00:00
|
|
|
node_id = self.request.query_params.get('node_id')
|
|
|
|
if is_uuid(node_id):
|
|
|
|
__, assets = self.query_asset_util.get_node_all_assets(node_id)
|
|
|
|
else:
|
|
|
|
assets = self.query_asset_util.get_all_assets()
|
|
|
|
return 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
|