Skip to content

Commit

Permalink
refactor: Perform resync_object_tags in api
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuf-musleh committed Oct 17, 2023
1 parent f9e28da commit bdaffeb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
27 changes: 23 additions & 4 deletions openedx_tagging/core/tagging/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,14 @@ def resync_object_tags(object_tags: QuerySet | None = None) -> int:
if not object_tags:
object_tags = ObjectTag.objects.select_related("tag", "taxonomy")

return ObjectTag.resync_object_tags(object_tags)
num_changed = 0
for object_tag in object_tags:
changed = object_tag.resync()
if changed:
object_tag.save()
num_changed += 1

return num_changed


def get_object_tags(
Expand Down Expand Up @@ -332,17 +339,29 @@ def update_tag_in_taxonomy(taxonomy: Taxonomy, tag: int, tag_value: str):
Currently only support updates the Tag value.
"""
return taxonomy.cast().update_tag(tag, tag_value)
taxonomy = taxonomy.cast()
updated_tag = taxonomy.update_tag(tag, tag_value)

# Resync all related ObjectTags to update to the new Tag value
object_tags = taxonomy.objecttag_set.all()
resync_object_tags(object_tags)

return updated_tag


def delete_tags_from_taxonomy(
taxonomy: Taxonomy,
tag_ids: list[Tag],
tag_ids: list[int],
with_subtags: bool
):
"""
Delete Tags that belong to a Taxonomy. If any of the Tags have children and
the `with_subtags` is not set to `True` it will fail, otherwise
the sub-tags will be deleted as well.
"""
return taxonomy.cast().delete_tags(tag_ids, with_subtags)
taxonomy = taxonomy.cast()
taxonomy.delete_tags(tag_ids, with_subtags)

# Resync all related ObjectTags after deleting the Tag(s)
object_tags = taxonomy.objecttag_set.all()
resync_object_tags(object_tags)
19 changes: 0 additions & 19 deletions openedx_tagging/core/tagging/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,6 @@ def update_tag(self, tag_id: int, tag_value: str) -> Tag:
tag = self.tag_set.get(id=tag_id)
tag.value = tag_value
tag.save()

# Resync all related ObjectTags to update to the new Tag value
object_tags = self.objecttag_set.all()
ObjectTag.resync_object_tags(object_tags)
return tag

def delete_tags(self, tag_ids: List[int], with_subtags: bool = False):
Expand Down Expand Up @@ -709,18 +705,3 @@ def copy(self, object_tag: ObjectTag) -> Self:
self._value = object_tag._value # pylint: disable=protected-access
self._name = object_tag._name # pylint: disable=protected-access
return self

@classmethod
def resync_object_tags(cls, object_tags: models.QuerySet[ObjectTag]) -> int:
"""
Reconciles ObjectTag entries with any changes made to their associated
taxonomies and tags. Return the number of changes made.
"""
num_changed = 0
for object_tag in object_tags:
changed = object_tag.resync()
if changed:
object_tag.save()
num_changed += 1

return num_changed

0 comments on commit bdaffeb

Please sign in to comment.