1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
7 from django.contrib.auth.models import User
9 from collections import defaultdict
10 from django.core.management import BaseCommand
12 from catalogue.models import Book
13 from librarian import RDFNS, DCNS
16 'pdf': 'application/pdf',
17 'epub': 'application/epub+zip',
18 'mobi': 'application/x-mobipocket-ebook',
25 r'<dc:relation.hasFormat id="%(format)s" xmlns:dc="http://purl.org/dc/elements/1.1/">%(url)s'
26 r'</dc:relation.hasFormat>',
27 r'<meta refines="#%(format)s" id="%(format)s-id" property="dcterms:identifier">ISBN-%(isbn)s</meta>',
28 r'<meta refines="#%(format)s-id" property="identifier-type">ISBN</meta>',
29 r'<meta refines="#%(format)s" property="dcterms:format">%(content_type)s</meta>',
33 def url_for_format(slug, format):
35 return 'https://wolnelektury.pl/katalog/lektura/%s.html' % slug
37 return 'http://wolnelektury.pl/media/book/%(format)s/%(slug)s.%(format)s' % {'slug': slug, 'format': format}
40 class Command(BaseCommand):
43 def add_arguments(self, parser):
45 '-u', '--username', dest='username', metavar='USER',
46 help='Assign commits to this user (required, preferably yourself).')
48 def handle(self, csv_file, **options):
49 username = options.get('username')
52 user = User.objects.get(username=username)
54 print('Please provide a username.')
57 csvfile = open(csv_file, 'rb')
58 isbn_lists = defaultdict(list)
59 for slug, format, isbn in csv.reader(csvfile, delimiter=','):
60 isbn_lists[slug].append((format, isbn))
63 for slug, isbn_list in isbn_lists.iteritems():
64 print('processing %s' % slug)
65 book = Book.objects.get(dc_slug=slug)
66 chunk = book.chunk_set.first()
68 src = old_head.materialize()
69 tree = etree.fromstring(src)
70 isbn_node = tree.find('.//' + DCNS("relation.hasFormat"))
71 if isbn_node is not None:
72 print('%s already contains ISBN metadata, skipping' % slug)
74 desc = tree.find(".//" + RDFNS("Description"))
75 for format, isbn in isbn_list:
76 for template in ISBN_TEMPLATES:
77 isbn_xml = template % {
80 'content_type': CONTENT_TYPES[format],
81 'url': url_for_format(slug, format),
83 element = etree.XML(isbn_xml)
86 new_head = chunk.commit(
87 etree.tostring(tree, encoding='unicode'),
89 description='automatyczne dodanie isbn'
91 print('committed %s' % slug)
92 if old_head.publishable:
93 new_head.set_publishable(True)
95 print('Warning: %s not publishable' % slug)