2016-08-09 09:27:37 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
#
|
|
|
|
|
2016-10-19 11:30:55 +00:00
|
|
|
from rest_framework.views import APIView, Response
|
2016-12-28 16:29:59 +00:00
|
|
|
from rest_framework.decorators import api_view
|
2016-11-15 11:33:04 +00:00
|
|
|
from rest_framework.generics import ListAPIView, get_object_or_404
|
2016-11-09 16:18:57 +00:00
|
|
|
from rest_framework import viewsets
|
2016-12-19 17:19:50 +00:00
|
|
|
from users.permissions import IsValidUser, IsSuperUser
|
2016-11-10 08:59:50 +00:00
|
|
|
from common.utils import get_object_or_none
|
2016-11-15 11:33:04 +00:00
|
|
|
from .utils import get_user_granted_assets, get_user_granted_asset_groups, get_user_asset_permissions, \
|
2016-11-15 15:09:36 +00:00
|
|
|
get_user_group_asset_permissions, get_user_group_granted_assets, get_user_group_granted_asset_groups
|
2016-11-04 10:33:16 +00:00
|
|
|
from .models import AssetPermission
|
2016-11-15 11:33:04 +00:00
|
|
|
from .hands import AssetGrantedSerializer, User, UserGroup, AssetGroup, Asset, AssetGroup, AssetGroupSerializer
|
2016-11-04 10:33:16 +00:00
|
|
|
from . import serializers
|
|
|
|
|
|
|
|
|
2016-11-09 16:18:57 +00:00
|
|
|
class AssetPermissionViewSet(viewsets.ModelViewSet):
|
2016-11-04 10:33:16 +00:00
|
|
|
queryset = AssetPermission.objects.all()
|
|
|
|
serializer_class = serializers.AssetPermissionSerializer
|
|
|
|
permission_classes = (IsSuperUser,)
|
2016-10-19 11:30:55 +00:00
|
|
|
|
2016-11-09 16:18:57 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super(AssetPermissionViewSet, self).get_queryset()
|
|
|
|
user_id = self.request.query_params.get('user', '')
|
2016-11-16 09:38:03 +00:00
|
|
|
user_group_id = self.request.query_params.get('user_group', '')
|
2016-11-15 11:33:04 +00:00
|
|
|
|
2016-11-10 08:59:50 +00:00
|
|
|
if user_id and user_id.isdigit():
|
2016-11-15 11:33:04 +00:00
|
|
|
user = get_object_or_404(User, id=int(user_id))
|
|
|
|
queryset = get_user_asset_permissions(user)
|
|
|
|
|
|
|
|
if user_group_id:
|
|
|
|
user_group = get_object_or_404(UserGroup, id=user_group_id)
|
|
|
|
queryset = get_user_group_asset_permissions(user_group)
|
2016-11-09 16:18:57 +00:00
|
|
|
return queryset
|
|
|
|
|
2016-11-10 08:59:50 +00:00
|
|
|
def get_serializer_class(self):
|
|
|
|
if getattr(self, 'user_id', ''):
|
|
|
|
return serializers.UserAssetPermissionSerializer
|
|
|
|
return serializers.AssetPermissionSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class RevokeUserAssetPermission(APIView):
|
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
|
|
|
|
def put(self, request, *args, **kwargs):
|
|
|
|
permission_id = str(request.data.get('id', ''))
|
|
|
|
user_id = str(request.data.get('user_id', ''))
|
|
|
|
|
|
|
|
if permission_id and user_id and permission_id.isdigit() and user_id.isdigit():
|
2016-11-15 11:33:04 +00:00
|
|
|
asset_permission = get_object_or_404(AssetPermission, id=int(permission_id))
|
|
|
|
user = get_object_or_404(User, id=int(user_id))
|
2016-11-10 08:59:50 +00:00
|
|
|
|
|
|
|
if asset_permission and user:
|
|
|
|
asset_permission.users.remove(user)
|
|
|
|
return Response({'msg': 'success'})
|
|
|
|
return Response({'msg': 'failed'}, status=404)
|
|
|
|
|
2016-10-19 11:30:55 +00:00
|
|
|
|
2016-11-15 11:33:04 +00:00
|
|
|
class RevokeUserGroupAssetPermission(APIView):
|
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
|
|
|
|
def put(self, request, *args, **kwargs):
|
|
|
|
permission_id = str(request.data.get('id', ''))
|
|
|
|
user_group_id = str(request.data.get('user_group_id', ''))
|
|
|
|
|
|
|
|
if permission_id and user_group_id and permission_id.isdigit() and user_group_id.isdigit():
|
|
|
|
asset_permission = get_object_or_404(AssetPermission, id=int(permission_id))
|
|
|
|
user_group = get_object_or_404(UserGroup, id=int(user_group_id))
|
|
|
|
|
|
|
|
if asset_permission and user_group:
|
|
|
|
asset_permission.user_groups.remove(user_group)
|
|
|
|
return Response({'msg': 'success'})
|
|
|
|
return Response({'msg': 'failed'}, status=404)
|
|
|
|
|
|
|
|
|
|
|
|
class UserGrantedAssetsApi(ListAPIView):
|
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
serializer_class = AssetGrantedSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
user_id = self.kwargs.get('pk', '')
|
|
|
|
|
|
|
|
if user_id:
|
|
|
|
user = get_object_or_404(User, id=user_id)
|
|
|
|
queryset = get_user_granted_assets(user)
|
|
|
|
else:
|
|
|
|
queryset = []
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class UserGrantedAssetGroupsApi(ListAPIView):
|
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
serializer_class = AssetGroupSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
user_id = self.kwargs.get('pk', '')
|
|
|
|
|
|
|
|
if user_id:
|
|
|
|
user = get_object_or_404(User, id=user_id)
|
|
|
|
queryset = get_user_granted_asset_groups(user)
|
|
|
|
else:
|
|
|
|
queryset = []
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class MyGrantedAssetsApi(ListAPIView):
|
2016-10-19 11:30:55 +00:00
|
|
|
permission_classes = (IsValidUser,)
|
2016-11-10 14:06:23 +00:00
|
|
|
serializer_class = AssetGrantedSerializer
|
2016-10-19 11:30:55 +00:00
|
|
|
|
2016-11-10 14:06:23 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
user = self.request.user
|
2016-10-19 11:30:55 +00:00
|
|
|
if user:
|
2016-11-10 14:06:23 +00:00
|
|
|
queryset = get_user_granted_assets(user)
|
2016-11-15 11:33:04 +00:00
|
|
|
else:
|
|
|
|
queryset = []
|
|
|
|
return queryset
|
2016-10-19 11:30:55 +00:00
|
|
|
|
2016-11-04 10:33:16 +00:00
|
|
|
|
2016-11-15 11:33:04 +00:00
|
|
|
class MyGrantedAssetsGroupsApi(APIView):
|
2016-11-04 10:33:16 +00:00
|
|
|
permission_classes = (IsValidUser,)
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
asset_groups = {}
|
|
|
|
user = request.user
|
|
|
|
|
|
|
|
if user:
|
|
|
|
assets = get_user_granted_assets(user)
|
|
|
|
for asset in assets:
|
|
|
|
for asset_group in asset.groups.all():
|
|
|
|
if asset_group.id in asset_groups:
|
2016-12-28 16:29:59 +00:00
|
|
|
asset_groups[asset_group.id]['assets_amount'] += 1
|
2016-11-04 10:33:16 +00:00
|
|
|
else:
|
|
|
|
asset_groups[asset_group.id] = {
|
|
|
|
'id': asset_group.id,
|
|
|
|
'name': asset_group.name,
|
2016-11-05 15:34:45 +00:00
|
|
|
'comment': asset_group.comment,
|
2016-11-15 11:44:08 +00:00
|
|
|
'assets_amount': 1
|
2016-11-04 10:33:16 +00:00
|
|
|
}
|
|
|
|
asset_groups_json = asset_groups.values()
|
2016-11-05 15:34:45 +00:00
|
|
|
return Response(asset_groups_json, status=200)
|
|
|
|
|
|
|
|
|
2016-11-15 11:33:04 +00:00
|
|
|
class MyAssetGroupAssetsApi(ListAPIView):
|
2016-11-05 15:34:45 +00:00
|
|
|
permission_classes = (IsValidUser,)
|
2016-11-10 14:06:23 +00:00
|
|
|
serializer_class = AssetGrantedSerializer
|
2016-11-05 15:34:45 +00:00
|
|
|
|
2016-11-10 14:06:23 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
queryset = []
|
|
|
|
asset_group_id = self.kwargs.get('pk', -1)
|
|
|
|
user = self.request.user
|
|
|
|
asset_group = get_object_or_none(AssetGroup, id=asset_group_id)
|
2016-11-05 15:34:45 +00:00
|
|
|
|
2016-11-10 14:06:23 +00:00
|
|
|
if user and asset_group:
|
2016-11-05 15:34:45 +00:00
|
|
|
assets = get_user_granted_assets(user)
|
2016-11-10 14:06:23 +00:00
|
|
|
for asset in assets:
|
|
|
|
if asset_group in asset.groups.all():
|
|
|
|
queryset.append(asset)
|
|
|
|
return queryset
|
2016-11-15 15:09:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserGroupGrantedAssetsApi(ListAPIView):
|
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
serializer_class = AssetGrantedSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
user_group_id = self.kwargs.get('pk', '')
|
|
|
|
|
|
|
|
if user_group_id:
|
2016-11-16 09:38:03 +00:00
|
|
|
user_group = get_object_or_404(UserGroup, id=user_group_id)
|
2016-11-15 15:09:36 +00:00
|
|
|
queryset = get_user_group_granted_assets(user_group)
|
|
|
|
else:
|
|
|
|
queryset = []
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class UserGroupGrantedAssetGroupsApi(ListAPIView):
|
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
serializer_class = AssetGroupSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
user_group_id = self.kwargs.get('pk', '')
|
|
|
|
|
|
|
|
if user_group_id:
|
2016-11-16 09:38:03 +00:00
|
|
|
user_group = get_object_or_404(UserGroup, id=user_group_id)
|
2016-11-15 15:09:36 +00:00
|
|
|
queryset = get_user_group_granted_asset_groups(user_group)
|
|
|
|
else:
|
|
|
|
queryset = []
|
|
|
|
return queryset
|