refactor: update cycle and module issue create

This commit is contained in:
pablohashescobar 2023-02-03 19:03:27 +05:30
parent 7e92efee23
commit a4f095fb59
2 changed files with 69 additions and 38 deletions

View File

@ -14,7 +14,6 @@ from plane.db.models import Cycle, CycleIssue, Issue
class CycleViewSet(BaseViewSet): class CycleViewSet(BaseViewSet):
serializer_class = CycleSerializer serializer_class = CycleSerializer
model = Cycle model = Cycle
permission_classes = [ permission_classes = [
@ -41,7 +40,6 @@ class CycleViewSet(BaseViewSet):
class CycleIssueViewSet(BaseViewSet): class CycleIssueViewSet(BaseViewSet):
serializer_class = CycleIssueSerializer serializer_class = CycleIssueSerializer
model = CycleIssue model = CycleIssue
@ -79,7 +77,6 @@ class CycleIssueViewSet(BaseViewSet):
def create(self, request, slug, project_id, cycle_id): def create(self, request, slug, project_id, cycle_id):
try: try:
issues = request.data.get("issues", []) issues = request.data.get("issues", [])
if not len(issues): if not len(issues):
@ -91,29 +88,48 @@ class CycleIssueViewSet(BaseViewSet):
workspace__slug=slug, project_id=project_id, pk=cycle_id workspace__slug=slug, project_id=project_id, pk=cycle_id
) )
issues = Issue.objects.filter( # Get all CycleIssues already created
pk__in=issues, workspace__slug=slug, project_id=project_id cycle_issues = list(CycleIssue.objects.filter(issue_id__in=issues))
)
# Delete old records in order to maintain the database integrity records_to_update = []
CycleIssue.objects.filter(issue_id__in=issues).delete() record_to_create = []
CycleIssue.objects.bulk_create( for issue in issues:
[ cycle_issue = [
cycle_issue
for cycle_issue in cycle_issues
if cycle_issue.issue_id in issues
]
if len(cycle_issue):
cycle_issue[0].cycle_id = cycle_id
records_to_update.append(cycle_issue[0])
else:
record_to_create.append(
CycleIssue( CycleIssue(
project_id=project_id, project_id=project_id,
workspace=cycle.workspace, workspace=cycle.workspace,
created_by=request.user, created_by=request.user,
updated_by=request.user, updated_by=request.user,
cycle=cycle, cycle=cycle,
issue=issue, issue_id=issue,
) )
for issue in issues )
],
CycleIssue.objects.bulk_create(
record_to_create,
batch_size=10, batch_size=10,
ignore_conflicts=True, ignore_conflicts=True,
) )
return Response({"message": "Success"}, status=status.HTTP_200_OK) CycleIssue.objects.bulk_update(
records_to_update,
["cycle"],
batch_size=10,
)
# Return all Cycle Issues
return Response(
CycleIssueSerializer(self.get_queryset(), many=True).data, status=status.HTTP_200_OK
)
except Cycle.DoesNotExist: except Cycle.DoesNotExist:
return Response( return Response(

View File

@ -25,7 +25,6 @@ from plane.db.models import (
class ModuleViewSet(BaseViewSet): class ModuleViewSet(BaseViewSet):
model = Module model = Module
permission_classes = [ permission_classes = [
ProjectEntityPermission, ProjectEntityPermission,
@ -95,7 +94,6 @@ class ModuleViewSet(BaseViewSet):
class ModuleIssueViewSet(BaseViewSet): class ModuleIssueViewSet(BaseViewSet):
serializer_class = ModuleIssueSerializer serializer_class = ModuleIssueSerializer
model = ModuleIssue model = ModuleIssue
@ -148,15 +146,23 @@ class ModuleIssueViewSet(BaseViewSet):
workspace__slug=slug, project_id=project_id, pk=module_id workspace__slug=slug, project_id=project_id, pk=module_id
) )
issues = Issue.objects.filter( module_issues = list(ModuleIssue.objects.filter(issue_id__in=issues))
pk__in=issues, workspace__slug=slug, project_id=project_id
)
# Delete old records in order to maintain the database integrity records_to_update = []
ModuleIssue.objects.filter(issue_id__in=issues).delete() record_to_create = []
ModuleIssue.objects.bulk_create( for issue in issues:
[ module_issue = [
module_issue
for module_issue in module_issues
if module_issue.issue_id in issues
]
if len(module_issue):
module_issue[0].module_id = module_id
records_to_update.append(module_issue[0])
else:
record_to_create.append(
ModuleIssue( ModuleIssue(
module=module, module=module,
issue=issue, issue=issue,
@ -165,11 +171,20 @@ class ModuleIssueViewSet(BaseViewSet):
created_by=request.user, created_by=request.user,
updated_by=request.user, updated_by=request.user,
) )
for issue in issues )
],
ModuleIssue.objects.bulk_create(
record_to_create,
batch_size=10, batch_size=10,
ignore_conflicts=True, ignore_conflicts=True,
) )
ModuleIssue.objects.bulk_update(
records_to_update,
["module"],
batch_size=10,
)
return Response({"message": "Success"}, status=status.HTTP_200_OK) return Response({"message": "Success"}, status=status.HTTP_200_OK)
except Module.DoesNotExist: except Module.DoesNotExist:
return Response( return Response(