1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
7 from django.core.management.base import BaseCommand
8 from django.db import transaction
10 from catalogue.models import Book, BookMedia
11 from catalogue.utils import ExistingFile
14 class Command(BaseCommand):
15 help = "Saves uploaded media with a given book and a given name. " \
16 "If media has a source SHA1 info - matching media is replaced."
17 args = 'path slug name'
20 def handle(self, *args, **options):
21 path, slug, name, part_name, index, parts_count = args
23 parts_count = int(parts_count)
25 book = Book.objects.get(slug=slug)
27 root, ext = os.path.splitext(path)
34 source_sha1 = BookMedia.read_source_sha1(path, ext)
35 print "Source file SHA1:", source_sha1
38 bm = book.media.get(type=ext, source_sha1=source_sha1)
39 print "Replacing media: %s (%s)" % (bm.name.encode('utf-8'), ext)
40 except (AssertionError, BookMedia.DoesNotExist):
41 bm = BookMedia(book=book, type=ext)
42 print "Creating new media"
44 bm.part_name = part_name
46 bm.file.save(None, ExistingFile(path))
47 bm.save(parts_count=parts_count)