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

feat: add get_taxonomy_by_export_id method [FC-0049] #164

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions openedx_tagging/core/tagging/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ def get_taxonomy(taxonomy_id: int) -> Taxonomy | None:
return taxonomy.cast() if taxonomy else None


def get_taxonomy_by_export_id(taxonomy_export_id: str) -> Taxonomy | None:
"""
Returns a Taxonomy cast to the appropriate subclass which has the given export ID.
"""
taxonomy = Taxonomy.objects.filter(export_id=taxonomy_export_id).first()
return taxonomy.cast() if taxonomy else None


def get_taxonomies(enabled=True) -> QuerySet[Taxonomy]:
"""
Returns a queryset containing the enabled taxonomies, sorted by name.
Expand Down
2 changes: 1 addition & 1 deletion openedx_tagging/core/tagging/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class Taxonomy(models.Model):
"Indicates whether this taxonomy should be visible to object authors."
),
)
# External ID that should only be used on import/export.
# Export ID that should only be used on import/export.
pomegranited marked this conversation as resolved.
Show resolved Hide resolved
# NOT use for any other purposes, you can use the numeric ID of the model instead;
# this id is editable.
export_id = models.CharField(
Expand Down
7 changes: 7 additions & 0 deletions tests/openedx_tagging/core/tagging/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ def test_get_taxonomy(self) -> None:
no_tax = tagging_api.get_taxonomy(200)
assert no_tax is None

def test_get_taxonomy_by_export_id(self) -> None:
tax1 = tagging_api.get_taxonomy_by_export_id("life_on_earth")
assert tax1 == self.taxonomy

no_tax = tagging_api.get_taxonomy_by_export_id("nope")
assert no_tax is None

def test_get_taxonomies(self) -> None:
tax1 = tagging_api.create_taxonomy("Enabled")
tax2 = tagging_api.create_taxonomy("Disabled", enabled=False)
Expand Down
Loading