From 2209a2c8d24ba7c92888f5c534e6e823eea2a93b Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Mon, 28 Sep 2020 18:49:18 +0800 Subject: [PATCH] =?UTF-8?q?feat(orgs):=20=E4=BF=AE=E6=94=B9OrgMemberRelati?= =?UTF-8?q?onAPI=EF=BC=8C=E6=94=AF=E6=8C=81=E9=80=9A=E8=BF=87=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=8F=82=E6=95=B0=E6=8E=A7=E5=88=B6=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=BF=BD=E7=95=A5=E5=B7=B2=E5=AD=98=E5=9C=A8=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=20(#4720)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(orgs): 修改OrgMemberRelationAPI,支持通过查询参数控制是否忽略已存在的数据 * feat(orgs): 修改构建数据库查询参数的问题 Co-authored-by: Bai --- apps/orgs/api.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/apps/orgs/api.py b/apps/orgs/api.py index d283019d3..ba8b06564 100644 --- a/apps/orgs/api.py +++ b/apps/orgs/api.py @@ -2,9 +2,11 @@ # from django.utils.translation import ugettext as _ +from django.db.models import Q from rest_framework import status, generics from rest_framework.views import Response from rest_framework_bulk import BulkModelViewSet +from rest_framework.mixins import CreateModelMixin from common.permissions import IsSuperUserOrAppUser from common.drf.api import JMSBulkRelationModelViewSet @@ -19,6 +21,8 @@ from perms.models import AssetPermission from orgs.utils import current_org from common.utils import get_logger from .filters import OrgMemberRelationFilterSet +from .models import OrganizationMember + logger = get_logger(__file__) @@ -71,6 +75,57 @@ class OrgMemberRelationBulkViewSet(JMSBulkRelationModelViewSet): serializer_class = OrgMemberSerializer filterset_class = OrgMemberRelationFilterSet + @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) + def perform_bulk_destroy(self, queryset): objs = list(queryset.all().prefetch_related('user', 'org')) queryset.delete()