Skip to content

Commit

Permalink
Create a different serializer for project update/create
Browse files Browse the repository at this point in the history
  • Loading branch information
gagantrivedi committed Nov 20, 2024
1 parent 1ddfcf1 commit d8570bb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
16 changes: 8 additions & 8 deletions api/projects/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ class Meta:
"edge_v2_migration_status",
)

def update(self, instance: Project, validated_data: dict) -> Project:
# Prevent updates to `organisation` field
validated_data.pop("organisation", None)
return super().update(instance, validated_data)

def get_migration_status(self, obj: Project) -> str:
if not settings.PROJECT_METADATA_TABLE_NAME_DYNAMO:
migration_status = ProjectIdentityMigrationStatus.NOT_APPLICABLE.value
Expand All @@ -74,9 +69,7 @@ def get_use_edge_identities(self, obj: Project) -> bool:
)


class ProjectUpdateOrCreateSerializer(
ReadOnlyIfNotValidPlanMixin, ProjectListSerializer
):
class ProjectCreateSerializer(ReadOnlyIfNotValidPlanMixin, ProjectListSerializer):
invalid_plans_regex = r"^(free|startup.*|scale-up.*)$"
field_names = ("stale_flags_limit_days", "enable_realtime_updates")

Expand All @@ -98,6 +91,13 @@ def get_subscription(self) -> typing.Optional[Subscription]:
return None


class ProjectUpdateSerializer(ProjectCreateSerializer):
class Meta(ProjectCreateSerializer.Meta):
read_only_fields = ProjectCreateSerializer.Meta.read_only_fields + (
"organisation",
)


class ProjectRetrieveSerializer(ProjectListSerializer):
total_features = serializers.SerializerMethodField()
total_segments = serializers.SerializerMethodField()
Expand Down
15 changes: 9 additions & 6 deletions api/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@
CreateUpdateUserProjectPermissionSerializer,
ListUserPermissionGroupProjectPermissionSerializer,
ListUserProjectPermissionSerializer,
ProjectCreateSerializer,
ProjectListSerializer,
ProjectRetrieveSerializer,
ProjectUpdateOrCreateSerializer,
ProjectUpdateSerializer,
)


Expand Down Expand Up @@ -75,11 +76,13 @@ class ProjectViewSet(viewsets.ModelViewSet):
permission_classes = [ProjectPermissions]

def get_serializer_class(self):
if self.action == "retrieve":
return ProjectRetrieveSerializer
elif self.action in ("create", "update", "partial_update"):
return ProjectUpdateOrCreateSerializer
return ProjectListSerializer
serializers = {
"retrieve": ProjectRetrieveSerializer,
"create": ProjectCreateSerializer,
"update": ProjectUpdateSerializer,
"partial_update": ProjectUpdateSerializer,
}
return serializers.get(self.action, ProjectListSerializer)

pagination_class = None

Expand Down
6 changes: 4 additions & 2 deletions api/tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ def organisation_with_persist_trait_data_disabled(organisation):


@pytest.fixture()
def dynamo_enabled_project(admin_client, organisation):
def dynamo_enabled_project(
admin_client: APIClient, organisation: Organisation, settings: SettingsWrapper
):
settings.EDGE_ENABLED = True
project_data = {
"name": "Test Project",
"organisation": organisation,
"enable_dynamo_db": True,
}
url = reverse("api-v1:projects:project-list")
response = admin_client.post(url, data=project_data)
Expand Down

0 comments on commit d8570bb

Please sign in to comment.