diff --git a/openedx_tagging/core/tagging/api.py b/openedx_tagging/core/tagging/api.py index 19d4f11e..2a7bff9a 100644 --- a/openedx_tagging/core/tagging/api.py +++ b/openedx_tagging/core/tagging/api.py @@ -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( @@ -332,12 +339,19 @@ 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 ): """ @@ -345,4 +359,9 @@ def delete_tags_from_taxonomy( 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) diff --git a/openedx_tagging/core/tagging/models/base.py b/openedx_tagging/core/tagging/models/base.py index d23cb8a4..cbf224b4 100644 --- a/openedx_tagging/core/tagging/models/base.py +++ b/openedx_tagging/core/tagging/models/base.py @@ -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): @@ -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