1
0
mirror of https://gitlab.crans.org/mediatek/med.git synced 2024-11-30 06:13:02 +00:00
med/media/management/commands/import_cds.py

51 lines
1.5 KiB
Python
Raw Permalink Normal View History

2020-05-22 20:17:17 +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 Author, CD
2020-05-22 20:17:17 +00:00
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('input', nargs='?',
type=FileType('r'),
default=stdin,
help="CD to be imported.")
def handle(self, *args, **options):
file = options["input"]
cds = []
for line in file:
cds.append(line[:-1].split('|', 2))
print("Registering", len(cds), "CDs")
imported = 0
for cd in cds:
if len(cd) != 3:
continue
2020-05-23 14:42:59 +00:00
title = cd[0]
side = cd[1]
2020-05-22 20:17:17 +00:00
authors_str = cd[2].split('|')
2021-10-23 16:31:03 +00:00
authors = [Author.objects.get_or_create(name=author)[0]
2020-05-22 20:17:17 +00:00
for author in authors_str]
2020-05-23 14:42:59 +00:00
cd, created = CD.objects.get_or_create(
2020-05-22 20:17:17 +00:00
title=title,
side_identifier=side,
)
cd.authors.set(authors)
cd.save()
if not created:
self.stderr.write(self.style.WARNING(
"One CD was already imported. Skipping..."))
else:
self.stdout.write(self.style.SUCCESS(
"CD imported"))
imported += 1
self.stdout.write(self.style.SUCCESS(
"{count} CDs imported".format(count=imported)))