ФУНКЦИИ - МЕЖПРОЕКТНАЯ КОММУНИКАЦИЯ: отправка во внешний контур и source-side список
This commit is contained in:
@@ -53,6 +53,10 @@ from .intake import (
|
||||
IntakeIssueCreateSerializer,
|
||||
IntakeIssueUpdateSerializer,
|
||||
)
|
||||
from .external_contours import (
|
||||
ExternalContourRequestCreateSerializer,
|
||||
ExternalContourRequestSerializer,
|
||||
)
|
||||
from .estimate import EstimateSerializer, EstimatePointSerializer
|
||||
from .asset import (
|
||||
UserAssetUploadSerializer,
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
# Copyright (c) 2023-present Plane Software, Inc. and contributors
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
# See the LICENSE file for details.
|
||||
|
||||
from rest_framework import serializers
|
||||
|
||||
from .base import BaseSerializer
|
||||
from .issue import IssueSerializer
|
||||
from .project import ProjectLiteSerializer
|
||||
from .state import StateLiteSerializer
|
||||
from .user import UserLiteSerializer
|
||||
from plane.db.models import IntakeIssue, Issue
|
||||
|
||||
|
||||
class ExternalContourIssuePayloadSerializer(serializers.Serializer):
|
||||
name = serializers.CharField(max_length=255)
|
||||
description_html = serializers.CharField(required=False, allow_blank=True, allow_null=True)
|
||||
priority = serializers.ChoiceField(choices=Issue.PRIORITY_CHOICES, default="none", required=False)
|
||||
assignee_ids = serializers.ListField(child=serializers.UUIDField(), required=False)
|
||||
label_ids = serializers.ListField(child=serializers.UUIDField(), required=False)
|
||||
target_date = serializers.DateField(required=False, allow_null=True)
|
||||
|
||||
|
||||
class ExternalContourRequestCreateSerializer(serializers.Serializer):
|
||||
target_project_id = serializers.UUIDField()
|
||||
issue = ExternalContourIssuePayloadSerializer()
|
||||
|
||||
|
||||
class ExternalContourIssueSerializer(BaseSerializer):
|
||||
assignee_ids = serializers.SerializerMethodField()
|
||||
assignee_details = serializers.SerializerMethodField()
|
||||
created_by_detail = UserLiteSerializer(source="created_by", read_only=True)
|
||||
label_details = serializers.SerializerMethodField()
|
||||
label_ids = serializers.SerializerMethodField()
|
||||
project_detail = ProjectLiteSerializer(source="project", read_only=True)
|
||||
state_detail = StateLiteSerializer(source="state", read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Issue
|
||||
fields = [
|
||||
"id",
|
||||
"name",
|
||||
"description_html",
|
||||
"priority",
|
||||
"sequence_id",
|
||||
"project_id",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"created_by",
|
||||
"state_id",
|
||||
"target_date",
|
||||
"label_ids",
|
||||
"label_details",
|
||||
"assignee_ids",
|
||||
"assignee_details",
|
||||
"state_detail",
|
||||
"project_detail",
|
||||
"created_by_detail",
|
||||
]
|
||||
read_only_fields = fields
|
||||
|
||||
def get_assignee_ids(self, obj):
|
||||
return [assignee.assignee_id for assignee in obj.issue_assignee.all()]
|
||||
|
||||
def get_assignee_details(self, obj):
|
||||
return UserLiteSerializer([assignee.assignee for assignee in obj.issue_assignee.all()], many=True).data
|
||||
|
||||
def get_label_ids(self, obj):
|
||||
return [label.label_id for label in obj.label_issue.all()]
|
||||
|
||||
def get_label_details(self, obj):
|
||||
return [
|
||||
{"id": str(label_bridge.label.id), "name": label_bridge.label.name, "color": label_bridge.label.color}
|
||||
for label_bridge in obj.label_issue.all()
|
||||
]
|
||||
|
||||
|
||||
class ExternalContourRequestSerializer(BaseSerializer):
|
||||
issue = ExternalContourIssueSerializer(read_only=True)
|
||||
source_project_id = serializers.SerializerMethodField()
|
||||
status = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = IntakeIssue
|
||||
fields = [
|
||||
"id",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"created_by",
|
||||
"issue",
|
||||
"source_project_id",
|
||||
"status",
|
||||
]
|
||||
read_only_fields = fields
|
||||
|
||||
def get_source_project_id(self, obj):
|
||||
return obj.extra.get("source_project_id")
|
||||
|
||||
def get_status(self, obj):
|
||||
issue = obj.issue
|
||||
if issue and issue.state and issue.state.group in ["completed", "cancelled"]:
|
||||
return "closed"
|
||||
return "open"
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
from .asset import urlpatterns as asset_patterns
|
||||
from .cycle import urlpatterns as cycle_patterns
|
||||
from .external_contours import urlpatterns as external_contour_patterns
|
||||
from .intake import urlpatterns as intake_patterns
|
||||
from .label import urlpatterns as label_patterns
|
||||
from .member import urlpatterns as member_patterns
|
||||
@@ -18,6 +19,7 @@ from .sticky import urlpatterns as sticky_patterns
|
||||
urlpatterns = [
|
||||
*asset_patterns,
|
||||
*cycle_patterns,
|
||||
*external_contour_patterns,
|
||||
*intake_patterns,
|
||||
*label_patterns,
|
||||
*member_patterns,
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
# Copyright (c) 2023-present Plane Software, Inc. and contributors
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
# See the LICENSE file for details.
|
||||
|
||||
from django.urls import path
|
||||
|
||||
from plane.api.views import ExternalContourDetailAPIEndpoint, ExternalContourListCreateAPIEndpoint
|
||||
|
||||
urlpatterns = [
|
||||
path(
|
||||
"workspaces/<str:slug>/projects/<uuid:project_id>/external-contours/",
|
||||
ExternalContourListCreateAPIEndpoint.as_view(http_method_names=["get", "post"]),
|
||||
name="external-contours",
|
||||
),
|
||||
path(
|
||||
"workspaces/<str:slug>/projects/<uuid:project_id>/external-contours/<uuid:request_id>/",
|
||||
ExternalContourDetailAPIEndpoint.as_view(http_method_names=["get"]),
|
||||
name="external-contour-detail",
|
||||
),
|
||||
]
|
||||
@@ -55,6 +55,10 @@ from .intake import (
|
||||
IntakeIssueListCreateAPIEndpoint,
|
||||
IntakeIssueDetailAPIEndpoint,
|
||||
)
|
||||
from .external_contours import (
|
||||
ExternalContourListCreateAPIEndpoint,
|
||||
ExternalContourDetailAPIEndpoint,
|
||||
)
|
||||
|
||||
from .asset import UserAssetEndpoint, UserServerAssetEndpoint, GenericAssetEndpoint
|
||||
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
# Copyright (c) 2023-present Plane Software, Inc. and contributors
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
# See the LICENSE file for details.
|
||||
|
||||
from django.shortcuts import get_object_or_404
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
|
||||
from plane.api.serializers import (
|
||||
ExternalContourRequestCreateSerializer,
|
||||
ExternalContourRequestSerializer,
|
||||
)
|
||||
from plane.app.permissions import ProjectLitePermission
|
||||
from plane.db.models import Intake, IntakeIssue, Project, State, StateGroup
|
||||
from plane.api.serializers.issue import IssueSerializer as IssueCreateSerializer
|
||||
from .base import BaseAPIView
|
||||
from plane.db.models.intake import IntakeIssueStatus, SourceType
|
||||
|
||||
|
||||
class ExternalContourListCreateAPIEndpoint(BaseAPIView):
|
||||
permission_classes = [ProjectLitePermission]
|
||||
serializer_class = ExternalContourRequestSerializer
|
||||
|
||||
def get_source_project(self, slug, project_id):
|
||||
return get_object_or_404(Project, workspace__slug=slug, pk=project_id)
|
||||
|
||||
def get_queryset(self):
|
||||
return (
|
||||
IntakeIssue.objects.filter(
|
||||
workspace__slug=self.kwargs.get("slug"),
|
||||
extra__bridge="external-contours",
|
||||
extra__source_project_id=str(self.kwargs.get("project_id")),
|
||||
)
|
||||
.select_related(
|
||||
"issue",
|
||||
"issue__state",
|
||||
"issue__project",
|
||||
"issue__created_by",
|
||||
)
|
||||
.prefetch_related("issue__issue_assignee__assignee", "issue__label_issue__label")
|
||||
.order_by("-updated_at")
|
||||
)
|
||||
|
||||
def get(self, request, slug, project_id):
|
||||
serializer = ExternalContourRequestSerializer(self.get_queryset(), many=True)
|
||||
return Response(
|
||||
{
|
||||
"results": serializer.data,
|
||||
"next_cursor": "",
|
||||
"prev_cursor": "",
|
||||
"next_page_results": False,
|
||||
"prev_page_results": False,
|
||||
"total_count": len(serializer.data),
|
||||
"count": len(serializer.data),
|
||||
"total_pages": 1,
|
||||
"extra_stats": None,
|
||||
"total_results": len(serializer.data),
|
||||
},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
def post(self, request, slug, project_id):
|
||||
source_project = self.get_source_project(slug, project_id)
|
||||
serializer = ExternalContourRequestCreateSerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
|
||||
target_project = get_object_or_404(
|
||||
Project,
|
||||
workspace_id=source_project.workspace_id,
|
||||
pk=serializer.validated_data["target_project_id"],
|
||||
archived_at__isnull=True,
|
||||
)
|
||||
|
||||
if str(target_project.id) == str(source_project.id):
|
||||
return Response({"error": "Target project must differ from source project"}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
triage_state = State.triage_objects.filter(project=target_project).first()
|
||||
if not triage_state:
|
||||
triage_state = State.objects.create(
|
||||
name="Triage",
|
||||
group=StateGroup.TRIAGE.value,
|
||||
project=target_project,
|
||||
color="#4E5355",
|
||||
sequence=65000,
|
||||
default=False,
|
||||
)
|
||||
|
||||
target_default_state = (
|
||||
State.objects.filter(project=target_project, default=True)
|
||||
.exclude(group=StateGroup.TRIAGE.value)
|
||||
.first()
|
||||
) or State.objects.filter(project=target_project).exclude(group=StateGroup.TRIAGE.value).order_by("sequence", "created_at").first()
|
||||
|
||||
if not target_default_state:
|
||||
return Response({"error": "Target project has no available workflow state"}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
intake = Intake.objects.filter(project=target_project, name="External Contours Bridge").first()
|
||||
if not intake:
|
||||
intake = Intake.objects.create(
|
||||
name="External Contours Bridge",
|
||||
description="System bridge intake used for cross-project routing.",
|
||||
is_default=False,
|
||||
project=target_project,
|
||||
)
|
||||
|
||||
issue_payload = serializer.validated_data["issue"]
|
||||
issue_serializer = IssueCreateSerializer(
|
||||
data={
|
||||
"name": issue_payload["name"],
|
||||
"description_html": issue_payload.get("description_html") or "<p></p>",
|
||||
"priority": issue_payload.get("priority", "none"),
|
||||
"assignees": issue_payload.get("assignee_ids", []),
|
||||
"labels": issue_payload.get("label_ids", []),
|
||||
"target_date": issue_payload.get("target_date"),
|
||||
"state_id": str(triage_state.id),
|
||||
},
|
||||
context={
|
||||
"project_id": str(target_project.id),
|
||||
"workspace_id": str(target_project.workspace_id),
|
||||
"default_assignee_id": target_project.default_assignee_id,
|
||||
},
|
||||
)
|
||||
issue_serializer.is_valid(raise_exception=True)
|
||||
issue = issue_serializer.save(state=triage_state)
|
||||
|
||||
intake_issue = IntakeIssue.objects.create(
|
||||
intake=intake,
|
||||
project=target_project,
|
||||
issue=issue,
|
||||
source=SourceType.IN_APP,
|
||||
status=IntakeIssueStatus.ACCEPTED.value,
|
||||
extra={
|
||||
"bridge": "external-contours",
|
||||
"source_project_id": str(source_project.id),
|
||||
"source_project_name": source_project.name,
|
||||
"target_project_id": str(target_project.id),
|
||||
"target_project_name": target_project.name,
|
||||
"requested_by_id": str(request.user.id),
|
||||
"requested_by_name": request.user.display_name,
|
||||
"requested_at": issue.created_at.isoformat() if issue.created_at else None,
|
||||
},
|
||||
)
|
||||
|
||||
if issue.state_id != target_default_state.id:
|
||||
issue.state = target_default_state
|
||||
issue.save()
|
||||
|
||||
response_serializer = ExternalContourRequestSerializer(
|
||||
IntakeIssue.objects.select_related(
|
||||
"issue",
|
||||
"issue__state",
|
||||
"issue__project",
|
||||
"issue__created_by",
|
||||
)
|
||||
.prefetch_related("issue__issue_assignee__assignee", "issue__label_issue__label")
|
||||
.get(pk=intake_issue.id)
|
||||
)
|
||||
return Response(response_serializer.data, status=status.HTTP_201_CREATED)
|
||||
|
||||
|
||||
class ExternalContourDetailAPIEndpoint(BaseAPIView):
|
||||
permission_classes = [ProjectLitePermission]
|
||||
serializer_class = ExternalContourRequestSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
return (
|
||||
IntakeIssue.objects.filter(
|
||||
workspace__slug=self.kwargs.get("slug"),
|
||||
extra__bridge="external-contours",
|
||||
extra__source_project_id=str(self.kwargs.get("project_id")),
|
||||
pk=self.kwargs.get("request_id"),
|
||||
)
|
||||
.select_related(
|
||||
"issue",
|
||||
"issue__state",
|
||||
"issue__project",
|
||||
"issue__created_by",
|
||||
)
|
||||
.prefetch_related("issue__issue_assignee__assignee", "issue__label_issue__label")
|
||||
)
|
||||
|
||||
def get(self, request, slug, project_id, request_id):
|
||||
contour_request = get_object_or_404(self.get_queryset())
|
||||
serializer = ExternalContourRequestSerializer(contour_request)
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
Reference in New Issue
Block a user