Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLAN-1253 #1526

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/planscape/goals/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django_filters import rest_framework as filters
from goals.models import TreatmentGoal
from organizations.models import Organization
from planscape.filters import CharArrayFilter
from projects.models import Project


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:
model = TreatmentGoal
fields = {
"name": ["exact", "icontains"],
}
3 changes: 2 additions & 1 deletion src/planscape/goals/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ class MetricAttribute(models.TextChoices):
SUM = "sum", "Sum"
MAJORITY = "majority", "Majority"
MINORITY = "minority", "Minority"
COUNT = "count", "COUNT"
COUNT = "COUNT", "Count"
FIELD = "FIELD", "Field"


class PostProcessingFunction(models.TextChoices):
Expand Down
6 changes: 6 additions & 0 deletions src/planscape/goals/routers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from rest_framework.routers import SimpleRouter

from goals.views import TreatmentGoalViewSet

router = SimpleRouter()
router.register("treatment_goals", TreatmentGoalViewSet, basename="treatment_goals")
89 changes: 89 additions & 0 deletions src/planscape/goals/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from rest_framework import serializers

from datasets.models import Dataset
from goals.models import MetricUsage, TreatmentGoal
from metrics.models import Metric


class MetricGoalSerializer(serializers.ModelSerializer):

"""Specific serializer to be used within goals.
READ ONLY.
"""

class Meta:
model = Metric
fields = (
"uuid",
"category",
"name",
"display_name",
)


class DatasetGoalSerializer(serializers.ModelSerializer):
"""Specific serializer to be used within goals.
READ ONLY
"""

class Meta:
model = Dataset
fields = (
"uuid",
"name",
"type",
"url",
"data_units",
"provider",
"source",
"source_url",
"reference_url",
)


class MetricUsageSerializer(serializers.ModelSerializer):
metric = MetricGoalSerializer()
dataset = DatasetGoalSerializer(source="metric.dataset")

class Meta:
model = MetricUsage
fields = (
"metric",
"dataset",
"type",
"attribute",
"pre_processing",
"post_processing",
"output_units",
)


class TreatmentGoalSerializer(serializers.ModelSerializer):
organization = serializers.UUIDField(
source="project.organization.uuid", read_only=True
)

metric_usages = MetricUsageSerializer(
many=True,
read_only=True,
)

project = serializers.UUIDField(source="project.uuid", read_only=True)

class Meta:
model = TreatmentGoal
fields = (
"uuid",
"created_at",
"created_by",
"updated_at",
"deleted_at",
"organization",
"project",
"name",
"summary",
"description",
"metric_usages",
"executor",
"execution_options",
)
Empty file.
129 changes: 129 additions & 0 deletions src/planscape/goals/tests/test_serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
from django.test import TransactionTestCase
from django.contrib.gis.geos import MultiPolygon, Polygon
from datasets.models import Dataset
from goals.models import (
MetricAttribute,
MetricUsageType,
PostProcessingFunction,
PreProcessingFunction,
TreatmentGoal,
MetricUsage,
TreatmentGoalExecutor,
get_forsys_defaults,
)
from goals.serializers import TreatmentGoalSerializer
from metrics.models import Category, Metric
from projects.models import Project
from organizations.models import Organization
from django.contrib.auth import get_user_model

User = get_user_model()


class TreatmentGoalSerializerTest(TransactionTestCase):
def setUp(self):
# Create a user
self.user = User.objects.create_user(
username="testuser", password="testpassword"
)

# Create an organization
self.organization = Organization.objects.create(name="Test Organization")

# Create a project
self.project = Project.objects.create(
name="Test Project",
organization=self.organization,
geometry=MultiPolygon(Polygon(((0, 0), (1, 0), (1, 1), (0, 1), (0, 0)))),
)
# Create a metric
self.metric = Metric.objects.create(
created_by=self.user,
project=self.project,
name="Test Metric",
display_name="Test Metric Display Name",
dataset=Dataset.objects.create(
created_by=self.user,
organization=self.organization,
name="Test Dataset",
type="VECTOR",
url="http://example.com",
),
category=Category.add_root(
created_by=self.user,
organization=self.organization,
name="Test Category",
path="Test Path",
),
)

# Create a treatment goal
self.treatment_goal = TreatmentGoal.objects.create(
created_by=self.user,
project=self.project,
name="Test Treatment Goal",
summary="Test Summary",
description="Test Description",
executor=TreatmentGoalExecutor.FORSYS,
execution_options=get_forsys_defaults(),
)

# Create a metric usage
self.metric_usage = MetricUsage.objects.create(
treatment_goal=self.treatment_goal,
metric=self.metric,
type=MetricUsageType.PRIORITY,
attribute=MetricAttribute.MEAN,
pre_processing=PreProcessingFunction.NONE,
post_processing=PostProcessingFunction.NONE,
output_units="units",
)

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["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["name"], self.treatment_goal.name)
self.assertEqual(data["summary"], self.treatment_goal.summary)
self.assertEqual(data["description"], self.treatment_goal.description)
self.assertEqual(data["executor"], self.treatment_goal.executor)
self.assertEqual(
data["execution_options"], self.treatment_goal.execution_options
)

def test_treatment_goal_serializer_update(self):
"""Test updating an existing TreatmentGoal instance using the serializer"""
data = {
"name": "Updated Treatment Goal",
"summary": "Updated Summary",
"description": "Updated Description",
}
serializer = TreatmentGoalSerializer(
instance=self.treatment_goal, data=data, partial=True
)
self.assertTrue(serializer.is_valid(), serializer.errors)
treatment_goal = serializer.save()

self.assertEqual(treatment_goal.name, data["name"])
self.assertEqual(treatment_goal.summary, data["summary"])
self.assertEqual(treatment_goal.description, data["description"])

def test_treatment_goal_serializer_validation(self):
"""Test validation errors in the TreatmentGoalSerializer"""
data = {
"created_by": self.user.id,
"project": self.project.id,
"name": "",
"summary": "New Summary",
"description": "New Description",
"executor": TreatmentGoalExecutor.FORSYS,
"execution_options": get_forsys_defaults(),
}
serializer = TreatmentGoalSerializer(data=data)
self.assertFalse(serializer.is_valid())
self.assertIn("name", serializer.errors)
Loading
Loading