Skip to content

Commit

Permalink
fix: Issue when delete all tags on import
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisChV committed Dec 20, 2023
1 parent 1d9c459 commit fb8d161
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
48 changes: 40 additions & 8 deletions openedx_tagging/core/tagging/import_export/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion openedx_tagging/core/tagging/import_export/import_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit fb8d161

Please sign in to comment.