increase max length of attachment file path
[edumed.git] / catalogue / management / commands / extract_attachments.py
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.
4 #
5 import errno
6 from optparse import make_option
7 import os
8 import shutil
9
10 from django.core.management.base import BaseCommand
11
12
13 def makedir(directory):
14     try:
15         os.makedirs(directory)
16     except OSError as e:
17         if e.errno != errno.EEXIST:
18             raise
19
20
21 class Command(BaseCommand):
22     help = 'Extracts attachments from given lessons.'
23
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.'),
27     )
28
29     def handle(self, **options):
30         from catalogue.models import Lesson
31
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)
36
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))
41