diff --git a/odyseescraper/management/commands/odyseetag.py b/odyseescraper/management/commands/odyseetag.py new file mode 100644 index 0000000..aeb3830 --- /dev/null +++ b/odyseescraper/management/commands/odyseetag.py @@ -0,0 +1,44 @@ +#! /usr/bin/env python3 +import odyseescraper +import re +from django.core.management.base import BaseCommand, CommandError +from django.db import models + +class Command(BaseCommand): + help = "Runs tagging rules against every release in the database" + + def handle(self, *args, **options): + self.stdout.write('Tagging releases...') + updates = 0 + ignores = 0 + failures = [] + # Yeah I know this scales multidimensionally based on how big the releases list is and the taggingrule list is + # This command is written assuming that's not an issue. Realistically, this should be done at time of release import, + # but having this functionality in place is good for if you have to edit/redact a title or implement a tagging rule + # in light of a surge of new releases or something. + for release in odyseescraper.models.OdyseeRelease.objects.all(): + for taggingrule in odyseescraper.models.TaggingRule.objects.all(): + try: + if taggingrule.title_regex and not re.match(taggingrule.title_regex, release.name): + continue + if taggingrule.description_regex and not re.match(taggingrule.description_regex, release.name): + continue + if taggingrule.tag in release.tags.all(): + ignores += 1 + continue + print('Would tag') + updates += 1 + except Exception as e: + self.stdout.write(self.style.WARNING(f'Failed to apply rule {str(taggingrule.name)}: {e}')) + failures.append({"release": str(release),"error":e}) + if failures: + self.stdout.write(self.style.ERROR('Errors occurred while tagging:')) + for error in failures: + self.stdout.write(self.style.ERROR(f'{error["release"]} - {error["error"]}')) + if updates > 0: + self.stdout.write(self.style.WARNING(f'Applied {updates} tags despite this, {ignores} already satisfied')) + else: + if updates > 0: + self.stdout.write(self.style.SUCCESS(f'Applied {updates} tags to releases, {ignores} already satisfied')) + else: + self.stdout.write(self.style.SUCCESS(f'No new tags applied, {ignores} rule(s) already satisfied'))