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.
6 from datetime import date
9 from django.core.management import BaseCommand
11 from documents.models import Book
12 from librarian import RDFNS, DCNS
15 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
16 <rdf:Description rdf:about="http://redakcja.wolnelektury.pl/documents/book/%(slug)s/">
24 DC_TEMPLATE = '<dc:%(tag)s xml:lang="pl" xmlns:dc="http://purl.org/dc/elements/1.1/">%(value)s</dc:%(tag)s>'
30 'contributor.translator',
32 'contributor.technical_editor',
33 'contributor.funding',
53 IDENTIFIER_PREFIX = 'http://wolnelektury.pl/katalog/lektura/'
56 def dc_desc_element(book):
57 xml = book.materialize()
58 tree = etree.fromstring(xml)
59 return tree.find(".//" + RDFNS("Description"))
62 def distinct_dc_values(tag, desc_elements):
64 for desc in desc_elements:
65 values.update(elem.text for elem in desc.findall(DCNS(tag)))
69 class Command(BaseCommand):
72 def handle(self, slug, **options):
73 children_slugs = [line.strip() for line in sys.stdin]
74 children = Book.objects.filter(dc_slug__in=children_slugs)
75 desc_elements = [dc_desc_element(child) for child in children]
76 title = u'Utwory wybrane'
79 'relation.hasPart': [IDENTIFIER_PREFIX + child_slug for child_slug in children_slugs],
80 'identifier.url': IDENTIFIER_PREFIX + slug,
81 'date': date.today().isoformat(),
85 if tag in own_attributes:
86 values = own_attributes[tag]
87 if not isinstance(values, list):
90 values = distinct_dc_values(tag, desc_elements)
92 dc_tags.append(DC_TEMPLATE % {'tag': tag, 'value': value})
93 xml = TEMPLATE % {'slug': slug, 'dc': '\n'.join(dc_tags)}