2020-05-24 14:01:05 +00:00
|
|
|
from argparse import FileType
|
|
|
|
from sys import stdin
|
|
|
|
|
|
|
|
from django.core.management import BaseCommand
|
2020-09-28 08:51:21 +00:00
|
|
|
from media.forms import generate_side_identifier
|
2021-10-23 16:31:03 +00:00
|
|
|
from media.models import Novel, Author
|
2020-05-24 14:01:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
parser.add_argument('input', nargs='?',
|
|
|
|
type=FileType('r'),
|
|
|
|
default=stdin,
|
|
|
|
help="Revues to be imported.")
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
file = options["input"]
|
|
|
|
romans = []
|
|
|
|
for line in file:
|
|
|
|
romans.append(line[:-1].split('|'))
|
|
|
|
|
|
|
|
print("Registering", len(romans), "romans")
|
|
|
|
|
|
|
|
imported = 0
|
|
|
|
|
|
|
|
for book in romans:
|
|
|
|
if len(book) != 2:
|
|
|
|
continue
|
|
|
|
|
|
|
|
title = book[1]
|
2021-10-23 16:31:03 +00:00
|
|
|
authors = [Author.objects.get_or_create(name=n)[0]
|
2020-05-24 14:01:05 +00:00
|
|
|
for n in book[0].split(';')]
|
2020-09-28 08:51:21 +00:00
|
|
|
side_identifier = generate_side_identifier(title, authors)
|
2021-10-23 16:31:03 +00:00
|
|
|
roman = Novel.objects.create(
|
2020-05-24 14:01:05 +00:00
|
|
|
title=title,
|
|
|
|
side_identifier=side_identifier,
|
|
|
|
)
|
|
|
|
roman.authors.set(authors)
|
|
|
|
roman.save()
|
|
|
|
|
|
|
|
self.stdout.write(self.style.SUCCESS(
|
|
|
|
"Roman imported"))
|
|
|
|
imported += 1
|
|
|
|
|
|
|
|
self.stdout.write(self.style.SUCCESS(
|
|
|
|
"{count} romans imported".format(count=imported)))
|