Skip to content

Commit

Permalink
feat: add all_orgs field
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Nov 6, 2023
1 parent 79a95bc commit e2e8f76
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
30 changes: 16 additions & 14 deletions openedx/core/djangoapps/content_tagging/rest_api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,27 @@ def validate(self, attrs: dict) -> dict:
return attrs


class OrgListField(serializers.RelatedField):
"""
Serializer to return the list of orgs for a taxonomy
"""
def to_representation(self, value):
"""
Return the Organization short_name, not the TaxonomyOrg object
"""
return value.org.short_name if value.org else None


class TaxonomyOrgSerializer(TaxonomySerializer):
"""
Serializer for Taxonomy objects inclusing the associated orgs
"""

orgs = OrgListField(many=True, read_only=True, source="taxonomyorg_set")
orgs = serializers.SerializerMethodField()
all_orgs = serializers.SerializerMethodField()

def get_orgs(self, obj) -> list[str]:
"""
Return the list of orgs for the taxonomy.
"""
return [taxonomy_org.org.short_name for taxonomy_org in obj.taxonomyorg_set.all() if taxonomy_org.org]

def get_all_orgs(self, obj) -> bool:
"""
Return True if the taxonomy is associated with all orgs.
"""
return obj.taxonomyorg_set.filter(org__isnull=True).exists()

class Meta:
model = TaxonomySerializer.Meta.model
fields = TaxonomySerializer.Meta.fields + ["orgs"]
read_only_fields = ["orgs"]
fields = TaxonomySerializer.Meta.fields + ["orgs", "all_orgs"]
read_only_fields = ["orgs", "all_orgs"]
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@ def test_update_org(self) -> None:
url = TAXONOMY_ORG_DETAIL_URL.format(pk=self.tA1.pk)
response = self.client.get(url)
assert response.data["orgs"] == [self.orgB.short_name, self.orgX.short_name]
assert not response.data["all_orgs"]

def test_update_all_org(self) -> None:
"""
Expand All @@ -1080,7 +1081,8 @@ def test_update_all_org(self) -> None:
# Check that the orgs were updated
url = TAXONOMY_ORG_DETAIL_URL.format(pk=self.tA1.pk)
response = self.client.get(url)
assert response.data["orgs"] == [None]
assert response.data["orgs"] == []
assert response.data["all_orgs"]

def test_update_no_org(self) -> None:
"""
Expand All @@ -1097,6 +1099,7 @@ def test_update_no_org(self) -> None:
url = TAXONOMY_ORG_DETAIL_URL.format(pk=self.tA1.pk)
response = self.client.get(url)
assert response.data["orgs"] == []
assert not response.data["all_orgs"]

@ddt.data(
(True, ["orgX"], "Using both all_orgs and orgs parameters should throw error"),
Expand Down Expand Up @@ -1135,7 +1138,8 @@ def test_update_org_system_defined(self) -> None:
# Check that the orgs didn't change
url = TAXONOMY_ORG_DETAIL_URL.format(pk=self.st1.pk)
response = self.client.get(url)
assert response.data["orgs"] == [None]
assert response.data["orgs"] == []
assert response.data["all_orgs"]

@ddt.data(
"staffA",
Expand Down

0 comments on commit e2e8f76

Please sign in to comment.