From bed5912f545a3e7d731ce439cb57c34063a1be2f Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 22 May 2020 21:37:02 +0200 Subject: [PATCH] Cat ISBN list to future medias --- media/management/commands/import_isbn.py | 94 +++++++++++++++++++++++ media/migrations/0033_futuremedia_type.py | 19 +++++ media/models.py | 10 +++ 3 files changed, 123 insertions(+) create mode 100644 media/management/commands/import_isbn.py create mode 100644 media/migrations/0033_futuremedia_type.py diff --git a/media/management/commands/import_isbn.py b/media/management/commands/import_isbn.py new file mode 100644 index 0000000..1a95a95 --- /dev/null +++ b/media/management/commands/import_isbn.py @@ -0,0 +1,94 @@ +from argparse import FileType +from sys import stdin + +from django.core.exceptions import ValidationError +from django.core.management import BaseCommand + +from media.models import BD, FutureMedia, Manga, Roman +from media.validators import isbn_validator + + +class Command(BaseCommand): + def add_arguments(self, parser): + parser.add_argument('--media-type', + type=str, + default='bd', + choices=[ + 'bd', + 'manga', + 'roman', + ], + help="Type of media to be " + "imported.") + parser.add_argument('input', nargs='?', + type=FileType('r'), + default=stdin, + help="ISBN to be imported.") + + def handle(self, *args, **options): + print(options) + type_str = options["media_type"] + + media_classes = [BD, Manga, Roman, FutureMedia] + + file = options["input"] + isbns = [] + for line in file: + isbns.append(line[:-1]) + + print("Registering", len(isbns), "ISBN") + + imported = 0 + not_imported = [] + + for isbn in isbns: + if not isbn: + continue + + try: + if not isbn_validator(isbn): + raise ValidationError( + "This ISBN is invalid for an unknown reason") + except ValidationError as e: + self.stderr.write(self.style.ERROR( + "The following ISBN is invalid:" + " {isbn}, reason: {reason}. Ignoring...".format( + isbn=isbn, reason=e.message))) + + isbn_exists = False + for cl in media_classes: + if cl.objects.filter(isbn=isbn).exists(): + isbn_exists = True + medium = cl.objects.get(isbn=isbn) + self.stderr.write(self.style.WARNING( + ("Warning: ISBN {isbn} already exists, and is " + + "registered as type {type}: {name}. Ignoring...") + .format(isbn=isbn, + name=str(medium), + type=str(cl._meta.verbose_name)))) + not_imported.append(medium) + break + + if isbn_exists: + continue + + FutureMedia.objects.create(isbn=isbn, type=type_str) + self.stdout.write(self.style.SUCCESS("ISBN {isbn} imported" + .format(isbn=isbn))) + imported += 1 + + self.stdout.write(self.style.SUCCESS("{count} media imported" + .format(count=imported))) + + with open('not_imported_media.csv', 'w') as f: + f.write("isbn|type|title\n") + for medium in not_imported: + if not hasattr(medium, 'title') or not medium.title: + medium.title = '' + f.write(medium.isbn + "|" + + str(medium._meta.verbose_name) + + "|" + medium.title + "\n") + + self.stderr.write(self.style.WARNING(("{count} media already " + + "imported").format( + count=len(not_imported)))) diff --git a/media/migrations/0033_futuremedia_type.py b/media/migrations/0033_futuremedia_type.py new file mode 100644 index 0000000..b963e06 --- /dev/null +++ b/media/migrations/0033_futuremedia_type.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.10 on 2020-05-22 19:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('media', '0032_auto_20200522_2107'), + ] + + operations = [ + migrations.AddField( + model_name='futuremedia', + name='type', + field=models.CharField(choices=[('bd', 'BD'), ('manga', 'Manga'), ('roman', 'Roman')], default='bd', max_length=8, verbose_name='type'), + preserve_default=False, + ), + ] diff --git a/media/models.py b/media/models.py index ade119c..428e601 100644 --- a/media/models.py +++ b/media/models.py @@ -266,6 +266,16 @@ class FutureMedia(models.Model): null=True, ) + type = models.CharField( + _('type'), + choices=[ + ('bd', _('BD')), + ('manga', _('Manga')), + ('roman', _('Roman')), + ], + max_length=8, + ) + class Meta: verbose_name = _("future medium") verbose_name_plural = _("future media")