1 # -*- coding: utf-8 -*-
2 # This file is part of EduMed, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from optparse import make_option
10 from django.core.management.base import BaseCommand
13 def makedir(directory):
15 os.makedirs(directory)
17 if e.errno != errno.EEXIST:
21 class Command(BaseCommand):
22 help = 'Extracts attachments from given lessons.'
24 option_list = BaseCommand.option_list + (
25 make_option('--slugs', dest='slugs_path', metavar="PATH", default=None,
26 help='PATH to file with lesson slugs.'),
29 def handle(self, **options):
30 from catalogue.models import Lesson
32 lessons = Lesson.objects.order_by('slug')
33 if options.get('slugs_path'):
34 slugs = [line.strip() for line in open(options.get('slugs_path')) if line.strip()]
35 lessons = lessons.filter(slug__in=slugs)
37 for lesson in lessons:
38 makedir('materialy/%s' % lesson.slug)
39 for attachment in lesson.attachment_set.all():
40 shutil.copy(attachment.file.path, 'materialy/%s/%s.%s' % (lesson.slug, attachment.slug, attachment.ext))