Skip to content

Commit

Permalink
Merge pull request #236 from makeplane/refactor/cycle_modules
Browse files Browse the repository at this point in the history
refactor: update cycle and module create operation
  • Loading branch information
pablohashescobar authored Feb 6, 2023
2 parents b93abb4 + a576a2a commit c7ad9f3
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 55 deletions.
92 changes: 71 additions & 21 deletions apiserver/plane/api/views/cycle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Python imports
import json

# Django imports
from django.db.models import OuterRef, Func, F
from django.core import serializers

# Third party imports
from rest_framework.response import Response
Expand All @@ -11,10 +15,10 @@
from plane.api.serializers import CycleSerializer, CycleIssueSerializer
from plane.api.permissions import ProjectEntityPermission
from plane.db.models import Cycle, CycleIssue, Issue
from plane.bgtasks.issue_activites_task import issue_activity


class CycleViewSet(BaseViewSet):

serializer_class = CycleSerializer
model = Cycle
permission_classes = [
Expand All @@ -41,7 +45,6 @@ def get_queryset(self):


class CycleIssueViewSet(BaseViewSet):

serializer_class = CycleIssueSerializer
model = CycleIssue

Expand Down Expand Up @@ -79,7 +82,6 @@ def get_queryset(self):

def create(self, request, slug, project_id, cycle_id):
try:

issues = request.data.get("issues", [])

if not len(issues):
Expand All @@ -91,29 +93,77 @@ def create(self, request, slug, project_id, cycle_id):
workspace__slug=slug, project_id=project_id, pk=cycle_id
)

issues = Issue.objects.filter(
pk__in=issues, workspace__slug=slug, project_id=project_id
)

# Delete old records in order to maintain the database integrity
CycleIssue.objects.filter(issue_id__in=issues).delete()
# Get all CycleIssues already created
cycle_issues = list(CycleIssue.objects.filter(issue_id__in=issues))
records_to_update = []
update_cycle_issue_activity = []
record_to_create = []

for issue in issues:
cycle_issue = [
cycle_issue
for cycle_issue in cycle_issues
if str(cycle_issue.issue_id) in issues
]
# Update only when cycle changes
if len(cycle_issue):
if cycle_issue[0].cycle_id != cycle_id:
update_cycle_issue_activity.append(
{
"old_cycle_id": str(cycle_issue[0].cycle_id),
"new_cycle_id": str(cycle_id),
"issue_id": str(cycle_issue[0].issue_id),
}
)
cycle_issue[0].cycle_id = cycle_id
records_to_update.append(cycle_issue[0])
else:
record_to_create.append(
CycleIssue(
project_id=project_id,
workspace=cycle.workspace,
created_by=request.user,
updated_by=request.user,
cycle=cycle,
issue_id=issue,
)
)

CycleIssue.objects.bulk_create(
[
CycleIssue(
project_id=project_id,
workspace=cycle.workspace,
created_by=request.user,
updated_by=request.user,
cycle=cycle,
issue=issue,
)
for issue in issues
],
record_to_create,
batch_size=10,
ignore_conflicts=True,
)
return Response({"message": "Success"}, status=status.HTTP_200_OK)
CycleIssue.objects.bulk_update(
records_to_update,
["cycle"],
batch_size=10,
)

# Capture Issue Activity
issue_activity.delay(
{
"type": "issue.activity",
"requested_data": json.dumps({"cycles_list": issues}),
"actor_id": str(self.request.user.id),
"issue_id": str(self.kwargs.get("pk", None)),
"project_id": str(self.kwargs.get("project_id", None)),
"current_instance": json.dumps(
{
"updated_cycle_issues": update_cycle_issue_activity,
"created_cycle_issues": serializers.serialize(
"json", record_to_create
),
}
),
},
)

# Return all Cycle Issues
return Response(
CycleIssueSerializer(self.get_queryset(), many=True).data,
status=status.HTTP_200_OK,
)

except Cycle.DoesNotExist:
return Response(
Expand Down
91 changes: 71 additions & 20 deletions apiserver/plane/api/views/module.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Python imports
import json

# Django Imports
from django.db import IntegrityError
from django.db.models import Prefetch, F, OuterRef, Func
from django.core import serializers

# Third party imports
from rest_framework.response import Response
Expand All @@ -22,10 +26,10 @@
Issue,
ModuleLink,
)
from plane.bgtasks.issue_activites_task import issue_activity


class ModuleViewSet(BaseViewSet):

model = Module
permission_classes = [
ProjectEntityPermission,
Expand Down Expand Up @@ -95,7 +99,6 @@ def create(self, request, slug, project_id):


class ModuleIssueViewSet(BaseViewSet):

serializer_class = ModuleIssueSerializer
model = ModuleIssue

Expand Down Expand Up @@ -148,29 +151,77 @@ def create(self, request, slug, project_id, module_id):
workspace__slug=slug, project_id=project_id, pk=module_id
)

issues = Issue.objects.filter(
pk__in=issues, workspace__slug=slug, project_id=project_id
)

# Delete old records in order to maintain the database integrity
ModuleIssue.objects.filter(issue_id__in=issues).delete()
module_issues = list(ModuleIssue.objects.filter(issue_id__in=issues))

update_module_issue_activity = []
records_to_update = []
record_to_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):
if module_issue[0].cycle_id != module_id:
update_module_issue_activity.append(
{
"old_module_id": str(module_issue[0].cycle_id),
"new_module_id": str(module_id),
"issue_id": str(module_issue[0].issue_id),
}
)
module_issue[0].module_id = module_id
records_to_update.append(module_issue[0])
else:
record_to_create.append(
ModuleIssue(
module=module,
issue=issue,
project_id=project_id,
workspace=module.workspace,
created_by=request.user,
updated_by=request.user,
)
)

ModuleIssue.objects.bulk_create(
[
ModuleIssue(
module=module,
issue=issue,
project_id=project_id,
workspace=module.workspace,
created_by=request.user,
updated_by=request.user,
)
for issue in issues
],
record_to_create,
batch_size=10,
ignore_conflicts=True,
)
return Response({"message": "Success"}, status=status.HTTP_200_OK)

ModuleIssue.objects.bulk_update(
records_to_update,
["module"],
batch_size=10,
)

# Capture Issue Activity
issue_activity.delay(
{
"type": "issue.activity",
"requested_data": json.dumps({"cycles_list": issues}),
"actor_id": str(self.request.user.id),
"issue_id": str(self.kwargs.get("pk", None)),
"project_id": str(self.kwargs.get("project_id", None)),
"current_instance": json.dumps(
{
"updated_cycle_issues": update_module_issue_activity,
"created_cycle_issues": serializers.serialize(
"json", record_to_create
),
}
),
},
)

return Response(
ModuleIssueSerializer(self.get_queryset(), many=True).data,
status=status.HTTP_200_OK,
)
except Module.DoesNotExist:
return Response(
{"error": "Module Does not exists"}, status=status.HTTP_400_BAD_REQUEST
Expand Down
Loading

0 comments on commit c7ad9f3

Please sign in to comment.