2020-09-28 08:51:21 +00:00
|
|
|
from django.core.management import BaseCommand
|
|
|
|
from django.db import transaction
|
|
|
|
from media.forms import generate_side_identifier
|
2021-10-23 16:31:03 +00:00
|
|
|
from media.models import Comic, Manga, Novel
|
2020-09-28 08:51:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
parser.add_argument('--type', '-t',
|
|
|
|
type=str,
|
|
|
|
default='bd',
|
|
|
|
choices=['bd', 'manga', 'roman'],
|
2020-12-28 22:12:27 +00:00
|
|
|
help="Type of medium where the "
|
|
|
|
"sides need to be regenerated.")
|
2020-10-02 14:43:04 +00:00
|
|
|
parser.add_argument('--noninteractivemode', '-ni', action="store_true",
|
2020-12-28 22:12:27 +00:00
|
|
|
help="Disable the interaction mode and replace "
|
|
|
|
"existing side identifiers.")
|
2020-10-02 14:43:04 +00:00
|
|
|
parser.add_argument('--no-commit', '-nc', action="store_true",
|
2020-12-28 22:12:27 +00:00
|
|
|
help="Only show modifications, don't commit "
|
|
|
|
"them to database.")
|
2020-09-28 08:51:21 +00:00
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
t = options["type"]
|
|
|
|
medium_class = None
|
|
|
|
if t == "bd":
|
2021-10-23 16:31:03 +00:00
|
|
|
medium_class = Comic
|
2020-09-28 08:51:21 +00:00
|
|
|
elif t == "manga":
|
|
|
|
medium_class = Manga
|
|
|
|
elif t == "roman":
|
2021-10-23 16:31:03 +00:00
|
|
|
medium_class = Novel
|
2020-09-28 08:51:21 +00:00
|
|
|
|
|
|
|
interactive_mode = not options["noninteractivemode"]
|
|
|
|
|
|
|
|
replaced = 0
|
|
|
|
|
|
|
|
for obj in medium_class.objects.all():
|
|
|
|
current_side_identifier = obj.side_identifier
|
|
|
|
if not obj.authors.all():
|
|
|
|
self.stdout.write(str(obj))
|
|
|
|
subtitle = obj.subtitle if hasattr(obj, "subtitle") else None
|
2020-12-28 22:12:27 +00:00
|
|
|
generated_side_identifier = generate_side_identifier(
|
|
|
|
obj.title, obj.authors.all(), subtitle)
|
2020-09-28 08:51:21 +00:00
|
|
|
if current_side_identifier != generated_side_identifier:
|
|
|
|
answer = 'y'
|
|
|
|
if interactive_mode:
|
|
|
|
answer = ''
|
|
|
|
while answer != 'y' and answer != 'n':
|
2020-12-28 22:12:27 +00:00
|
|
|
answer = input(f"For medium {obj}, current side: "
|
|
|
|
f"{current_side_identifier}, "
|
|
|
|
f"generated side: "
|
|
|
|
f"{generated_side_identifier}, "
|
|
|
|
f"would you like to replace ? [y/n]")\
|
|
|
|
.lower()[0]
|
2020-09-28 08:51:21 +00:00
|
|
|
if answer == 'y':
|
2020-12-28 22:12:27 +00:00
|
|
|
self.stdout.write(self.style.WARNING(
|
|
|
|
f"Replace side of {obj} from {current_side_identifier}"
|
|
|
|
f" to {generated_side_identifier}..."))
|
2020-09-28 08:51:21 +00:00
|
|
|
obj.side_identifier = generated_side_identifier
|
2020-10-02 14:43:04 +00:00
|
|
|
if not options["no_commit"]:
|
|
|
|
obj.save()
|
2020-09-28 08:51:21 +00:00
|
|
|
replaced += 1
|
|
|
|
|
|
|
|
if replaced:
|
2020-12-28 22:12:27 +00:00
|
|
|
self.stdout.write(self.style.SUCCESS(
|
|
|
|
f"{replaced} side identifiers were replaced."))
|
2020-09-28 08:51:21 +00:00
|
|
|
else:
|
|
|
|
self.stdout.write(self.style.WARNING("Nothing changed."))
|