From fb8d16112fa4705fda8f8ac44cd3401cfb08619e Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Wed, 20 Dec 2023 17:08:15 -0500 Subject: [PATCH] fix: Issue when delete all tags on import --- .../core/tagging/import_export/actions.py | 48 +++++++++++++++---- .../core/tagging/import_export/import_plan.py | 2 +- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/openedx_tagging/core/tagging/import_export/actions.py b/openedx_tagging/core/tagging/import_export/actions.py index 679f36ae..524c6589 100644 --- a/openedx_tagging/core/tagging/import_export/actions.py +++ b/openedx_tagging/core/tagging/import_export/actions.py @@ -72,7 +72,9 @@ def _get_tag(self) -> Tag: """ Returns the respective tag of this actions """ - return self.taxonomy.tag_set.get(external_id=self.tag.id) + if self.tag.id: + return self.taxonomy.tag_set.get(external_id=self.tag.id) + return self.taxonomy.tag_set.get(value=self.tag.value) def _search_action( self, @@ -256,16 +258,35 @@ class UpdateParentTag(ImportAction): def __str__(self) -> str: taxonomy_tag = self._get_tag() + if not taxonomy_tag.parent: from_str = _("from empty parent") else: - from_str = _("from parent (external_id={external_id})").format(external_id=taxonomy_tag.parent.external_id) + if taxonomy_tag.parent.external_id: + from_str = _("from parent (external_id={external_id})").format( + external_id=taxonomy_tag.parent.external_id + ) + else: + from_str = _("from parent (value={value})").format( + value=taxonomy_tag.parent.value + ) + from_str = "" + + if taxonomy_tag.external_id: + prefix_str = _("Update the parent of tag (external_id={external_id})").format( + external_id=taxonomy_tag.external_id + ) + else: + prefix_str = "" + prefix_str = _("Update the parent of tag (value={value})").format( + value=taxonomy_tag.value + ) return str( _( - "Update the parent of tag (external_id={external_id}) " + "{prefix_str} " "{from_str} to parent (external_id={parent_id})." - ).format(external_id=taxonomy_tag.external_id, from_str=from_str, parent_id=self.tag.parent_id) + ).format(prefix_str=prefix_str, from_str=from_str, parent_id=self.tag.parent_id) ) @classmethod @@ -323,11 +344,18 @@ class RenameTag(ImportAction): def __str__(self) -> str: taxonomy_tag = self._get_tag() + if taxonomy_tag.external_id: + prefix_str = _("Rename tag value of tag (external_id={external_id})").format( + external_id=taxonomy_tag.external_id + ) + else: + prefix_str = _("Rename tag value of tag (id={id})").format(id=taxonomy_tag.id) + return str( _( - "Rename tag value of tag (external_id={external_id}) " + "{prefix_str} " "from '{from_value}' to '{to_value}'" - ).format(external_id=taxonomy_tag.external_id, from_value=taxonomy_tag.value, to_value=self.tag.value) + ).format(prefix_str=prefix_str, from_value=taxonomy_tag.value, to_value=self.tag.value) ) @classmethod @@ -373,7 +401,9 @@ class DeleteTag(ImportAction): """ def __str__(self) -> str: - return str(_("Delete tag (external_id={external_id})").format(external_id=self.tag.id)) + if self.tag.id: + return str(_("Delete tag (external_id={external_id})").format(external_id=self.tag.id)) + return str(_("Delete tag (value={value})").format(value=self.tag.value)) name = "delete" @@ -413,7 +443,9 @@ class WithoutChanges(ImportAction): name = "without_changes" def __str__(self) -> str: - return str(_("No changes needed for tag (external_id={external_id})").format(external_id=self.tag.id)) + if self.tag.id: + return str(_("No changes needed for tag (external_id={external_id})").format(external_id=self.tag.id)) + return str(_("No changes needed for tag (value={value})").format(value=self.tag.value)) @classmethod def applies_for(cls, taxonomy: Taxonomy, tag) -> bool: diff --git a/openedx_tagging/core/tagging/import_export/import_plan.py b/openedx_tagging/core/tagging/import_export/import_plan.py index b5ce4130..af71964e 100644 --- a/openedx_tagging/core/tagging/import_export/import_plan.py +++ b/openedx_tagging/core/tagging/import_export/import_plan.py @@ -136,7 +136,7 @@ def generate_actions( if replace: tags_for_delete = { - tag.external_id: tag for tag in self.taxonomy.tag_set.all() + tag.external_id or tag.id: tag for tag in self.taxonomy.tag_set.all() } for tag in tags: