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.
8 from datetime import date
11 from django.core.management import BaseCommand
13 from catalogue.models import Book
14 from librarian import RDFNS, DCNS
17 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
18 <rdf:Description rdf:about="http://redakcja.wolnelektury.pl/documents/book/%(slug)s/">
26 DC_TEMPLATE = '<dc:%(tag)s xml:lang="pl" xmlns:dc="http://purl.org/dc/elements/1.1/">%(value)s</dc:%(tag)s>'
32 'contributor.translator',
34 'contributor.technical_editor',
35 'contributor.funding',
55 IDENTIFIER_PREFIX = 'http://wolnelektury.pl/katalog/lektura/'
58 def dc_desc_element(book):
59 xml = book.materialize()
60 tree = etree.fromstring(xml)
61 return tree.find(".//" + RDFNS("Description"))
64 def distinct_dc_values(tag, desc_elements):
66 for desc in desc_elements:
67 values.update(elem.text for elem in desc.findall(DCNS(tag)))
71 class Command(BaseCommand):
74 def handle(self, slug, **options):
75 children_slugs = [line.strip() for line in sys.stdin]
76 children = Book.objects.filter(dc_slug__in=children_slugs)
77 desc_elements = [dc_desc_element(child) for child in children]
78 title = u'Utwory wybrane'
81 'relation.hasPart': [IDENTIFIER_PREFIX + child_slug for child_slug in children_slugs],
82 'identifier.url': IDENTIFIER_PREFIX + slug,
83 'date': date.today().isoformat(),
87 if tag in own_attributes:
88 values = own_attributes[tag]
89 if not isinstance(values, list):
92 values = distinct_dc_values(tag, desc_elements)
94 dc_tags.append(DC_TEMPLATE % {'tag': tag, 'value': value})
95 xml = TEMPLATE % {'slug': slug, 'dc': '\n'.join(dc_tags)}