med/media/management/commands/split_media_types.py

62 lines
2.3 KiB
Python
Raw Normal View History

2020-05-22 16:04:41 +00:00
from time import sleep
2020-05-21 14:56:41 +00:00
from django.core.management import BaseCommand
from media.forms import MediaAdminForm
2020-05-22 16:04:41 +00:00
from media.models import BD, Manga
2020-05-21 14:56:41 +00:00
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('--view-only', action="store_true",
help="Display only modifications. "
+ "Only useful for debug.")
def handle(self, *args, **options):
converted = 0
2020-05-22 16:04:41 +00:00
for media in BD.objects.all():
if media.pk < 3400:
continue
# We sleep 5 seconds to avoid a ban from Bedetheque
sleep(5)
2020-05-21 14:56:41 +00:00
self.stdout.write(str(media))
2020-05-21 15:07:50 +00:00
form = MediaAdminForm(instance=media,
data={"isbn": media.isbn, "_isbn": True, })
2020-05-21 14:56:41 +00:00
form.full_clean()
2020-05-21 15:07:50 +00:00
if "format" not in form.cleaned_data:
self.stdout.write("Format not specified."
" Assume it is a comic strip.")
2020-05-21 14:56:41 +00:00
continue
format = form.cleaned_data["format"]
self.stdout.write("Format: {}".format(format))
if not options["view_only"]:
if format == "manga":
2020-05-21 15:07:50 +00:00
self.stdout.write(self.style.WARNING(
"This media is a manga. "
"Transfer it into a new object..."))
2020-05-21 14:56:41 +00:00
manga = Manga.objects.create(
isbn=media.isbn,
title=media.title,
subtitle=media.subtitle,
external_url=media.external_url,
side_identifier=media.side_identifier,
number_of_pages=media.number_of_pages,
publish_date=media.publish_date,
)
manga.authors.set(media.authors.all())
manga.save()
2020-05-21 15:07:50 +00:00
self.stdout.write(self.style.SUCCESS(
"Manga successfully saved. Deleting old medium..."))
2020-05-21 14:56:41 +00:00
media.delete()
self.stdout.write(self.style.SUCCESS("Medium deleted"))
converted += 1
2020-05-21 15:07:50 +00:00
self.stdout.write(self.style.SUCCESS(
"Successfully saved {:d} mangas".format(converted)))