Skip to content

Commit

Permalink
Revert "Limit related OrgUnit choices"
Browse files Browse the repository at this point in the history
This reverts commit 0cac7e1.
  • Loading branch information
kemar committed Jan 13, 2025
1 parent c7ad488 commit b491410
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 32 deletions.
18 changes: 6 additions & 12 deletions iaso/api/data_source_versions_synchronization/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,6 @@ def validate(self, validated_data):
return validated_data


class OrgUnitForUser(serializers.PrimaryKeyRelatedField):
"""
Limit related OrgUnit choices.
"""

def get_queryset(self):
user = self.context["request"].user
return OrgUnit.objects.filter_for_user(user)


class CreateJsonDiffParametersSerializer(serializers.Serializer):
"""
Validate the parameters of DataSourceVersionsSynchronization.create_json_diff().
Expand All @@ -101,15 +91,19 @@ class CreateJsonDiffParametersSerializer(serializers.Serializer):
source_version_to_update_validation_status = serializers.ChoiceField(
choices=OrgUnit.VALIDATION_STATUS_CHOICES, required=False, default=None
)
source_version_to_update_top_org_unit = OrgUnitForUser(required=False, default=None)
source_version_to_update_top_org_unit = serializers.PrimaryKeyRelatedField(
queryset=OrgUnit.objects.all(), required=False, default=None
)
source_version_to_update_org_unit_types = serializers.PrimaryKeyRelatedField(
queryset=OrgUnitType.objects.all(), many=True, required=False, default=None
)
# Version to compare with.
source_version_to_compare_with_validation_status = serializers.ChoiceField(
choices=OrgUnit.VALIDATION_STATUS_CHOICES, required=False, default=None
)
source_version_to_compare_with_top_org_unit = OrgUnitForUser(required=False, default=None)
source_version_to_compare_with_top_org_unit = serializers.PrimaryKeyRelatedField(
queryset=OrgUnit.objects.all(), required=False, default=None
)
source_version_to_compare_with_org_unit_types = serializers.PrimaryKeyRelatedField(
queryset=OrgUnitType.objects.all(), many=True, required=False, default=None
)
Expand Down
2 changes: 1 addition & 1 deletion iaso/api/data_source_versions_synchronization/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def create_json_diff(self, request: Request, pk: int) -> Response:

json_diff_params_serializer = CreateJsonDiffParametersSerializer(
data=self.request.data,
context={"data_source_versions_synchronization": data_source_versions_synchronization, "request": request},
context={"data_source_versions_synchronization": data_source_versions_synchronization},
)
json_diff_params_serializer.is_valid(raise_exception=True)
json_diff_params = json_diff_params_serializer.validated_data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import time_machine

from rest_framework.test import APIRequestFactory

from iaso import models as m
from iaso.api.data_source_versions_synchronization.serializers import (
CreateJsonDiffParametersSerializer,
Expand Down Expand Up @@ -144,10 +142,8 @@ class CreateJsonDiffParametersSerializerTestCase(TestCase):
def setUpTestData(cls):
cls.account = m.Account.objects.create(name="Account")
cls.user = cls.create_user_with_profile(username="user", first_name="Foo", last_name="Bar", account=cls.account)
cls.project = m.Project.objects.create(name="Project", account=cls.account, app_id="foo.bar.baz")

cls.data_source = m.DataSource.objects.create(name="Data source")
cls.data_source.projects.set([cls.project])

cls.source_version_to_update = m.SourceVersion.objects.create(
data_source=cls.data_source, number=2, description="Source A"
Expand Down Expand Up @@ -195,9 +191,6 @@ def test_deserialize_without_params(self):
)

def test_deserialize_with_params(self):
request = APIRequestFactory().get("/")
request.user = self.user

json_diff_params = {
# Version to update.
"source_version_to_update_validation_status": OrgUnit.VALIDATION_NEW,
Expand All @@ -216,8 +209,7 @@ def test_deserialize_with_params(self):
"field_names": ["name", "parent", "opening_date", "closed_date"],
}
serializer = CreateJsonDiffParametersSerializer(
data=json_diff_params,
context={"data_source_versions_synchronization": self.data_source_sync, "request": request},
data=json_diff_params, context={"data_source_versions_synchronization": self.data_source_sync}
)

self.assertTrue(serializer.is_valid())
Expand Down Expand Up @@ -247,15 +239,11 @@ def test_deserialize_with_params(self):
self.assertIn("closed_date", data["field_names"])

def test_validate_source_version_to_update_top_org_unit(self):
request = APIRequestFactory().get("/")
request.user = self.user

json_diff_params = {
"source_version_to_update_top_org_unit": self.source_version_to_compare_with_top_org_unit.pk,
}
serializer = CreateJsonDiffParametersSerializer(
data=json_diff_params,
context={"data_source_versions_synchronization": self.data_source_sync, "request": request},
data=json_diff_params, context={"data_source_versions_synchronization": self.data_source_sync}
)
self.assertFalse(serializer.is_valid())
self.assertIn(
Expand All @@ -264,15 +252,11 @@ def test_validate_source_version_to_update_top_org_unit(self):
)

def test_validate_source_version_to_compare_with_top_org_unit(self):
request = APIRequestFactory().get("/")
request.user = self.user

json_diff_params = {
"source_version_to_compare_with_top_org_unit": self.source_version_to_update_top_org_unit.pk,
}
serializer = CreateJsonDiffParametersSerializer(
data=json_diff_params,
context={"data_source_versions_synchronization": self.data_source_sync, "request": request},
data=json_diff_params, context={"data_source_versions_synchronization": self.data_source_sync}
)
self.assertFalse(serializer.is_valid())
self.assertIn(
Expand Down

0 comments on commit b491410

Please sign in to comment.