2018-07-25 03:21:12 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2020-07-15 06:35:48 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2020-09-28 10:49:18 +00:00
|
|
|
from django.db.models import Q
|
2020-05-06 07:31:08 +00:00
|
|
|
from rest_framework import status, generics
|
2018-11-02 06:38:44 +00:00
|
|
|
from rest_framework.views import Response
|
|
|
|
from rest_framework_bulk import BulkModelViewSet
|
2020-09-28 10:49:18 +00:00
|
|
|
from rest_framework.mixins import CreateModelMixin
|
2018-07-25 03:21:12 +00:00
|
|
|
|
2018-07-25 07:05:28 +00:00
|
|
|
from common.permissions import IsSuperUserOrAppUser
|
2020-08-03 08:17:46 +00:00
|
|
|
from common.drf.api import JMSBulkRelationModelViewSet
|
2020-07-20 02:42:22 +00:00
|
|
|
from .models import Organization, ROLE
|
2020-08-03 08:17:46 +00:00
|
|
|
from .serializers import (
|
|
|
|
OrgSerializer, OrgReadSerializer,
|
|
|
|
OrgRetrieveSerializer, OrgMemberSerializer
|
|
|
|
)
|
2018-11-02 06:38:44 +00:00
|
|
|
from users.models import User, UserGroup
|
|
|
|
from assets.models import Asset, Domain, AdminUser, SystemUser, Label
|
|
|
|
from perms.models import AssetPermission
|
|
|
|
from orgs.utils import current_org
|
|
|
|
from common.utils import get_logger
|
2020-08-03 08:17:46 +00:00
|
|
|
from .filters import OrgMemberRelationFilterSet
|
2020-09-28 10:49:18 +00:00
|
|
|
from .models import OrganizationMember
|
|
|
|
|
2018-07-25 03:21:12 +00:00
|
|
|
|
2018-11-02 06:38:44 +00:00
|
|
|
logger = get_logger(__file__)
|
2018-07-25 03:21:12 +00:00
|
|
|
|
2018-11-02 06:38:44 +00:00
|
|
|
|
|
|
|
class OrgViewSet(BulkModelViewSet):
|
2020-07-01 06:38:03 +00:00
|
|
|
filter_fields = ('name',)
|
|
|
|
search_fields = ('name', 'comment')
|
2018-07-25 03:21:12 +00:00
|
|
|
queryset = Organization.objects.all()
|
|
|
|
serializer_class = OrgSerializer
|
2018-07-25 07:05:28 +00:00
|
|
|
permission_classes = (IsSuperUserOrAppUser,)
|
2018-11-02 06:38:44 +00:00
|
|
|
org = None
|
|
|
|
|
|
|
|
def get_serializer_class(self):
|
2020-05-21 08:09:42 +00:00
|
|
|
mapper = {
|
|
|
|
'list': OrgReadSerializer,
|
|
|
|
'retrieve': OrgRetrieveSerializer
|
|
|
|
}
|
|
|
|
return mapper.get(self.action, super().get_serializer_class())
|
2018-11-02 06:38:44 +00:00
|
|
|
|
|
|
|
def get_data_from_model(self, model):
|
|
|
|
if model == User:
|
2020-07-20 02:42:22 +00:00
|
|
|
data = model.objects.filter(orgs__id=self.org.id, m2m_org_members__role=ROLE.USER)
|
2018-11-02 06:38:44 +00:00
|
|
|
else:
|
|
|
|
data = model.objects.filter(org_id=self.org.id)
|
|
|
|
return data
|
|
|
|
|
|
|
|
def destroy(self, request, *args, **kwargs):
|
|
|
|
self.org = self.get_object()
|
|
|
|
models = [
|
|
|
|
User, UserGroup,
|
|
|
|
Asset, Domain, AdminUser, SystemUser, Label,
|
|
|
|
AssetPermission,
|
|
|
|
]
|
|
|
|
for model in models:
|
|
|
|
data = self.get_data_from_model(model)
|
|
|
|
if data:
|
2020-07-15 06:35:48 +00:00
|
|
|
msg = _('Organization contains undeleted resources')
|
|
|
|
return Response(data={'error': msg}, status=status.HTTP_403_FORBIDDEN)
|
2018-11-02 06:38:44 +00:00
|
|
|
else:
|
|
|
|
if str(current_org) == str(self.org):
|
2020-07-15 06:35:48 +00:00
|
|
|
msg = _('The current organization cannot be deleted')
|
|
|
|
return Response(data={'error': msg}, status=status.HTTP_403_FORBIDDEN)
|
2018-11-02 06:38:44 +00:00
|
|
|
self.org.delete()
|
|
|
|
return Response({'msg': True}, status=status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
2020-08-03 08:17:46 +00:00
|
|
|
class OrgMemberRelationBulkViewSet(JMSBulkRelationModelViewSet):
|
2020-05-06 07:31:08 +00:00
|
|
|
permission_classes = (IsSuperUserOrAppUser,)
|
2020-08-03 08:17:46 +00:00
|
|
|
m2m_field = Organization.members.field
|
|
|
|
serializer_class = OrgMemberSerializer
|
|
|
|
filterset_class = OrgMemberRelationFilterSet
|
2020-05-06 07:31:08 +00:00
|
|
|
|
2020-09-28 10:49:18 +00:00
|
|
|
@staticmethod
|
|
|
|
def clear_request_data(request):
|
|
|
|
data = request.data
|
|
|
|
|
|
|
|
ignore_already_exist = request.query_params.get('ignore_already_exist')
|
|
|
|
if not ignore_already_exist:
|
|
|
|
return data
|
|
|
|
|
|
|
|
query_params = Q()
|
|
|
|
for _data in data:
|
|
|
|
query_fields = {}
|
|
|
|
org = _data.get('org')
|
|
|
|
if org:
|
|
|
|
query_fields.update({'org': org})
|
|
|
|
user = _data.get('user')
|
|
|
|
if user:
|
|
|
|
query_fields.update({'user': user})
|
|
|
|
role = _data.get('role')
|
|
|
|
if role:
|
|
|
|
query_fields.update({'role': role})
|
|
|
|
query_params |= Q(**query_fields)
|
|
|
|
|
|
|
|
if not query_params:
|
|
|
|
return data
|
|
|
|
|
|
|
|
members = OrganizationMember.objects.filter(query_params)
|
|
|
|
members = [
|
|
|
|
{'org': str(member.org_id), 'user': str(member.user_id), 'role': member.role}
|
|
|
|
for member in members
|
|
|
|
]
|
|
|
|
if not members:
|
|
|
|
return data
|
|
|
|
|
|
|
|
for member in members:
|
|
|
|
if member in data:
|
|
|
|
data.remove(member)
|
|
|
|
return data
|
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
bulk = isinstance(request.data, list)
|
|
|
|
|
|
|
|
if not bulk:
|
|
|
|
return CreateModelMixin.create(self, request, *args, **kwargs)
|
|
|
|
|
|
|
|
else:
|
|
|
|
data = self.clear_request_data(request)
|
|
|
|
serializer = self.get_serializer(data=data, many=True)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
self.perform_bulk_create(serializer)
|
|
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
|
|
|
|
2020-08-03 08:17:46 +00:00
|
|
|
def perform_bulk_destroy(self, queryset):
|
|
|
|
objs = list(queryset.all().prefetch_related('user', 'org'))
|
|
|
|
queryset.delete()
|
|
|
|
self.send_m2m_changed_signal(objs, action='post_remove')
|