Import future media
This commit is contained in:
parent
bed5912f54
commit
50f3cf39c1
|
@ -9,7 +9,8 @@ from reversion.admin import VersionAdmin
|
|||
|
||||
from med.admin import admin_site
|
||||
from .forms import MediaAdminForm
|
||||
from .models import Auteur, BD, CD, Emprunt, FutureMedia, Jeu, Manga, Roman, Vinyle
|
||||
from .models import Auteur, BD, CD, Emprunt, FutureMedia, Jeu, Manga,\
|
||||
Roman, Vinyle
|
||||
|
||||
|
||||
class AuteurAdmin(VersionAdmin):
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
from time import sleep
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.management import BaseCommand
|
||||
|
||||
from media.forms import MediaAdminForm
|
||||
from media.models import BD, FutureMedia, Manga, Roman
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
for future_medium in FutureMedia.objects.all():
|
||||
isbn = future_medium.isbn
|
||||
type_str = future_medium.type
|
||||
if type_str == 'bd':
|
||||
cl = BD
|
||||
elif type_str == 'manga':
|
||||
cl = Manga
|
||||
elif type_str == 'roman':
|
||||
cl = Roman
|
||||
else:
|
||||
self.stderr.write(self.style.WARNING(
|
||||
"Unknown medium type: {type}. Ignoring..."
|
||||
.format(type=type_str)))
|
||||
continue
|
||||
|
||||
if cl.objects.filter(isbn=isbn).exists():
|
||||
self.stderr.write(self.style.WARNING(
|
||||
"ISBN {isbn} already exists".format(isbn=isbn)
|
||||
))
|
||||
|
||||
form = MediaAdminForm(instance=cl(),
|
||||
data={"isbn": isbn, "_isbn": True, })
|
||||
# Don't DDOS any website
|
||||
sleep(5)
|
||||
|
||||
try:
|
||||
form.full_clean()
|
||||
form.save()
|
||||
future_medium.delete()
|
||||
self.stdout.write(self.style.SUCCESS(
|
||||
"Medium with ISBN {isbn} successfully imported"
|
||||
.format(isbn=isbn)))
|
||||
except ValidationError as e:
|
||||
self.stderr.write(self.style.WARNING(
|
||||
"An error occured while importing ISBN {isbn}: {error}"
|
||||
.format(isbn=isbn, error=e.message)))
|
Loading…
Reference in New Issue