Import vinyles

This commit is contained in:
Yohann D'ANELLO 2020-05-22 22:15:37 +02:00
parent 50f3cf39c1
commit d88fccb51d
3 changed files with 53 additions and 3 deletions

View File

@ -41,7 +41,7 @@ class Command(BaseCommand):
self.stdout.write(self.style.SUCCESS(
"Medium with ISBN {isbn} successfully imported"
.format(isbn=isbn)))
except ValidationError as e:
except (ValidationError, ValueError) as e:
self.stderr.write(self.style.WARNING(
"An error occured while importing ISBN {isbn}: {error}"
.format(isbn=isbn, error=e.message)))
.format(isbn=isbn, error=str(e))))

View File

@ -26,7 +26,6 @@ class Command(BaseCommand):
help="ISBN to be imported.")
def handle(self, *args, **options):
print(options)
type_str = options["media_type"]
media_classes = [BD, Manga, Roman, FutureMedia]

View File

@ -0,0 +1,51 @@
from argparse import FileType
from sys import stdin
from django.core.management import BaseCommand
from media.models import Auteur, Vinyle
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('input', nargs='?',
type=FileType('r'),
default=stdin,
help="ISBN to be imported.")
def handle(self, *args, **options):
file = options["input"]
vinyles = []
for line in file:
vinyles.append(line[:-1].split('|', 2))
print("Registering", len(vinyles), "vinyles")
imported = 0
for vinyle in vinyles:
if len(vinyle) != 3:
continue
side = vinyle[0]
title = vinyle[1]
authors_str = vinyle[2].split('|')
authors = [Auteur.objects.get_or_create(name=author)[0]
for author in authors_str]
vinyle, created = Vinyle.objects.get_or_create(
title=title,
side_identifier=side,
)
vinyle.authors.set(authors)
vinyle.save()
if not created:
self.stderr.write(self.style.WARNING(
"One vinyle was already imported. Skipping..."))
else:
self.stdout.write(self.style.SUCCESS(
"Vinyle imported"))
imported += 1
self.stdout.write(self.style.SUCCESS(
"{count} vinyles imported".format(count=imported)))