med/media/management/commands/import_revues.py

59 lines
1.7 KiB
Python
Raw Normal View History

2020-05-24 13:37:57 +00:00
from argparse import FileType
from sys import stdin
from django.core.management import BaseCommand
2021-10-23 16:31:03 +00:00
from media.models import Review
2020-05-24 13:37:57 +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"]
revues = []
for line in file:
revues.append(line[:-1].split('|'))
print("Registering", len(revues), "revues")
imported = 0
for revue in revues:
if len(revue) != 5:
continue
title = revue[0]
number = revue[1]
day = revue[2]
if not day:
day = None
month = revue[3]
if not month:
month = None
year = revue[4]
if not year:
year = None
2021-10-23 16:31:03 +00:00
revue, created = Review.objects.get_or_create(
2020-05-24 13:37:57 +00:00
title=title,
number=number.replace('*', ''),
year=year,
month=month,
day=day,
double=number.endswith('*'),
)
if not created:
self.stderr.write(self.style.WARNING(
"One revue was already imported. Skipping..."))
else:
self.stdout.write(self.style.SUCCESS(
"Revue imported"))
imported += 1
self.stdout.write(self.style.SUCCESS(
"{count} revues imported".format(count=imported)))