Skip to content

Commit 0b15a32

Browse files
[WEB-5038] fix: cycle creation in external api endpoint (#7866)
* feat: set default owner for cycle creation if not provided * Updated CycleListCreateAPIEndpoint to assign the current user as the owner when the 'owned_by' field is not included in the request data. * Enhanced the CycleCreateSerializer initialization to ensure proper ownership assignment during cycle creation. * feat: add comprehensive tests for Cycle API endpoints * Introduced a new test suite for Cycle API endpoints, covering creation, retrieval, updating, and deletion of cycles. * Implemented tests for various scenarios including successful operations, invalid data handling, and conflict resolution with external IDs. * Enhanced test coverage for listing cycles with different view filters and verifying cycle metrics annotations. * feat: enhance CycleCreateSerializer to include ownership assignment * Added 'owned_by' field to CycleCreateSerializer to specify the user who owns the cycle. * Updated CycleListCreateAPIEndpoint to remove redundant ownership assignment logic, relying on the serializer to handle default ownership. * Ensured that if 'owned_by' is not provided, it defaults to the current user during cycle creation. * fix: correct assertion syntax in CycleListCreateAPIEndpoint tests * Updated the assertion in the test for successful cycle creation to use the correct syntax for checking the response status code. * Ensured that the test accurately verifies the expected behavior of the API endpoint.
1 parent f5eb13f commit 0b15a32

3 files changed

Lines changed: 398 additions & 4 deletions

File tree

apps/api/plane/api/serializers/cycle.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Module imports
66
from .base import BaseSerializer
7-
from plane.db.models import Cycle, CycleIssue
7+
from plane.db.models import Cycle, CycleIssue, User
88
from plane.utils.timezone_converter import convert_to_utc
99

1010

@@ -16,6 +16,13 @@ class CycleCreateSerializer(BaseSerializer):
1616
and UTC normalization for time-bound iteration planning and sprint management.
1717
"""
1818

19+
owned_by = serializers.PrimaryKeyRelatedField(
20+
queryset=User.objects.all(),
21+
required=False,
22+
allow_null=True,
23+
help_text="User who owns the cycle. If not provided, defaults to the current user.",
24+
)
25+
1926
def __init__(self, *args, **kwargs):
2027
super().__init__(*args, **kwargs)
2128
project = self.context.get("project")
@@ -72,6 +79,10 @@ def validate(self, data):
7279
date=str(data.get("end_date", None).date()),
7380
project_id=project_id,
7481
)
82+
83+
if not data.get("owned_by"):
84+
data["owned_by"] = self.context["request"].user
85+
7586
return data
7687

7788

apps/api/plane/api/views/cycle.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ def post(self, request, slug, project_id):
307307
if (request.data.get("start_date", None) is None and request.data.get("end_date", None) is None) or (
308308
request.data.get("start_date", None) is not None and request.data.get("end_date", None) is not None
309309
):
310-
serializer = CycleCreateSerializer(data=request.data)
310+
311+
serializer = CycleCreateSerializer(data=request.data, context={"request": request})
311312
if serializer.is_valid():
312313
if (
313314
request.data.get("external_id")
@@ -332,7 +333,7 @@ def post(self, request, slug, project_id):
332333
},
333334
status=status.HTTP_409_CONFLICT,
334335
)
335-
serializer.save(project_id=project_id, owned_by=request.user)
336+
serializer.save(project_id=project_id)
336337
# Send the model activity
337338
model_activity.delay(
338339
model_name="cycle",
@@ -518,7 +519,7 @@ def patch(self, request, slug, project_id, pk):
518519
status=status.HTTP_400_BAD_REQUEST,
519520
)
520521

521-
serializer = CycleUpdateSerializer(cycle, data=request.data, partial=True)
522+
serializer = CycleUpdateSerializer(cycle, data=request.data, partial=True, context={"request": request})
522523
if serializer.is_valid():
523524
if (
524525
request.data.get("external_id")

0 commit comments

Comments
 (0)