47930ecf4d574783c45eca6213b6090a54cba4f8
[edumed.git] / catalogue / management / commands / repackage.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 os
6 from optparse import make_option
7 from django.conf import settings
8 from django.core.management.base import BaseCommand
9 from catalogue.models import Section
10 import zipfile
11
12
13 class Command(BaseCommand):
14     option_list = BaseCommand.option_list + (
15         make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
16             help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
17     )
18     help = 'Rebuilds downloadable packages.'
19
20     def build_package(self, zippath, student, verbose):
21         with open(zippath, 'w') as outf:
22             zipf = zipfile.ZipFile(outf, 'w', zipfile.ZIP_STORED)
23             
24             for si, section in enumerate(Section.objects.all()):
25                 li = 1
26                 li_adv = 1
27                 for lesson in section.lesson_set.all():
28                     if lesson.type == 'course':
29                         if lesson.level.slug == "liceum":
30                             ind = li_adv
31                             li_adv += 1
32                             postfix = " (zaawansowane)"
33                         else:
34                             ind = li
35                             li += 1
36                             postfix = ""
37                         prefix = "%d_%s%s/%02d_%s/" % (
38                                 si, section.slug, postfix,
39                                 ind,
40                                 lesson.slug,
41                             )
42                         li += 1
43                     elif lesson.type == 'synthetic':
44                         prefix = "%d_%s%s/synteza_%s/" % (
45                                 si, section.slug, postfix, lesson.slug)
46                     else:
47                         prefix = "%d_%s%s/%s/" % (
48                                 si, section.slug, postfix, lesson.slug)
49                     lesson.add_to_zip(zipf, student, prefix)
50             zipf.close()
51
52     def handle(self, **options):
53         verbose = options.get('verbose')
54
55         self.build_package(
56             os.path.join(settings.MEDIA_ROOT, settings.CATALOGUE_PACKAGE), 
57             False, verbose)
58         self.build_package(
59             os.path.join(settings.MEDIA_ROOT, settings.CATALOGUE_PACKAGE_STUDENT),
60             True, verbose)