Skip to content

Commit 4e9710b

Browse files
committed
Compromise on perform_create
1 parent 9d669cf commit 4e9710b

File tree

3 files changed

+10
-20
lines changed

3 files changed

+10
-20
lines changed

api/segments/serializers.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from functools import cached_property
21
from typing import Any
32

43
import structlog
@@ -114,30 +113,19 @@ class Meta:
114113
"rules",
115114
"metadata",
116115
]
117-
read_only_fields = ["project"]
118-
119-
@cached_property
120-
def project(self) -> Project:
121-
"""Resolve the project from the view's URL kwargs"""
122-
if self.instance:
123-
return self.instance.project
124-
try:
125-
pk = int(self.context["view"].kwargs["project_pk"])
126-
except KeyError as error:
127-
raise RuntimeError(
128-
"The serializer context is missing a view with project_pk in its URL."
129-
) from error
130-
return Project.objects.get(pk=pk) # type: ignore[no-any-return]
131116

132117
def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
133118
attrs = super().validate(attrs)
134-
organisation = self.project.organisation
119+
project = self.instance.project if self.instance else Project.objects.get(
120+
pk=int(self.context["view"].kwargs["project_pk"]),
121+
)
122+
organisation = project.organisation
135123

136124
self._validate_required_metadata(
137-
organisation, attrs.get("metadata", []), project=self.project
125+
organisation, attrs.get("metadata", []), project=project
138126
)
139127
self._validate_segment_rules_conditions_limit(attrs["rules"])
140-
self._validate_project_segment_limit(self.project)
128+
self._validate_project_segment_limit(project)
141129
return attrs
142130

143131
def create(self, validated_data: dict[str, Any]): # type: ignore[no-untyped-def]

api/segments/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def get_queryset(self): # type: ignore[no-untyped-def]
136136
return queryset
137137

138138
def perform_create(self, serializer: SegmentSerializer) -> None: # type: ignore[override]
139-
serializer.save(project_id=self.kwargs["project_pk"]) # type: ignore[no-untyped-call]
139+
serializer.save(project_id=int(self.kwargs["project_pk"])) # type: ignore[no-untyped-call]
140140

141141
@extend_schema(parameters=[AssociatedFeaturesQuerySerializer])
142142
@action(

api/tests/unit/segments/test_unit_segments_views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1895,7 +1895,7 @@ def test_create_segment__body_project_differs_from_url__does_not_create_in_other
18951895
other_project = Project.objects.create(name="Other Project", organisation=other_org)
18961896

18971897
# When
1898-
admin_client.post(
1898+
response = admin_client.post(
18991899
f"/api/v1/projects/{project.id}/segments/",
19001900
data={
19011901
"name": "a_wild_pokemon",
@@ -1906,4 +1906,6 @@ def test_create_segment__body_project_differs_from_url__does_not_create_in_other
19061906
)
19071907

19081908
# Then
1909+
assert response.status_code == status.HTTP_201_CREATED
1910+
assert response.json()["project"] == project.id
19091911
assert not Segment.objects.filter(project=other_project).exists()

0 commit comments

Comments
 (0)