コード例 #1
0
    def delete(self, request: Request, organization) -> Response:
        """Remove a Key transaction for a user"""
        if not self.has_feature(organization, request):
            return Response(status=404)

        project = self.get_project(request, organization)

        serializer = serializers.TeamKeyTransactionSerializer(
            data=request.data,
            context={
                "request": request,
                "organization": organization,
            },
        )

        if serializer.is_valid():
            data = serializer.validated_data

            TeamKeyTransaction.objects.filter(
                organization=organization,
                project_team__in=ProjectTeam.objects.filter(
                    project=project, team__in=data["team"]),
                transaction=data["transaction"],
            ).delete()

            return Response(status=204)

        return Response(serializer.errors, status=400)
コード例 #2
0
    def post(self, request: Request, organization) -> Response:
        """Create a Key Transaction"""
        if not self.has_feature(organization, request):
            return Response(status=404)

        project = self.get_project(request, organization)

        with transaction.atomic():
            serializer = serializers.TeamKeyTransactionSerializer(
                data=request.data,
                context={
                    "mode": "create",
                    "request": request,
                    "organization": organization,
                },
            )

            if serializer.is_valid():
                data = serializer.validated_data
                base_filter = {
                    "organization": organization,
                    "transaction": data["transaction"],
                }

                project_teams = ProjectTeam.objects.filter(
                    project=project, team__in=data["team"])
                if len(project_teams) < len(data["team"]):
                    # some teams do not have access to the specified project
                    return Response(
                        {"detail": "Team does not have access to project"},
                        status=400)

                keyed_transaction_team_ids = set(
                    TeamKeyTransaction.objects.values_list(
                        "project_team__team_id",
                        flat=True).filter(**base_filter,
                                          project_team__in=project_teams))
                if len(keyed_transaction_team_ids) == len(data["team"]):
                    # all teams already have the specified transaction marked as key
                    return Response(status=204)

                try:
                    unkeyed_project_teams = project_teams.exclude(
                        team_id__in=keyed_transaction_team_ids)
                    TeamKeyTransaction.objects.bulk_create([
                        TeamKeyTransaction(**base_filter,
                                           project_team=project_team)
                        for project_team in unkeyed_project_teams
                    ])
                    return Response(status=201)
                # Even though we tried to avoid it, the TeamKeyTransaction was created already
                except IntegrityError:
                    return Response(status=409)

        return Response(serializer.errors, status=400)
コード例 #3
0
    def delete(self, request, organization):
        """ Remove a Key transaction for a user """
        if not self.has_feature(request, organization):
            return Response(status=404)

        project = self.get_project(request, organization)

        if not self.has_team_feature(request, organization):
            transaction = request.data["transaction"]
            try:
                model = KeyTransaction.objects.get(
                    transaction=transaction,
                    organization=organization,
                    project=project,
                    owner=request.user,
                )
            except KeyTransaction.DoesNotExist:
                return Response(status=204)

            model.delete()

            return Response(status=204)

        serializer = serializers.TeamKeyTransactionSerializer(
            data=request.data,
            context={
                "request": request,
                "organization": organization,
            },
        )

        if serializer.is_valid():
            data = serializer.validated_data
            base_filter = {
                "organization": organization,
                "project": project,
                "transaction": data["transaction"],
            }

            TeamKeyTransaction.objects.filter(**base_filter,
                                              team__in=data["team"]).delete()

            return Response(status=204)

        return Response(serializer.errors, status=400)
コード例 #4
0
    def post(self, request, organization):
        """ Create a Key Transaction """
        if not self.has_feature(request, organization):
            return Response(status=404)

        project = self.get_project(request, organization)

        if not self.has_team_feature(request, organization):
            base_filter = {"organization": organization, "owner": request.user}

            with transaction.atomic():
                serializer = serializers.KeyTransactionSerializer(
                    data=request.data, context=base_filter)
                if serializer.is_valid():
                    data = serializer.validated_data
                    base_filter["transaction"] = data["transaction"]
                    base_filter["project"] = project

                    if KeyTransaction.objects.filter(**base_filter).exists():
                        return Response(status=204)

                    try:
                        KeyTransaction.objects.create(**base_filter)
                        return Response(status=201)
                    # Even though we tried to avoid it, this KeyTransaction was created already
                    except IntegrityError:
                        return Response(status=204)
                return Response(serializer.errors, status=400)

        with transaction.atomic():
            serializer = serializers.TeamKeyTransactionSerializer(
                data=request.data,
                context={
                    "mode": "create",
                    "request": request,
                    "organization": organization,
                },
            )

            if serializer.is_valid():
                data = serializer.validated_data
                base_filter = {
                    "organization": organization,
                    "project": project,
                    "transaction": data["transaction"],
                }

                keyed_transaction_team_ids = set(
                    TeamKeyTransaction.objects.values_list(
                        "team_id", flat=True).filter(**base_filter,
                                                     team__in=data["team"]))
                if len(keyed_transaction_team_ids) == len(data["team"]):
                    return Response(status=204)

                try:
                    # TeamKeyTransaction.objects.create(**base_filter)
                    TeamKeyTransaction.objects.bulk_create([
                        TeamKeyTransaction(**base_filter, team=team)
                        for team in data["team"]
                        if team.id not in keyed_transaction_team_ids
                    ])
                    return Response(status=201)
                # Even though we tried to avoid it, the TeamKeyTransaction was created already
                except IntegrityError:
                    return Response(status=409)

        return Response(serializer.errors, status=400)