minor fix
[redakcja.git] / apps / catalogue / management / commands / add_curriculum.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 import json
7
8 import sys
9 from django.contrib.auth.models import User
10 from django.utils.encoding import force_str
11 from lxml import etree
12 from optparse import make_option
13
14 from django.core.management import BaseCommand
15
16 from catalogue.models import Book
17 from librarian import DCNS
18
19 DC_TEMPLATE = r'<dc:subject.curriculum.new xmlns:dc="http://purl.org/dc/elements/1.1/">%s</dc:subject.curriculum.new>'
20
21
22 class Command(BaseCommand):
23     option_list = BaseCommand.option_list + (
24         # make_option('-q', '--quiet', action='store_false', dest='verbose',
25         #     default=True, help='Less output'),
26         # make_option('-d', '--dry-run', action='store_true', dest='dry_run',
27         #     default=False, help="Don't actually touch anything"),
28         make_option(
29             '-u', '--username', dest='username', metavar='USER',
30             help='Assign commits to this user (required, preferably yourself).'),
31     )
32     args = 'json_file'
33
34     def handle(self, json_file, **options):
35         username = options.get('username')
36
37         if username:
38             user = User.objects.get(username=username)
39         else:
40             print 'Please provide a username.'
41             sys.exit(1)
42
43         data = json.load(open(json_file, 'rb'))
44
45         for slug, ident_list in data:
46             print 'processing %s' % slug
47             try:
48                 book = Book.objects.get(slug=slug)
49             except Book.DoesNotExist:
50                 print 'WARNING %s not found!!!' % slug
51                 continue
52             chunk = book.chunk_set.all()[0]
53             old_head = chunk.head
54             src = old_head.materialize()
55             tree = etree.fromstring(force_str(src.replace('&nbsp;', u'\xa0')))
56             curr_node = tree.find('.//' + DCNS("subject.curriculum.new"))
57             if curr_node is not None:
58                 print '%s already contains new curriculum metadata, skipping' % slug
59                 continue
60             desc = tree.find(".//metadata")
61             for ident in ident_list:
62                 dc_xml = DC_TEMPLATE % ident
63                 element = etree.XML(dc_xml)
64                 element.tail = '\n'
65                 desc.append(element)
66             new_head = chunk.commit(
67                 etree.tostring(tree, encoding=unicode),
68                 author=user,
69                 description='automatyczne dodanie nowej podstawy programowej'
70             )
71             print 'committed %s' % slug
72             if old_head.publishable:
73                 new_head.set_publishable(True)
74             else:
75                 print 'Warning: %s not publishable' % slug