Skip to content

Commit

Permalink
style: Rename exceptions and running black
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisChV committed Jul 26, 2023
1 parent 09de752 commit b056500
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 163 deletions.
142 changes: 67 additions & 75 deletions openedx_tagging/core/tagging/import_export/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
from django.utils.translation import gettext_lazy as _

from ..models import Taxonomy, Tag
from .exceptions import ActionError, ActionConflict
from .exceptions import ImportActionError, ImportActionConflict


class ImportAction:
"""
Base class to create actions
Base class to create actions
"""

name = 'import_action'
name = "import_action"

def __init__(self, taxonomy: Taxonomy, tag, index: int):
self.taxonomy = taxonomy
Expand All @@ -23,7 +23,7 @@ def __init__(self, taxonomy: Taxonomy, tag, index: int):

def __repr__(self):
return _(f"Action {self.name} (index={self.index},id={self.tag.id})")

def __str__(self):
return str(self.__repr__)

Expand All @@ -36,7 +36,7 @@ def valid_for(cls, taxonomy: Taxonomy, tag) -> bool:
"""
raise NotImplementedError

def validate(self, indexed_actions) -> List[ActionError]:
def validate(self, indexed_actions) -> List[ImportActionError]:
"""
Implement this to find inconsistencies with tags in the
database or with previous actions.
Expand All @@ -59,10 +59,10 @@ def _search_action(
for action in indexed_actions[action_name]:
if search_value == getattr(action.tag, attr):
return action

return None

def _validate_parent(self, indexed_actions) -> ActionError:
def _validate_parent(self, indexed_actions) -> ImportActionError:
"""
Validates if the parent is created
"""
Expand All @@ -71,14 +71,16 @@ def _validate_parent(self, indexed_actions) -> ActionError:
self.taxonomy.tag_set.get(external_id=self.tag.parent_id)
except Tag.DoesNotExist:
# Or if the parent is created on previous actions
if not self._search_action(indexed_actions, CreateTag.name, 'id', self.tag.parent_id):
return ActionError(
if not self._search_action(
indexed_actions, CreateTag.name, "id", self.tag.parent_id
):
return ImportActionError(
action=self,
tag_id=self.tag.id,
message=_(
f"Unknown parent tag ({self.tag.parent_id}). "
"You need to add parent before the child in your file."
)
),
)

def _validate_value(self, indexed_actions):
Expand All @@ -89,37 +91,38 @@ def _validate_value(self, indexed_actions):
try:
# Validates if exists a tag with the same value on the Taxonomy
tag = self.taxonomy.tag_set.get(value=self.tag.value)
return ActionError(
return ImportActionError(
action=self,
tag_id=self.tag.id,
message=_(f"Duplicated tag value with tag (id={tag.id}).")
message=_(f"Duplicated tag value with tag (id={tag.id})."),
)
except Tag.DoesNotExist:
# Validates value duplication on create actions
action = self._search_action(
indexed_actions,
CreateTag.name,
'value',
"value",
self.tag.value,
)

if not action:
# Validates value duplication on rename actions
action = self._search_action(
indexed_actions,
RenameTag.name,
'value',
"value",
self.tag.value,
)

if action:
return ActionConflict(
return ImportActionConflict(
action=self,
tag_id=self.tag.id,
conflict_action_index=action.index,
message=_("Duplicated tag value.")
message=_("Duplicated tag value."),
)


class CreateTag(ImportAction):
"""
Action for create a Tag
Expand All @@ -130,18 +133,20 @@ class CreateTag(ImportAction):
- Id duplicates with previous create actions.
- Value duplicates with tags on the database.
- Value duplicates with previous create and rename actions.
- Parent validation. If the parent is in the database or created
- Parent validation. If the parent is in the database or created
in previous actions.
"""

name = 'create'
name = "create"

def __str__(self):
return str(_(
"Create a new tag with values "
f"(external_id={self.tag.id}, value={self.tag.value}, "
f"parent_id={self.tag.parent_id})."
))
return str(
_(
"Create a new tag with values "
f"(external_id={self.tag.id}, value={self.tag.value}, "
f"parent_id={self.tag.parent_id})."
)
)

@classmethod
def valid_for(cls, taxonomy: Taxonomy, tag) -> bool:
Expand All @@ -158,21 +163,16 @@ def _validate_id(self, indexed_actions):
"""
Check for id duplicates in previous create actions
"""
action = self._search_action(
indexed_actions,
self.name,
'id',
self.tag.id
)
action = self._search_action(indexed_actions, self.name, "id", self.tag.id)
if action:
return ActionConflict(
return ImportActionConflict(
action=self,
tag_id=self.tag.id,
conflict_action_index=action.index,
message=_("Duplicated external_id tag.")
message=_("Duplicated external_id tag."),
)

def validate(self, indexed_actions) -> List[ActionError]:
def validate(self, indexed_actions) -> List[ImportActionError]:
"""
Validates the creation action
"""
Expand All @@ -182,7 +182,7 @@ def validate(self, indexed_actions) -> List[ActionError]:
error = self._validate_id(indexed_actions)
if error:
errors.append(error)

# Duplicate value validation
error = self._validate_value(indexed_actions)
if error:
Expand All @@ -193,7 +193,7 @@ def validate(self, indexed_actions) -> List[ActionError]:
error = self._validate_parent(indexed_actions)
if error:
errors.append(error)

return errors

def execute(self):
Expand All @@ -207,23 +207,25 @@ class UpdateParentTag(ImportAction):
Action created if there is a change on the parent
Validations:
- Parent validation. If the parent is in the database
- Parent validation. If the parent is in the database
or created in previous actions.
"""

name = 'update_parent'
name = "update_parent"

def __str__(self):
taxonomy_tag = self.taxonomy.tag_set.get(external_id=self.tag.id)
taxonomy_tag = self.taxonomy.tag_set.get(external_id=self.tag.id)
if not taxonomy_tag.parent:
from_str = _("from empty parent")
else:
from_str = _(f"from parent (external_id={taxonomy_tag.parent.external_id})")

return str(_(
f"Update the parent of tag (id={taxonomy_tag.id}) "
f"{from_str} to parent (external_id={self.tag.parent_id})."
))
return str(
_(
f"Update the parent of tag (id={taxonomy_tag.id}) "
f"{from_str} to parent (external_id={self.tag.parent_id})."
)
)

@classmethod
def valid_for(cls, taxonomy: Taxonomy, tag) -> bool:
Expand All @@ -233,19 +235,13 @@ def valid_for(cls, taxonomy: Taxonomy, tag) -> bool:
try:
taxonomy_tag = taxonomy.tag_set.get(external_id=tag.id)
return (
(
taxonomy_tag.parent is not None
and taxonomy_tag.parent.external_id != tag.parent_id
)
or
(
taxonomy_tag.parent is None and tag.parent_id is not None
)
)
taxonomy_tag.parent is not None
and taxonomy_tag.parent.external_id != tag.parent_id
) or (taxonomy_tag.parent is None and tag.parent_id is not None)
except Tag.DoesNotExist:
return False

def validate(self, indexed_actions) -> List[ActionError]:
def validate(self, indexed_actions) -> List[ImportActionError]:
"""
Validates the update parent action
"""
Expand Down Expand Up @@ -274,14 +270,16 @@ class RenameTag(ImportAction):
- Value duplicates with previous create and rename actions.
"""

name = 'rename'
name = "rename"

def __str__(self):
taxonomy_tag = self.taxonomy.tag_set.get(external_id=self.tag.id)
return str(_(
f"Rename tag value of tag (id={taxonomy_tag.id}) "
f"from '{taxonomy_tag.value}' to '{self.tag.value}'"
))
taxonomy_tag = self.taxonomy.tag_set.get(external_id=self.tag.id)
return str(
_(
f"Rename tag value of tag (id={taxonomy_tag.id}) "
f"from '{taxonomy_tag.value}' to '{self.tag.value}'"
)
)

@classmethod
def valid_for(cls, taxonomy: Taxonomy, tag) -> bool:
Expand All @@ -290,13 +288,11 @@ def valid_for(cls, taxonomy: Taxonomy, tag) -> bool:
"""
try:
taxonomy_tag = taxonomy.tag_set.get(external_id=tag.id)
return (
taxonomy_tag.value != tag.value
)
return taxonomy_tag.value != tag.value
except Tag.DoesNotExist:
return False

def validate(self, indexed_actions) -> List[ActionError]:
def validate(self, indexed_actions) -> List[ImportActionError]:
"""
Validates the rename action
"""
Expand All @@ -323,12 +319,10 @@ class DeleteTag(ImportAction):
"""

def __str__(self):
taxonomy_tag = self.taxonomy.tag_set.get(external_id=self.tag.id)
return str(_(
f"Delete tag (id={taxonomy_tag.id})"
))
taxonomy_tag = self.taxonomy.tag_set.get(external_id=self.tag.id)
return str(_(f"Delete tag (id={taxonomy_tag.id})"))

name = 'delete'
name = "delete"

@classmethod
def valid_for(cls, taxonomy: Taxonomy, tag) -> bool:
Expand All @@ -341,7 +335,7 @@ def valid_for(cls, taxonomy: Taxonomy, tag) -> bool:
except Tag.DoesNotExist:
return False

def validate(self, indexed_actions) -> List[ActionError]:
def validate(self, indexed_actions) -> List[ImportActionError]:
"""
No validations necessary
"""
Expand All @@ -351,19 +345,18 @@ def validate(self, indexed_actions) -> List[ActionError]:
def execute(self):
pass


class WithoutChanges(ImportAction):
"""
Action when there is no change on the Tag
Does not require validations
"""

name = 'without_changes'
name = "without_changes"

def __str__(self):
return str(_(
f"No changes needed for tag (external_id={self.tag.id})"
))
return str(_(f"No changes needed for tag (external_id={self.tag.id})"))

@classmethod
def valid_for(cls, taxonomy: Taxonomy, tag) -> bool:
Expand All @@ -372,8 +365,7 @@ def valid_for(cls, taxonomy: Taxonomy, tag) -> bool:
"""
return False


def validate(self, indexed_actions) -> List[ActionError]:
def validate(self, indexed_actions) -> List[ImportActionError]:
"""
No validations necessary
"""
Expand Down
9 changes: 4 additions & 5 deletions openedx_tagging/core/tagging/import_export/api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from io import BytesIO
from typing import List

from ..models import Taxonomy
from .parsers import get_parser, ParserFormat
from .exceptions import ImportError
from .dsl import TagImportDSL


# TODO This function must be a celery task
def import_tags(
taxonomy: Taxonomy,
Expand All @@ -21,14 +20,14 @@ def import_tags(
# Check if there are errors in the parse
if errors:
return errors

# Generate the actions
dsl = TagImportDSL()
dsl.generate_actions(tags, taxonomy, replace)

# Execute the plan
if execute:
return dsl.execute()

# Or return the plan
return dsl.plan()
Loading

0 comments on commit b056500

Please sign in to comment.