mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
* dev: initialize inbox * dev: inbox and inbox issues models, views and serializers * dev: issue object filter for inbox * dev: filter for search issues * dev: inbox snooze and duplicates * dev: set duplicate to null by default * feat: inbox ui and services * feat: project detail in inbox * style: layout, popover, icons, sidebar * dev: default inbox for project and pending issues count * dev: fix exception when creating default inbox * fix: empty state for inbox * dev: auto issue state updation when rejected or marked duplicate * fix: inbox update status * fix: hydrating chose with old values filters workflow * feat: inbox issue filtering * fix: issue inbox filtering * feat: filter inbox issues * refactor: analytics, border colors * dev: filters and views for inbox * dev: source for inboxissue and update list inbox issue * dev: update list endpoint to house filters and additional data * dev: bridge id for list * dev: remove print logs * dev: update inbox issue workflow * dev: add description_html in issue details * fix: inbox track event auth, chore: inbox issue action authorization * fix: removed unnecessary api calls * style: viewed issues * fix: priority validation * dev: remove print logs * dev: update issue inbox update workflow * chore: added inbox view context * fix: type errors * fix: build errors and warnings * dev: update issue inbox workflow and log all the changes * fix: filters logic, sidebar fields to show * dev: update issue filtering status * chore: update create inbox issue modal, fix: mutation issues * dev: update issue accept workflow * chore: add comment to inbox issues * chore: remove inboxIssueId from url after deleting * dev: update the issue triage workflow * fix: mutation after issue status change * chore: issue details sidebar divider * fix: issue activity for inbox issues * dev: update inbox perrmissions * dev: create new permission layer * chore: auth layer for inbox * chore: show accepting status * chore: show issue status at the top of issue details --------- Co-authored-by: Dakshesh Jain <dakshesh.jain14@gmail.com> Co-authored-by: gurusainath <gurusainath007@gmail.com> Co-authored-by: Aaryan Khandelwal <aaryankhandu123@gmail.com>
350 lines
12 KiB
Python
350 lines
12 KiB
Python
# Python imports
|
|
import json
|
|
|
|
# Django import
|
|
from django.utils import timezone
|
|
from django.db.models import Q, Count, OuterRef, Func, F, Prefetch
|
|
from django.core.serializers.json import DjangoJSONEncoder
|
|
|
|
# Third party imports
|
|
from rest_framework import status
|
|
from rest_framework.response import Response
|
|
from sentry_sdk import capture_exception
|
|
|
|
# Module imports
|
|
from .base import BaseViewSet
|
|
from plane.api.permissions import ProjectBasePermission, ProjectLitePermission
|
|
from plane.db.models import (
|
|
Project,
|
|
Inbox,
|
|
InboxIssue,
|
|
Issue,
|
|
State,
|
|
IssueLink,
|
|
IssueAttachment,
|
|
IssueActivity,
|
|
)
|
|
from plane.api.serializers import (
|
|
IssueSerializer,
|
|
InboxSerializer,
|
|
InboxIssueSerializer,
|
|
IssueCreateSerializer,
|
|
IssueStateInboxSerializer,
|
|
)
|
|
from plane.utils.issue_filters import issue_filters
|
|
from plane.bgtasks.issue_activites_task import issue_activity
|
|
|
|
|
|
class InboxViewSet(BaseViewSet):
|
|
permission_classes = [
|
|
ProjectBasePermission,
|
|
]
|
|
|
|
serializer_class = InboxSerializer
|
|
model = Inbox
|
|
|
|
def get_queryset(self):
|
|
return (
|
|
super()
|
|
.get_queryset()
|
|
.filter(
|
|
workspace__slug=self.kwargs.get("slug"),
|
|
project_id=self.kwargs.get("project_id"),
|
|
)
|
|
.annotate(
|
|
pending_issue_count=Count(
|
|
"issue_inbox",
|
|
filter=Q(issue_inbox__status=-2),
|
|
)
|
|
)
|
|
.select_related("workspace", "project")
|
|
)
|
|
|
|
def perform_create(self, serializer):
|
|
serializer.save(project_id=self.kwargs.get("project_id"))
|
|
|
|
def destroy(self, request, slug, project_id, pk):
|
|
try:
|
|
inbox = Inbox.objects.get(
|
|
workspace__slug=slug, project_id=project_id, pk=pk
|
|
)
|
|
|
|
if inbox.is_default:
|
|
return Response(
|
|
{"error": "You cannot delete the default inbox"},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
|
|
inbox.delete()
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
|
except Exception as e:
|
|
capture_exception(e)
|
|
return Response(
|
|
{"error": "Something went wronf please try again later"},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
|
|
|
|
class InboxIssueViewSet(BaseViewSet):
|
|
permission_classes = [
|
|
ProjectLitePermission,
|
|
]
|
|
|
|
serializer_class = InboxIssueSerializer
|
|
model = InboxIssue
|
|
|
|
filterset_fields = [
|
|
"status",
|
|
]
|
|
|
|
def get_queryset(self):
|
|
return self.filter_queryset(
|
|
super()
|
|
.get_queryset()
|
|
.filter(
|
|
Q(snoozed_till__gte=timezone.now()) | Q(snoozed_till__isnull=True),
|
|
workspace__slug=self.kwargs.get("slug"),
|
|
project_id=self.kwargs.get("project_id"),
|
|
inbox_id=self.kwargs.get("inbox_id"),
|
|
)
|
|
.select_related("issue", "workspace", "project")
|
|
)
|
|
|
|
def list(self, request, slug, project_id, inbox_id):
|
|
try:
|
|
order_by = request.GET.get("order_by", "created_at")
|
|
filters = issue_filters(request.query_params, "GET")
|
|
issues = (
|
|
Issue.objects.filter(
|
|
issue_inbox__inbox_id=inbox_id,
|
|
workspace__slug=slug,
|
|
project_id=project_id,
|
|
)
|
|
.annotate(
|
|
sub_issues_count=Issue.issue_objects.filter(parent=OuterRef("id"))
|
|
.order_by()
|
|
.annotate(count=Func(F("id"), function="Count"))
|
|
.values("count")
|
|
)
|
|
.annotate(bridge_id=F("issue_inbox__id"))
|
|
.filter(project_id=project_id)
|
|
.filter(workspace__slug=slug)
|
|
.select_related("project")
|
|
.select_related("workspace")
|
|
.select_related("state")
|
|
.select_related("parent")
|
|
.prefetch_related("assignees")
|
|
.prefetch_related("labels")
|
|
.order_by(order_by)
|
|
.filter(**filters)
|
|
.annotate(
|
|
link_count=IssueLink.objects.filter(issue=OuterRef("id"))
|
|
.order_by()
|
|
.annotate(count=Func(F("id"), function="Count"))
|
|
.values("count")
|
|
)
|
|
.annotate(
|
|
attachment_count=IssueAttachment.objects.filter(
|
|
issue=OuterRef("id")
|
|
)
|
|
.order_by()
|
|
.annotate(count=Func(F("id"), function="Count"))
|
|
.values("count")
|
|
)
|
|
.prefetch_related(
|
|
Prefetch(
|
|
"issue_inbox",
|
|
queryset=InboxIssue.objects.only(
|
|
"status", "duplicate_to", "snoozed_till", "source"
|
|
),
|
|
)
|
|
)
|
|
)
|
|
issues_data = IssueStateInboxSerializer(issues, many=True).data
|
|
return Response(
|
|
issues_data,
|
|
status=status.HTTP_200_OK,
|
|
)
|
|
|
|
except Exception as e:
|
|
capture_exception(e)
|
|
return Response(
|
|
{"error": "Something went wrong please try again later"},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
|
|
def create(self, request, slug, project_id, inbox_id):
|
|
try:
|
|
if not request.data.get("issue", {}).get("name", False):
|
|
return Response(
|
|
{"error": "Name is required"}, status=status.HTTP_400_BAD_REQUEST
|
|
)
|
|
|
|
if not request.data.get("issue", {}).get("priority", "low") in [
|
|
"low",
|
|
"medium",
|
|
"high",
|
|
"urgent",
|
|
None,
|
|
]:
|
|
return Response(
|
|
{"error": "Invalid priority"}, status=status.HTTP_400_BAD_REQUEST
|
|
)
|
|
|
|
# Create or get state
|
|
state, _ = State.objects.get_or_create(
|
|
name="Triage",
|
|
group="backlog",
|
|
description="Default state for managing all Inbox Issues",
|
|
project_id=project_id,
|
|
color="#ff7700",
|
|
)
|
|
|
|
# create an issue
|
|
issue = Issue.objects.create(
|
|
name=request.data.get("issue", {}).get("name"),
|
|
description=request.data.get("issue", {}).get("description", {}),
|
|
description_html=request.data.get("issue", {}).get(
|
|
"description_html", "<p></p>"
|
|
),
|
|
priority=request.data.get("issue", {}).get("priority", "low"),
|
|
project_id=project_id,
|
|
state=state,
|
|
)
|
|
|
|
# Create an Issue Activity
|
|
# Track the issue
|
|
issue_activity.delay(
|
|
type="issue.activity.created",
|
|
requested_data=json.dumps(request.data, cls=DjangoJSONEncoder),
|
|
actor_id=str(request.user.id),
|
|
issue_id=str(issue.id),
|
|
project_id=str(project_id),
|
|
current_instance=None,
|
|
)
|
|
# create an inbox issue
|
|
InboxIssue.objects.create(
|
|
inbox_id=inbox_id,
|
|
project_id=project_id,
|
|
issue=issue,
|
|
source=request.data.get("source", "in-app"),
|
|
)
|
|
|
|
serializer = IssueStateInboxSerializer(issue)
|
|
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
|
|
|
except Exception as e:
|
|
capture_exception(e)
|
|
return Response(
|
|
{"error": "Something went wrong please try again later"},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
|
|
def partial_update(self, request, slug, project_id, inbox_id, pk):
|
|
try:
|
|
inbox_issue = InboxIssue.objects.get(
|
|
pk=pk, workspace__slug=slug, project_id=project_id, inbox_id=inbox_id
|
|
)
|
|
|
|
issue_data = request.data.pop("issue", False)
|
|
|
|
if bool(issue_data):
|
|
issue = Issue.objects.get(
|
|
pk=inbox_issue.issue_id, workspace__slug=slug, project_id=project_id
|
|
)
|
|
issue_serializer = IssueCreateSerializer(
|
|
issue, data=issue_data, partial=True
|
|
)
|
|
|
|
if issue_serializer.is_valid():
|
|
current_instance = issue
|
|
issue_serializer.save()
|
|
# Log all the updates
|
|
requested_data = json.dumps(issue_data, cls=DjangoJSONEncoder)
|
|
if issue is not None:
|
|
issue_activity.delay(
|
|
type="issue.activity.updated",
|
|
requested_data=requested_data,
|
|
actor_id=str(request.user.id),
|
|
issue_id=str(issue.id),
|
|
project_id=str(project_id),
|
|
current_instance=json.dumps(
|
|
IssueSerializer(current_instance).data,
|
|
cls=DjangoJSONEncoder,
|
|
),
|
|
)
|
|
else:
|
|
return Response(
|
|
issue_serializer.errors, status=status.HTTP_400_BAD_REQUEST
|
|
)
|
|
|
|
serializer = InboxIssueSerializer(
|
|
inbox_issue, data=request.data, partial=True
|
|
)
|
|
|
|
if serializer.is_valid():
|
|
serializer.save()
|
|
# Update the issue state if the issue is rejected or marked as duplicate
|
|
if serializer.data["status"] in [-1, 2]:
|
|
issue = Issue.objects.get(
|
|
pk=inbox_issue.issue_id,
|
|
workspace__slug=slug,
|
|
project_id=project_id,
|
|
)
|
|
state = State.objects.filter(
|
|
group="cancelled", workspace__slug=slug, project_id=project_id
|
|
).first()
|
|
if state is not None:
|
|
issue.state = state
|
|
issue.save()
|
|
|
|
# Update the issue state if it is accepted
|
|
if serializer.data["status"] in [1]:
|
|
issue = Issue.objects.get(
|
|
pk=inbox_issue.issue_id,
|
|
workspace__slug=slug,
|
|
project_id=project_id,
|
|
)
|
|
|
|
# Update the issue state only if it is in triage state
|
|
if issue.state.name == "Triage":
|
|
# Move to default state
|
|
state = State.objects.filter(
|
|
workspace__slug=slug, project_id=project_id, default=True
|
|
).first()
|
|
if state is not None:
|
|
issue.state = state
|
|
issue.save()
|
|
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
except InboxIssue.DoesNotExist:
|
|
return Response(
|
|
{"error": "Inbox Issue does not exist"},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
except Exception as e:
|
|
capture_exception(e)
|
|
return Response(
|
|
{"error": "Something went wrong please try again later"},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
|
|
def retrieve(self, request, slug, project_id, inbox_id, pk):
|
|
try:
|
|
inbox_issue = InboxIssue.objects.get(
|
|
pk=pk, workspace__slug=slug, project_id=project_id, inbox_id=inbox_id
|
|
)
|
|
issue = Issue.objects.get(
|
|
pk=inbox_issue.issue_id, workspace__slug=slug, project_id=project_id
|
|
)
|
|
serializer = IssueStateInboxSerializer(issue)
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
|
except Exception as e:
|
|
capture_exception(e)
|
|
return Response(
|
|
{"error": "Something went wrong please try again later"},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|