1 # -*- coding: utf-8 -*-
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.
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
14 from django.core.management import BaseCommand
16 from catalogue.models import Book
17 from librarian import DCNS
19 DC_TEMPLATE = r'<dc:subject.curriculum.new xmlns:dc="http://purl.org/dc/elements/1.1/">%s</dc:subject.curriculum.new>'
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"),
29 '-u', '--username', dest='username', metavar='USER',
30 help='Assign commits to this user (required, preferably yourself).'),
34 def handle(self, json_file, **options):
35 username = options.get('username')
38 user = User.objects.get(username=username)
40 print 'Please provide a username.'
43 data = json.load(open(json_file, 'rb'))
45 for slug, ident_list in data:
46 print 'processing %s' % slug
48 book = Book.objects.get(slug=slug)
49 except Book.DoesNotExist:
50 print 'WARNING %s not found!!!' % slug
52 chunk = book.chunk_set.all()[0]
54 src = old_head.materialize()
55 tree = etree.fromstring(force_str(src.replace(' ', 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
60 desc = tree.find(".//metadata")
61 for ident in ident_list:
62 dc_xml = DC_TEMPLATE % ident
63 element = etree.XML(dc_xml)
66 new_head = chunk.commit(
67 etree.tostring(tree, encoding=unicode),
69 description='automatyczne dodanie nowej podstawy programowej'
71 print 'committed %s' % slug
72 if old_head.publishable:
73 new_head.set_publishable(True)
75 print 'Warning: %s not publishable' % slug