Skip to content

Commit

Permalink
470 - Update Command to fit service changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Trafire committed Jul 24, 2024
1 parent 0616dfb commit fd48913
Showing 1 changed file with 3 additions and 30 deletions.
33 changes: 3 additions & 30 deletions taggit/management/commands/merge_duplicate_tags.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
from django.apps import apps
from django.core.management.base import BaseCommand, CommandError

from taggit.services.tag_merging import TagMergingService


class Command(BaseCommand):
help = "Merges duplicate tags, with options to specify tag and tagged item models."
help = "Merges Tags with the same name but different case."

def add_arguments(self, parser):
parser.add_argument(
"tag_name", type=str, help="The name of the tag to merge duplicates for."
)
parser.add_argument(
"--tag-model", type=str, help="The tag model to use.", default="taggit.Tag"
)

def handle(self, *args, **options):
"""
Expand All @@ -23,22 +19,13 @@ def handle(self, *args, **options):
*args: Variable length argument list.
**options: Arbitrary keyword arguments. Expected to contain:
- tag_name (str): The name of the tag to merge duplicates for.
- tag_model (str): The Django model path for the tag model.
Raises:
CommandError: If the specified tag does not exist or if an unspecified error occurs during
"""
tag_name = options["tag_name"]
tag_model_path = options["tag_model"]

# Dynamic import of models
TagModel = self.dynamic_import(tag_model_path)

# Initialize the TagMergingService with the specified models

service = TagMergingService(
tag_model=TagModel,
)
service = TagMergingService()

try:
service.merge_case_insensitive_tags(tag_name)
Expand All @@ -47,19 +34,5 @@ def handle(self, *args, **options):
f'Successfully merged duplicates of the tag "{tag_name}"'
)
)
except TagModel.DoesNotExist:
raise CommandError(f'Tag "{tag_name}" does not exist.')
except Exception as e:
raise CommandError(f"Error occurred while merging tags: {e}")

@staticmethod
def dynamic_import(app_label_model_name: str):
"""
Dynamically imports a Django model given its 'app_label.model_name' string.
"""
try:
return apps.get_model(app_label_model_name)
except LookupError:
raise ImportError(
f"No model found with app_label.model_name '{app_label_model_name}'"
)

0 comments on commit fd48913

Please sign in to comment.