Skip to content

Commit

Permalink
feat: Remove "re-sync" of deleted taxonomies, update test_api.py
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenmacdonald committed Sep 29, 2023
1 parent bc30c50 commit 689cd26
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 382 deletions.
6 changes: 4 additions & 2 deletions openedx_tagging/core/tagging/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,17 @@ 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
) -> QuerySet[ObjectTag]:
"""
Returns a Queryset of object tags for a given object.
Pass taxonomy to limit the returned object_tags to a specific taxonomy.
Pass taxonomy_id to limit the returned object_tags to a specific taxonomy.
"""
filters = {"taxonomy_id": taxonomy_id} if taxonomy_id else {}
tags = (
ObjectTagClass.objects.filter(object_id=object_id)
ObjectTagClass.objects.filter(object_id=object_id, **filters)
.select_related("tag", "taxonomy")
.order_by("id")
)
Expand Down
30 changes: 2 additions & 28 deletions openedx_tagging/core/tagging/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,34 +567,8 @@ def resync(self) -> bool:
"""
changed = False

# Locate an enabled taxonomy matching _name, and maybe a tag matching _value
if not self.taxonomy_id:
# Use the linked tag's taxonomy if there is one.
if self.tag:
self.taxonomy_id = self.tag.taxonomy_id
changed = True
else:
for taxonomy in Taxonomy.objects.filter(
name=self.name, enabled=True
).order_by("allow_free_text", "id"):
# Cast to the subclass to preserve custom validation
taxonomy = taxonomy.cast()

# Closed taxonomies require a tag matching _value,
# and we'd rather match a closed taxonomy than an open one.
# So see if there's a matching tag available in this taxonomy.
tag = taxonomy.tag_set.filter(value=self.value).first()

# Make sure this taxonomy will accept object tags like this.
self.taxonomy = taxonomy
self.tag = tag
if taxonomy.validate_object_tag(self):
changed = True
break
# If not, undo those changes and try the next one
else:
self.taxonomy = None
self.tag = None
# We used to have code here that would try to find a new taxonomy if the current taxonomy has been deleted.
# But for now that's removed, as it risks things like linking a tag to the wrong org's taxonomy.

# Sync the stored _name with the taxonomy.name
if self.taxonomy and self._name != self.taxonomy.name:
Expand Down
Loading

0 comments on commit 689cd26

Please sign in to comment.