Skip to content

Commit

Permalink
removes all uuid from the API & filters
Browse files Browse the repository at this point in the history
  • Loading branch information
george-silva committed Aug 13, 2024
1 parent 90de14c commit 068d845
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 55 deletions.
7 changes: 3 additions & 4 deletions src/planscape/datasets/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -27,7 +26,7 @@ class DatasetDetailSerializer(DatasetSerializer):
class Meta:
model = Dataset
fields = (
"uuid",
"id",
"created_at",
"created_by",
"updated_at",
Expand Down
8 changes: 4 additions & 4 deletions src/planscape/datasets/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions src/planscape/goals/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions src/planscape/goals/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -59,21 +59,21 @@ 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(
many=True,
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",
Expand Down
6 changes: 3 additions & 3 deletions src/planscape/goals/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/planscape/goals/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
12 changes: 3 additions & 9 deletions src/planscape/impacts/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from rest_framework import serializers
from rest_framework.relations import PrimaryKeyRelatedField

from core.fields import UUIDRelatedField
from impacts.models import (
Expand All @@ -16,7 +17,6 @@ class Meta:
model = ProjectArea
fields = (
"id",
"uuid",
"name",
)

Expand All @@ -40,7 +40,6 @@ class Meta:
model = TreatmentPlan
fields = (
"id",
"uuid",
"created_at",
"created_by",
"updated_at",
Expand All @@ -60,7 +59,6 @@ class Meta:
model = TreatmentPlan
fields = (
"id",
"uuid",
"created_at",
"creator_name",
"updated_at",
Expand Down Expand Up @@ -91,7 +89,6 @@ class Meta:
model = TreatmentPrescription
fields = (
"id",
"uuid",
"created_at",
"created_by",
"updated_at",
Expand All @@ -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(),
)

Expand All @@ -133,7 +128,6 @@ class Meta:
model = TreatmentPrescription
fields = (
"id",
"uuid",
"created_at",
"created_by",
"updated_at",
Expand Down
2 changes: 1 addition & 1 deletion src/planscape/impacts/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 0 additions & 3 deletions src/planscape/metrics/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
9 changes: 4 additions & 5 deletions src/planscape/metrics/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)

Expand All @@ -21,7 +20,7 @@ class MetricSerializer(serializers.ModelSerializer):
class Meta:
model = Metric
fields = (
"uuid",
"id",
"created_at",
"created_by",
"updated_at",
Expand Down
10 changes: 4 additions & 6 deletions src/planscape/metrics/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 3 additions & 3 deletions src/planscape/metrics/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/planscape/metrics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ class MetricsViewSet(ListModelMixin, RetrieveModelMixin, GenericViewSet):
ordering_fields = ["name", "created_at"]
filterset_class = MetricFilterSet
serializer_class = MetricSerializer
lookup_field = "uuid"
3 changes: 0 additions & 3 deletions src/planscape/planning/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ class ListScenarioSerializer(serializers.ModelSerializer):
class Meta:
fields = (
"id",
"uuid",
"updated_at",
"created_at",
"planning_area",
Expand Down Expand Up @@ -318,7 +317,6 @@ def update(self, instance, validated_data):
class Meta:
fields = (
"id",
"uuid",
"updated_at",
"created_at",
"planning_area",
Expand Down Expand Up @@ -370,7 +368,6 @@ class Meta:
model = ProjectArea
fields = (
"id",
"uuid",
"scenario",
"name",
"origin",
Expand Down
2 changes: 1 addition & 1 deletion src/planscape/projects/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ProjectSerializer(serializers.ModelSerializer):
class Meta:
model = Project
fields = (
"uuid",
"id",
"organization",
"created_at",
"created_by",
Expand Down
4 changes: 2 additions & 2 deletions src/planscape/projects/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/planscape/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 068d845

Please sign in to comment.