1
0
mirror of https://gitlab.crans.org/mediatek/med.git synced 2024-11-27 23:33:03 +00:00
med/media/management/commands/import_future_media.py

53 lines
2.0 KiB
Python
Raw Normal View History

2020-10-03 13:05:05 +00:00
from random import random
2020-05-22 19:49:09 +00:00
from time import sleep
from django.core.management import BaseCommand
from media.forms import MediaAdminForm
2021-10-23 16:31:03 +00:00
from media.models import Comic, FutureMedium, Manga, Novel
2020-05-22 19:49:09 +00:00
class Command(BaseCommand):
def handle(self, *args, **options):
2021-10-23 16:31:03 +00:00
for future_medium in FutureMedium.objects.all():
2020-05-22 19:49:09 +00:00
isbn = future_medium.isbn
type_str = future_medium.type
if type_str == 'bd':
2021-10-23 16:31:03 +00:00
cl = Comic
2020-05-22 19:49:09 +00:00
elif type_str == 'manga':
cl = Manga
elif type_str == 'roman':
2021-10-23 16:31:03 +00:00
cl = Novel
2020-05-22 19:49:09 +00:00
else:
self.stderr.write(self.style.WARNING(
"Unknown medium type: {type}. Ignoring..."
.format(type=type_str)))
continue
if cl.objects.filter(isbn=isbn).exists():
2020-12-28 22:12:27 +00:00
self.stderr.write(self.style.WARNING(
f"ISBN {isbn} for type {type_str} already exists, "
f"remove it"))
2020-10-02 15:12:02 +00:00
future_medium.delete()
continue
2020-05-22 19:49:09 +00:00
form = MediaAdminForm(instance=cl(),
data={"isbn": isbn, "_isbn": True, })
# Don't DDOS any website
2020-10-03 13:05:05 +00:00
sleep(5 + (4 * random() - 1))
2020-05-22 19:49:09 +00:00
try:
form.full_clean()
2020-12-28 22:12:27 +00:00
if hasattr(form.instance, "subtitle") and \
not form.instance.subtitle:
2020-10-02 15:12:02 +00:00
form.instance.subtitle = ""
2020-05-22 19:49:09 +00:00
form.save()
future_medium.delete()
self.stdout.write(self.style.SUCCESS(
"Medium with ISBN {isbn} successfully imported"
.format(isbn=isbn)))
2020-10-03 13:05:05 +00:00
except Exception as e:
2020-05-22 19:49:09 +00:00
self.stderr.write(self.style.WARNING(
"An error occured while importing ISBN {isbn}: {error}"
2020-12-28 22:12:27 +00:00
.format(isbn=isbn,
error=str(e.__class__) + "(" + str(e) + ")")))