1
0
mirror of https://gitlab.crans.org/mediatek/med.git synced 2024-12-29 06:22:23 +00:00
med/media/management/commands/import_vinyles.py

59 lines
1.9 KiB
Python
Raw Normal View History

2020-05-22 20:15:37 +00:00
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,
2020-05-22 20:17:17 +00:00
help="Vinyle to be imported.")
2020-05-22 20:15:37 +00:00
2020-05-23 12:23:39 +00:00
parser.add_argument('--rpm',
type=int,
default=45,
help="RPM of the imported vinyles.")
2020-05-22 20:15:37 +00:00
def handle(self, *args, **options):
2020-05-23 12:23:39 +00:00
rpm = options["rpm"]
2020-05-22 20:15:37 +00:00
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]
2020-05-24 15:47:37 +00:00
title = vinyle[1 if rpm == 33 else 2]
authors_str = vinyle[2 if rpm == 33 else 1]\
.split('|' if rpm == 33 else ';')
2020-05-22 20:15:37 +00:00
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,
2020-05-24 15:47:37 +00:00
rpm=rpm,
2020-05-22 20:15:37 +00:00
)
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)))