Merge pull request #67 from pablohashescobar/fix/cycle_module_issue

fix: make cycle issue and module issue responses consistent
This commit is contained in:
Vamsi Kurama 2022-12-22 23:29:59 +05:30 committed by GitHub
commit 24fcdd821d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 120 additions and 16 deletions

View File

@ -21,7 +21,7 @@ class CycleSerializer(BaseSerializer):
class CycleIssueSerializer(BaseSerializer):
issue_details = IssueStateSerializer(read_only=True, source="issue")
issue_detail = IssueStateSerializer(read_only=True, source="issue")
class Meta:
model = CycleIssue

View File

@ -22,6 +22,8 @@ from plane.db.models import (
IssueBlocker,
CycleIssue,
Cycle,
Module,
ModuleIssue,
)
@ -75,6 +77,11 @@ class IssueCreateSerializer(BaseSerializer):
write_only=True,
required=False,
)
blocks_list = serializers.ListField(
child=serializers.PrimaryKeyRelatedField(queryset=Issue.objects.all()),
write_only=True,
required=False,
)
class Meta:
model = Issue
@ -92,6 +99,7 @@ class IssueCreateSerializer(BaseSerializer):
blockers = validated_data.pop("blockers_list", None)
assignees = validated_data.pop("assignees_list", None)
labels = validated_data.pop("labels_list", None)
blocks = validated_data.pop("blocks_list", None)
project = self.context["project"]
issue = Issue.objects.create(**validated_data, project=project)
@ -144,6 +152,22 @@ class IssueCreateSerializer(BaseSerializer):
batch_size=10,
)
if blocks is not None:
IssueBlocker.objects.bulk_create(
[
IssueBlocker(
block=block,
blocked_by=issue,
project=project,
workspace=project.workspace,
created_by=issue.created_by,
updated_by=issue.updated_by,
)
for block in blocks
],
batch_size=10,
)
return issue
def update(self, instance, validated_data):
@ -151,6 +175,7 @@ class IssueCreateSerializer(BaseSerializer):
blockers = validated_data.pop("blockers_list", None)
assignees = validated_data.pop("assignees_list", None)
labels = validated_data.pop("labels_list", None)
blocks = validated_data.pop("blocks_list", None)
if blockers is not None:
IssueBlocker.objects.filter(block=instance).delete()
@ -203,6 +228,23 @@ class IssueCreateSerializer(BaseSerializer):
batch_size=10,
)
if blocks is not None:
IssueBlocker.objects.filter(blocked_by=instance).delete()
IssueBlocker.objects.bulk_create(
[
IssueBlocker(
block=block,
blocked_by=instance,
project=instance.project,
workspace=instance.project.workspace,
created_by=instance.created_by,
updated_by=instance.updated_by,
)
for block in blocks
],
batch_size=10,
)
return super().update(instance, validated_data)
@ -342,6 +384,37 @@ class IssueCycleDetailSerializer(BaseSerializer):
]
class ModuleBaseSerializer(BaseSerializer):
class Meta:
model = Module
fields = "__all__"
read_only_fields = [
"workspace",
"project",
"created_by",
"updated_by",
"created_at",
"updated_at",
]
class IssueModuleDetailSerializer(BaseSerializer):
module_detail = ModuleBaseSerializer(read_only=True, source="module")
class Meta:
model = ModuleIssue
fields = "__all__"
read_only_fields = [
"workspace",
"project",
"created_by",
"updated_by",
"created_at",
"updated_at",
]
class IssueSerializer(BaseSerializer):
project_detail = ProjectSerializer(read_only=True, source="project")
state_detail = StateSerializer(read_only=True, source="state")
@ -351,6 +424,7 @@ class IssueSerializer(BaseSerializer):
blocked_issues = BlockedIssueSerializer(read_only=True, many=True)
blocker_issues = BlockerIssueSerializer(read_only=True, many=True)
issue_cycle = IssueCycleDetailSerializer(read_only=True)
issue_module = IssueModuleDetailSerializer(read_only=True, many=True)
class Meta:
model = Issue

View File

@ -5,7 +5,7 @@ from rest_framework import serializers
from .base import BaseSerializer
from .user import UserLiteSerializer
from .project import ProjectSerializer
from .issue import IssueFlatSerializer
from .issue import IssueStateSerializer
from plane.db.models import User, Module, ModuleMember, ModuleIssue
@ -52,6 +52,7 @@ class ModuleWriteSerializer(BaseSerializer):
for member in members
],
batch_size=10,
ignore_conflicts=True,
)
return module
@ -59,25 +60,22 @@ class ModuleWriteSerializer(BaseSerializer):
def update(self, instance, validated_data):
members = validated_data.pop("members_list", None)
project = self.context["project"]
module = Module.objects.create(**validated_data, project=project)
if members is not None:
ModuleIssue.objects.filter(module=instance).delete()
ModuleMember.objects.bulk_create(
[
ModuleMember(
module=module,
module=instance,
member=member,
project=project,
workspace=project.workspace,
created_by=module.created_by,
updated_by=module.updated_by,
project=instance.project,
workspace=instance.project.workspace,
created_by=instance.created_by,
updated_by=instance.updated_by,
)
for member in members
],
batch_size=10,
ignore_conflicts=True
)
return super().update(instance, validated_data)
@ -100,7 +98,7 @@ class ModuleFlatSerializer(BaseSerializer):
class ModuleIssueSerializer(BaseSerializer):
module_detail = ModuleFlatSerializer(read_only=True, source="module")
issue_detail = IssueFlatSerializer(read_only=True, source="issue")
issue_detail = IssueStateSerializer(read_only=True, source="issue")
class Meta:
model = ModuleIssue

View File

@ -37,6 +37,7 @@ from plane.db.models import (
Label,
IssueBlocker,
CycleIssue,
ModuleIssue
)
@ -96,6 +97,12 @@ class IssueViewSet(BaseViewSet):
queryset=CycleIssue.objects.select_related("cycle", "issue"),
),
)
.prefetch_related(
Prefetch(
"issue_module",
queryset=ModuleIssue.objects.select_related("module", "issue"),
),
)
)
def grouper(self, issue, group_by):
@ -175,6 +182,7 @@ class UserWorkSpaceIssues(BaseAPIView):
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
class WorkSpaceIssuesEndpoint(BaseAPIView):
permission_classes = [

View File

@ -44,7 +44,7 @@ class ModuleViewSet(BaseViewSet):
.prefetch_related("members")
.prefetch_related(
Prefetch(
"module_issues",
"issue_module",
queryset=ModuleIssue.objects.select_related("module", "issue"),
)
)

View File

@ -0,0 +1,24 @@
# Generated by Django 3.2.14 on 2022-12-22 09:06
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('db', '0013_projectmember_default_props'),
]
operations = [
migrations.AlterField(
model_name='moduleissue',
name='issue',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='issue_module', to='db.issue'),
),
migrations.AlterField(
model_name='moduleissue',
name='module',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='issue_module', to='db.module'),
),
]

View File

@ -71,10 +71,10 @@ class ModuleMember(ProjectBaseModel):
class ModuleIssue(ProjectBaseModel):
module = models.ForeignKey(
"db.Module", on_delete=models.CASCADE, related_name="module_issues"
"db.Module", on_delete=models.CASCADE, related_name="issue_module"
)
issue = models.ForeignKey(
"db.Issue", on_delete=models.CASCADE, related_name="module_issues"
"db.Issue", on_delete=models.CASCADE, related_name="issue_module"
)
class Meta: