jumpserver/apps/assets/api/platform.py

46 lines
1.6 KiB
Python
Raw Normal View History

2022-04-28 14:54:18 +00:00
from rest_framework.decorators import action
from rest_framework.response import Response
2022-04-02 10:35:46 +00:00
2022-04-28 14:54:18 +00:00
from common.drf.api import JMSModelViewSet
from common.drf.serializers import GroupedChoiceSerailizer
2022-04-02 10:35:46 +00:00
from assets.models import Platform
from assets.serializers import PlatformSerializer
2022-04-28 14:54:18 +00:00
from assets.const import AllTypes, Category
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,
'categories': GroupedChoiceSerailizer
}
2022-04-12 09:53:56 +00:00
filterset_fields = ['name']
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-10 11:27:08 +00:00
'type_constraints': 'assets-view_platform'
2022-04-28 14:54:18 +00:00
}
@action(methods=['GET'], detail=False)
def categories(self, request, *args, **kwargs):
data = AllTypes.grouped_choices_to_objs()
serializer = self.get_serializer(data, many=True)
return Response(serializer.data)
2022-04-02 10:35:46 +00:00
2022-08-10 11:27:08 +00:00
@action(methods=['GET'], detail=False, url_path='type-constraints')
def type_constraints(self, request, *args, **kwargs):
2022-05-05 08:18:05 +00:00
category = request.query_params.get('category')
tp = request.query_params.get('type')
2022-08-10 11:27:08 +00:00
limits = AllTypes.get_constraints(category, tp)
2022-05-05 08:18:05 +00:00
return Response(limits)
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)