From b0565024bd03130672287d27bd865f88f6ff0f7b Mon Sep 17 00:00:00 2001 From: George Silva <863039+george-silva@users.noreply.github.com> Date: Wed, 14 Aug 2024 11:08:20 -0300 Subject: [PATCH] removes all uuid from the API & filters (#1676) --- src/planscape/datasets/serializers.py | 7 +++---- src/planscape/datasets/tests/test_serializers.py | 8 ++++---- src/planscape/goals/filters.py | 2 -- src/planscape/goals/serializers.py | 10 +++++----- src/planscape/goals/tests/test_serializers.py | 6 +++--- src/planscape/goals/tests/test_views.py | 4 ++-- src/planscape/impacts/serializers.py | 12 +++--------- src/planscape/impacts/tests/test_views.py | 2 +- src/planscape/metrics/filters.py | 3 --- src/planscape/metrics/serializers.py | 9 ++++----- src/planscape/metrics/tests/test_serializers.py | 10 ++++------ src/planscape/metrics/tests/test_views.py | 6 +++--- src/planscape/metrics/views.py | 1 - src/planscape/planning/serializers.py | 3 --- src/planscape/projects/serializers.py | 2 +- src/planscape/projects/tests/test_views.py | 4 ++-- src/planscape/projects/views.py | 1 - 17 files changed, 35 insertions(+), 55 deletions(-) diff --git a/src/planscape/datasets/serializers.py b/src/planscape/datasets/serializers.py index 322fde529..d1a8dbfff 100644 --- a/src/planscape/datasets/serializers.py +++ b/src/planscape/datasets/serializers.py @@ -6,15 +6,14 @@ class DatasetSerializer(serializers.ModelSerializer): - organization = UUIDRelatedField( - uuid_field="uuid", + organization = serializers.PrimaryKeyRelatedField( queryset=Organization.objects.all(), ) class Meta: model = Dataset fields = ( - "uuid", + "id", "organization", "name", "type", @@ -27,7 +26,7 @@ class DatasetDetailSerializer(DatasetSerializer): class Meta: model = Dataset fields = ( - "uuid", + "id", "created_at", "created_by", "updated_at", diff --git a/src/planscape/datasets/tests/test_serializers.py b/src/planscape/datasets/tests/test_serializers.py index 4ab566af7..05f3f7f66 100644 --- a/src/planscape/datasets/tests/test_serializers.py +++ b/src/planscape/datasets/tests/test_serializers.py @@ -27,8 +27,8 @@ def setUp(self): def test_dataset_serializer(self): serializer = DatasetSerializer(instance=self.dataset) data = serializer.data - self.assertEqual(data["uuid"], str(self.dataset.uuid)) - self.assertEqual(data["organization"], str(self.dataset.organization.uuid)) + self.assertEqual(data["id"], self.dataset.id) + self.assertEqual(data["organization"], self.dataset.organization.id) self.assertEqual(data["name"], self.dataset.name) self.assertEqual(data["type"], self.dataset.type) self.assertEqual(data["blob_status"], self.dataset.blob_status) @@ -37,8 +37,8 @@ def test_dataset_serializer(self): def test_dataset_detail_serializer(self): serializer = DatasetDetailSerializer(instance=self.dataset) data = serializer.data - self.assertEqual(data["uuid"], str(self.dataset.uuid)) - self.assertEqual(data["organization"], str(self.dataset.organization.uuid)) + self.assertEqual(data["id"], self.dataset.id) + self.assertEqual(data["organization"], self.dataset.organization.id) self.assertEqual(data["name"], self.dataset.name) self.assertEqual(data["type"], self.dataset.type) self.assertEqual(data["blob_status"], self.dataset.blob_status) diff --git a/src/planscape/goals/filters.py b/src/planscape/goals/filters.py index eacafa8de..a793e527e 100644 --- a/src/planscape/goals/filters.py +++ b/src/planscape/goals/filters.py @@ -9,13 +9,11 @@ class TreatmentGoalFilterSet(filters.FilterSet): organization = filters.ModelChoiceFilter( queryset=Organization.objects.all(), field_name="project__organization", - to_field_name="uuid", ) project = filters.ModelChoiceFilter( queryset=Project.objects.all(), field_name="project", - to_field_name="uuid", ) class Meta: diff --git a/src/planscape/goals/serializers.py b/src/planscape/goals/serializers.py index a37867eaf..6cfb47cf1 100644 --- a/src/planscape/goals/serializers.py +++ b/src/planscape/goals/serializers.py @@ -43,7 +43,7 @@ class Meta: class MetricUsageSerializer(serializers.ModelSerializer): metric = MetricGoalSerializer() - dataset = DatasetGoalSerializer(source="metric.dataset") + dataset = DatasetGoalSerializer(source="metric.dataset", read_only=True) class Meta: model = MetricUsage @@ -59,8 +59,8 @@ class Meta: class TreatmentGoalSerializer(serializers.ModelSerializer): - organization = serializers.UUIDField( - source="project.organization.uuid", read_only=True + organization = serializers.PrimaryKeyRelatedField( + source="project.organization", read_only=True ) metric_usages = MetricUsageSerializer( @@ -68,12 +68,12 @@ class TreatmentGoalSerializer(serializers.ModelSerializer): read_only=True, ) - project = serializers.UUIDField(source="project.uuid", read_only=True) + project = serializers.PrimaryKeyRelatedField(read_only=True) class Meta: model = TreatmentGoal fields = ( - "uuid", + "id", "created_at", "created_by", "updated_at", diff --git a/src/planscape/goals/tests/test_serializers.py b/src/planscape/goals/tests/test_serializers.py index ecae9d15b..7d9df689e 100644 --- a/src/planscape/goals/tests/test_serializers.py +++ b/src/planscape/goals/tests/test_serializers.py @@ -83,11 +83,11 @@ def test_treatment_goal_serializer(self): """Test the TreatmentGoalSerializer data""" serializer = TreatmentGoalSerializer(instance=self.treatment_goal) data = serializer.data - self.assertEqual(data["uuid"], str(self.treatment_goal.uuid)) + self.assertEqual(data["id"], self.treatment_goal.id) self.assertEqual(data["created_by"], self.user.id) self.assertEqual(data["deleted_at"], self.treatment_goal.deleted_at) - self.assertEqual(data["organization"], str(self.organization.uuid)) - self.assertEqual(data["project"], str(self.project.uuid)) + self.assertEqual(data["organization"], self.organization.id) + self.assertEqual(data["project"], self.project.id) self.assertEqual(data["name"], self.treatment_goal.name) self.assertEqual(data["summary"], self.treatment_goal.summary) self.assertEqual(data["description"], self.treatment_goal.description) diff --git a/src/planscape/goals/tests/test_views.py b/src/planscape/goals/tests/test_views.py index 2f925fc06..dd4e3a0f1 100644 --- a/src/planscape/goals/tests/test_views.py +++ b/src/planscape/goals/tests/test_views.py @@ -172,7 +172,7 @@ def test_filter_by_organization(self): url = reverse("api:goals:treatment_goals-list") response = self.client.get( url, - {"organization": self.organization.uuid}, + {"organization": self.organization.id}, ) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data.get("results")), 1) @@ -183,7 +183,7 @@ def test_filter_by_project(self): url = reverse("api:goals:treatment_goals-list") response = self.client.get( url, - {"project": str(self.project.uuid)}, + {"project": self.project.id}, ) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data.get("results")), 1) diff --git a/src/planscape/impacts/serializers.py b/src/planscape/impacts/serializers.py index 0027c5a43..a04eff878 100644 --- a/src/planscape/impacts/serializers.py +++ b/src/planscape/impacts/serializers.py @@ -1,4 +1,5 @@ from rest_framework import serializers +from rest_framework.relations import PrimaryKeyRelatedField from core.fields import UUIDRelatedField from impacts.models import ( @@ -16,7 +17,6 @@ class Meta: model = ProjectArea fields = ( "id", - "uuid", "name", ) @@ -40,7 +40,6 @@ class Meta: model = TreatmentPlan fields = ( "id", - "uuid", "created_at", "created_by", "updated_at", @@ -60,7 +59,6 @@ class Meta: model = TreatmentPlan fields = ( "id", - "uuid", "created_at", "creator_name", "updated_at", @@ -91,7 +89,6 @@ class Meta: model = TreatmentPrescription fields = ( "id", - "uuid", "created_at", "created_by", "updated_at", @@ -110,13 +107,11 @@ class TreamentPrescriptionUpsertSerializer(serializers.Serializer): default=serializers.CurrentUserDefault(), ) - treatment_plan = UUIDRelatedField( - uuid_field="uuid", + treatment_plan = PrimaryKeyRelatedField( queryset=TreatmentPlan.objects.all(), ) - project_area = UUIDRelatedField( - uuid_field="uuid", + project_area = PrimaryKeyRelatedField( queryset=ProjectArea.objects.all(), ) @@ -133,7 +128,6 @@ class Meta: model = TreatmentPrescription fields = ( "id", - "uuid", "created_at", "created_by", "updated_at", diff --git a/src/planscape/impacts/tests/test_views.py b/src/planscape/impacts/tests/test_views.py index 62245667f..bd1a2f5c1 100644 --- a/src/planscape/impacts/tests/test_views.py +++ b/src/planscape/impacts/tests/test_views.py @@ -26,7 +26,7 @@ def test_create_tx_plan_returns_201(self): format="json", ) data = response.json() - tx_plan = TreatmentPlan.objects.get(uuid=data.get("uuid")) + tx_plan = TreatmentPlan.objects.get(id=data.get("id")) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(1, TreatmentPlan.objects.all().count()) self.assertEqual( diff --git a/src/planscape/metrics/filters.py b/src/planscape/metrics/filters.py index c3c56ef3f..2cea216d5 100644 --- a/src/planscape/metrics/filters.py +++ b/src/planscape/metrics/filters.py @@ -9,13 +9,10 @@ class MetricFilterSet(filters.FilterSet): organization = filters.ModelChoiceFilter( queryset=Organization.objects.all(), field_name="project__organization", - to_field_name="uuid", ) project = filters.ModelChoiceFilter( queryset=Project.objects.all(), - field_name="project", - to_field_name="uuid", ) category = filters.ModelChoiceFilter(queryset=Category.objects.all()) diff --git a/src/planscape/metrics/serializers.py b/src/planscape/metrics/serializers.py index d685594f9..666d4010e 100644 --- a/src/planscape/metrics/serializers.py +++ b/src/planscape/metrics/serializers.py @@ -7,12 +7,11 @@ class MetricSerializer(serializers.ModelSerializer): - organization = serializers.UUIDField( - source="project.organization.uuid", read_only=True, default=None + organization = serializers.PrimaryKeyRelatedField( + source="project.organization.id", read_only=True, default=None ) - project = UUIDRelatedField( - uuid_field="uuid", + project = serializers.PrimaryKeyRelatedField( queryset=Project.objects.all(), ) @@ -21,7 +20,7 @@ class MetricSerializer(serializers.ModelSerializer): class Meta: model = Metric fields = ( - "uuid", + "id", "created_at", "created_by", "updated_at", diff --git a/src/planscape/metrics/tests/test_serializers.py b/src/planscape/metrics/tests/test_serializers.py index 8bd5cf0c6..4ffb20eae 100644 --- a/src/planscape/metrics/tests/test_serializers.py +++ b/src/planscape/metrics/tests/test_serializers.py @@ -47,12 +47,10 @@ def setUp(self): def test_metric_serializer(self): serializer = MetricSerializer(instance=self.metric) data = serializer.data - self.assertEqual(data["uuid"], str(self.metric.uuid)) - self.assertEqual( - data["organization"], str(self.metric.project.organization.uuid) - ) - self.assertEqual(data["project"], str(self.metric.project.uuid)) - self.assertEqual(data["dataset"]["uuid"], str(self.metric.dataset.uuid)) + self.assertEqual(data["id"], self.metric.id) + self.assertEqual(data["organization"], self.metric.project.organization.id) + self.assertEqual(data["project"], self.metric.project.id) + self.assertEqual(data["dataset"]["id"], self.metric.dataset.id) self.assertEqual(data["name"], self.metric.name) self.assertEqual(data["display_name"], self.metric.display_name) self.assertEqual(data["capabilities"], self.metric.capabilities) diff --git a/src/planscape/metrics/tests/test_views.py b/src/planscape/metrics/tests/test_views.py index d3919eeb4..99ad26612 100644 --- a/src/planscape/metrics/tests/test_views.py +++ b/src/planscape/metrics/tests/test_views.py @@ -63,7 +63,7 @@ def test_list_metrics(self): def test_retrieve_metric(self): response = self.client.get( - reverse("api:metrics:metrics-detail", args=[self.metric.uuid]) + reverse("api:metrics:metrics-detail", args=[self.metric.id]) ) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data["name"], self.metric.name) @@ -76,7 +76,7 @@ def test_filter_by_organization(self): url = reverse("api:metrics:metrics-list") response = self.client.get( url, - {"organization": self.organization.uuid}, + {"organization": self.organization.id}, ) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data.get("results")), 1) @@ -87,7 +87,7 @@ def test_filter_by_project(self): url = reverse("api:metrics:metrics-list") response = self.client.get( url, - {"project": str(self.project.uuid)}, + {"project": str(self.project.id)}, ) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data.get("results")), 1) diff --git a/src/planscape/metrics/views.py b/src/planscape/metrics/views.py index a551e439c..f163722b7 100644 --- a/src/planscape/metrics/views.py +++ b/src/planscape/metrics/views.py @@ -20,4 +20,3 @@ class MetricsViewSet(ListModelMixin, RetrieveModelMixin, GenericViewSet): ordering_fields = ["name", "created_at"] filterset_class = MetricFilterSet serializer_class = MetricSerializer - lookup_field = "uuid" diff --git a/src/planscape/planning/serializers.py b/src/planscape/planning/serializers.py index a103ae94c..e487e86f2 100644 --- a/src/planscape/planning/serializers.py +++ b/src/planscape/planning/serializers.py @@ -271,7 +271,6 @@ class ListScenarioSerializer(serializers.ModelSerializer): class Meta: fields = ( "id", - "uuid", "updated_at", "created_at", "planning_area", @@ -318,7 +317,6 @@ def update(self, instance, validated_data): class Meta: fields = ( "id", - "uuid", "updated_at", "created_at", "planning_area", @@ -370,7 +368,6 @@ class Meta: model = ProjectArea fields = ( "id", - "uuid", "scenario", "name", "origin", diff --git a/src/planscape/projects/serializers.py b/src/planscape/projects/serializers.py index a33dd24c5..01cf0501e 100644 --- a/src/planscape/projects/serializers.py +++ b/src/planscape/projects/serializers.py @@ -7,7 +7,7 @@ class ProjectSerializer(serializers.ModelSerializer): class Meta: model = Project fields = ( - "uuid", + "id", "organization", "created_at", "created_by", diff --git a/src/planscape/projects/tests/test_views.py b/src/planscape/projects/tests/test_views.py index 3e19055f0..7dfb9151a 100644 --- a/src/planscape/projects/tests/test_views.py +++ b/src/planscape/projects/tests/test_views.py @@ -76,7 +76,7 @@ def test_list_projects(self): def test_retrieve_project(self): url = reverse( "api:projects:projects-detail", - args=[self.public_project.uuid], + args=[self.public_project.id], ) response = self.client.get(url) self.assertEqual(response.status_code, 200) @@ -85,7 +85,7 @@ def test_retrieve_project(self): def test_retrieve_nonexistent_project(self): url = reverse( "api:projects:projects-detail", - args=[str(uuid4())], + args=[str(999999)], ) response = self.client.get(url) self.assertEqual(response.status_code, 404) diff --git a/src/planscape/projects/views.py b/src/planscape/projects/views.py index 94f5faaa8..9f0f42f27 100644 --- a/src/planscape/projects/views.py +++ b/src/planscape/projects/views.py @@ -17,7 +17,6 @@ class ProjectViewSet(ListModelMixin, RetrieveModelMixin, GenericViewSet): ordering_fields = ["name", "display_name", "created_at"] filterset_class = ProjectFilterSet serializer_class = ProjectSerializer - lookup_field = "uuid" def get_queryset(self): # TODO: we need to customize