jumpserver/apps/assets/api/platform.py

42 lines
1.4 KiB
Python
Raw Normal View History

from jumpserver.utils import has_valid_xpack_license
2022-04-28 14:54:18 +00:00
from common.drf.api import JMSModelViewSet
2022-09-13 02:41:49 +00:00
from common.drf.serializers import GroupedChoiceSerializer
2022-04-02 10:35:46 +00:00
from assets.models import Platform
from assets.const import AllTypes
2022-09-29 12:44:45 +00:00
from assets.serializers import PlatformSerializer
2022-04-02 10:35:46 +00:00
__all__ = ['AssetPlatformViewSet']
2022-04-28 14:54:18 +00:00
class AssetPlatformViewSet(JMSModelViewSet):
2022-04-02 10:35:46 +00:00
queryset = Platform.objects.all()
2022-04-28 14:54:18 +00:00
serializer_classes = {
'default': PlatformSerializer,
2022-09-13 02:41:49 +00:00
'categories': GroupedChoiceSerializer
2022-04-28 14:54:18 +00:00
}
2022-08-22 05:25:57 +00:00
filterset_fields = ['name', 'category', 'type']
2022-04-02 10:35:46 +00:00
search_fields = ['name']
2022-04-28 14:54:18 +00:00
rbac_perms = {
2022-05-05 08:18:05 +00:00
'categories': 'assets.view_platform',
2022-08-29 07:50:25 +00:00
'type_constraints': 'assets.view_platform',
'ops_methods': 'assets.view_platform'
2022-04-28 14:54:18 +00:00
}
def get_queryset(self):
queryset = super().get_queryset()
queryset = queryset.filter(type__in=AllTypes.get_types())
return queryset
2022-10-27 10:34:25 +00:00
def get_object(self):
pk = self.kwargs.get('pk', '')
if pk.isnumeric():
return super().get_object()
return self.get_queryset().get(name=pk)
2022-04-02 10:35:46 +00:00
def check_object_permissions(self, request, obj):
if request.method.lower() in ['delete', 'put', 'patch'] and obj.internal:
self.permission_denied(
request, message={"detail": "Internal platform"}
)
return super().check_object_permissions(request, obj)