From f97685c788673020efaf550e4712c3fb56b28680 Mon Sep 17 00:00:00 2001 From: ibuler Date: Sat, 10 Oct 2020 18:33:43 +0800 Subject: [PATCH] =?UTF-8?q?perf(orgs):=20=E4=BC=98=E5=8C=96=E7=BB=84?= =?UTF-8?q?=E7=BB=87=E7=94=A8=E6=88=B7=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/orgs/api.py | 54 +++------------------------------------- apps/orgs/filters.py | 2 +- apps/orgs/serializers.py | 5 ++++ 3 files changed, 10 insertions(+), 51 deletions(-) diff --git a/apps/orgs/api.py b/apps/orgs/api.py index ba8b06564..e7c83fa36 100644 --- a/apps/orgs/api.py +++ b/apps/orgs/api.py @@ -75,56 +75,10 @@ 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_create(self, serializer): + data = serializer.validated_data + relations = [OrganizationMember(**i) for i in data] + OrganizationMember.objects.bulk_create(relations, ignore_conflicts=True) def perform_bulk_destroy(self, queryset): objs = list(queryset.all().prefetch_related('user', 'org')) diff --git a/apps/orgs/filters.py b/apps/orgs/filters.py index df68e468f..c6c026198 100644 --- a/apps/orgs/filters.py +++ b/apps/orgs/filters.py @@ -13,4 +13,4 @@ class OrgMemberRelationFilterSet(filterset.FilterSet): class Meta: model = OrganizationMember - fields = ('org_id', 'user_id', 'role', 'id') + fields = ('org_id', 'user_id', 'org', 'user', 'role', 'id') diff --git a/apps/orgs/serializers.py b/apps/orgs/serializers.py index 21b695996..2a732a03f 100644 --- a/apps/orgs/serializers.py +++ b/apps/orgs/serializers.py @@ -60,6 +60,11 @@ class OrgMemberSerializer(BulkModelSerializer): model = OrganizationMember fields = ('id', 'org', 'user', 'role', 'org_display', 'user_display', 'role_display') + def get_unique_together_validators(self): + if self.parent: + return [] + return super().get_unique_together_validators() + @classmethod def setup_eager_loading(cls, queryset): return queryset.annotate(