Skip to content

Commit

Permalink
feat: minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenmacdonald committed Sep 29, 2023
1 parent 89a4ab8 commit ba53463
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
9 changes: 5 additions & 4 deletions openedx_tagging/core/tagging/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def resync_object_tags(object_tags: QuerySet | None = None) -> int:
def get_object_tags(
object_id: str,
taxonomy_id: int | None = None,
ObjectTagClass: type[ObjectTag] = ObjectTag
object_tag_class: type[ObjectTag] = ObjectTag
) -> QuerySet[ObjectTag]:
"""
Returns a Queryset of object tags for a given object.
Expand All @@ -154,7 +154,7 @@ def get_object_tags(
"""
filters = {"taxonomy_id": taxonomy_id} if taxonomy_id else {}
tags = (
ObjectTagClass.objects.filter(object_id=object_id, **filters)
object_tag_class.objects.filter(object_id=object_id, **filters)
.select_related("tag", "taxonomy")
.order_by("id")
)
Expand All @@ -175,15 +175,15 @@ def tag_object(
taxonomy: Taxonomy,
tags: list[str],
object_id: str,
ObjectTagClass: type[ObjectTag] = ObjectTag,
object_tag_class: type[ObjectTag] = ObjectTag,
) -> None:
"""
Replaces the existing ObjectTag entries for the given taxonomy + object_id
with the given list of tags.
tags: A list of the values of the tags from this taxonomy to apply.
ObjectTagClass: Optional. Use a proxy subclass of ObjectTag for additional
object_tag_class: Optional. Use a proxy subclass of ObjectTag for additional
validation. (e.g. only allow tagging certain types of objects.)
Raised Tag.DoesNotExist if the proposed tags are invalid for this taxonomy.
Expand All @@ -206,6 +206,7 @@ def _check_new_tag_count(new_tag_count: int) -> None:
if not isinstance(tags, list):
raise ValueError(_(f"Tags must be a list, not {type(tags).__name__}."))

ObjectTagClass = object_tag_class
taxonomy = taxonomy.cast() # Make sure we're using the right subclass. This is a no-op if we are already.
tags = list(dict.fromkeys(tags)) # Remove duplicates preserving order

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1296,3 +1296,4 @@
allow_multiple: false
allow_free_text: false
visible_to_authors: true
_taxonomy_class: openedx_tagging.core.tagging.models.system_defined.LanguageTaxonomy
2 changes: 1 addition & 1 deletion tests/openedx_tagging/core/tagging/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_get_taxonomies(self) -> None:
] + self.dummy_taxonomies
assert str(enabled[0]) == f"<Taxonomy> ({tax1.id}) Enabled"
assert str(enabled[1]) == "<Taxonomy> (5) Import Taxonomy Test"
assert str(enabled[2]) == "<Taxonomy> (-1) Languages"
assert str(enabled[2]) == "<LanguageTaxonomy> (-1) Languages"
assert str(enabled[3]) == "<Taxonomy> (1) Life on Earth"
assert str(enabled[4]) == "<SystemDefinedTaxonomy> (4) System defined taxonomy"

Expand Down
4 changes: 1 addition & 3 deletions tests/openedx_tagging/core/tagging/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def setUp(self):
self.system_taxonomy = Taxonomy.objects.get(
name="System defined taxonomy"
)
self.language_taxonomy = Taxonomy.objects.get(name="Languages")
self.language_taxonomy.taxonomy_class = LanguageTaxonomy
self.language_taxonomy = self.language_taxonomy.cast()
self.language_taxonomy = LanguageTaxonomy.objects.get(name="Languages")
self.user_taxonomy = Taxonomy.objects.get(name="User Authors").cast()
self.archaea = get_tag("Archaea")
self.archaebacteria = get_tag("Archaebacteria")
Expand Down

0 comments on commit ba53463

Please sign in to comment.