2018-07-25 03:21:12 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2018-11-02 06:38:44 +00:00
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework.views import Response
|
|
|
|
from rest_framework_bulk import BulkModelViewSet
|
2018-07-25 03:21:12 +00:00
|
|
|
|
2018-07-25 07:05:28 +00:00
|
|
|
from common.permissions import IsSuperUserOrAppUser
|
2018-07-25 03:21:12 +00:00
|
|
|
from .models import Organization
|
2018-11-02 06:38:44 +00:00
|
|
|
from .serializers import OrgSerializer, OrgReadSerializer, \
|
|
|
|
OrgMembershipUserSerializer, OrgMembershipAdminSerializer
|
|
|
|
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
|
|
|
|
from .mixins import OrgMembershipModelViewSetMixin
|
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):
|
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):
|
|
|
|
if self.action in ('list', 'retrieve'):
|
|
|
|
return OrgReadSerializer
|
|
|
|
else:
|
|
|
|
return super().get_serializer_class()
|
|
|
|
|
|
|
|
def get_data_from_model(self, model):
|
|
|
|
if model == User:
|
|
|
|
data = model.objects.filter(orgs__id=self.org.id)
|
|
|
|
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:
|
|
|
|
return Response(status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
else:
|
|
|
|
if str(current_org) == str(self.org):
|
|
|
|
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
|
|
|
self.org.delete()
|
|
|
|
return Response({'msg': True}, status=status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
|
|
class OrgMembershipAdminsViewSet(OrgMembershipModelViewSetMixin, BulkModelViewSet):
|
|
|
|
serializer_class = OrgMembershipAdminSerializer
|
|
|
|
membership_class = Organization.admins.through
|
|
|
|
permission_classes = (IsSuperUserOrAppUser, )
|
|
|
|
|
|
|
|
|
|
|
|
class OrgMembershipUsersViewSet(OrgMembershipModelViewSetMixin, BulkModelViewSet):
|
|
|
|
serializer_class = OrgMembershipUserSerializer
|
|
|
|
membership_class = Organization.users.through
|
|
|
|
permission_classes = (IsSuperUserOrAppUser, )
|
2019-01-15 11:01:33 +00:00
|
|
|
|