1- from typing import TYPE_CHECKING , Any
1+ from typing import Any
22
33from rest_framework import serializers
44
@@ -117,7 +117,7 @@ class Meta(EnvironmentSerializerWithMetadata.Meta):
117117 )
118118
119119
120- class CreateUpdateEnvironmentSerializer (
120+ class _BaseCreateUpdateEnvironmentSerializer (
121121 ReadOnlyIfNotValidPlanMixin , EnvironmentSerializerWithMetadata
122122):
123123 invalid_plans = ("free" ,)
@@ -132,30 +132,32 @@ class Meta(EnvironmentSerializerWithMetadata.Meta):
132132 )
133133 ]
134134
135+
136+ class CreateEnvironmentSerializer (_BaseCreateUpdateEnvironmentSerializer ):
135137 def get_subscription (self ) -> Subscription | None :
136138 view = self .context ["view" ]
139+ if getattr (view , "swagger_fake_view" , False ):
140+ return None
141+
142+ project_id = view .request .data ["project" ]
143+ project = Project .objects .select_related (
144+ "organisation" , "organisation__subscription"
145+ ).get (id = project_id )
146+
147+ return getattr (project .organisation , "subscription" , None )
137148
138- if view .action == "create" :
139- # handle `project` not being part of the data
140- # When request comes from drf-spectacular (as part of schema generation)
141- project_id = view .request .data .get ("project" )
142- if not project_id :
143- return None
144-
145- project = Project .objects .select_related (
146- "organisation" , "organisation__subscription"
147- ).get (id = project_id )
148-
149- return getattr (project .organisation , "subscription" , None )
150- elif view .action in ("update" , "partial_update" ):
151- # Handle schema generation when instance is None.
152- if self .instance is None :
153- return None
154- if TYPE_CHECKING :
155- assert isinstance (self .instance , Environment )
156- return getattr (self .instance .project .organisation , "subscription" , None )
157-
158- return None
149+
150+ class UpdateEnvironmentSerializer (_BaseCreateUpdateEnvironmentSerializer ):
151+ class Meta (_BaseCreateUpdateEnvironmentSerializer .Meta ):
152+ read_only_fields = EnvironmentSerializerLight .Meta .read_only_fields + ( # type: ignore[assignment]
153+ "project" ,
154+ )
155+
156+ def get_subscription (self ) -> Subscription | None :
157+ view = self .context ["view" ]
158+ if getattr (view , "swagger_fake_view" , False ):
159+ return None
160+ return getattr (self .instance .project .organisation , "subscription" , None ) # type: ignore[union-attr]
159161
160162
161163class CloneEnvironmentSerializer (EnvironmentSerializerLight ):
0 commit comments