From: Jan Szejko Date: Fri, 19 May 2017 08:57:47 +0000 (+0200) Subject: command to add a parent work X-Git-Url: https://git.mdrn.pl/redakcja.git/commitdiff_plain/dee29ed64f4ea7f3e74adb0d5f625f25c682ac57 command to add a parent work --- diff --git a/apps/catalogue/management/commands/add_parent.py b/apps/catalogue/management/commands/add_parent.py new file mode 100644 index 00000000..2ab0510c --- /dev/null +++ b/apps/catalogue/management/commands/add_parent.py @@ -0,0 +1,101 @@ +# -*- coding: utf-8 -*- +# +# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# +import sys + +from datetime import date +from lxml import etree + +from django.core.management import BaseCommand + +from catalogue.models import Book +from librarian import RDFNS, DCNS + +TEMPLATE = ''' + + +%(dc)s + + + + +''' + +DC_TEMPLATE = '%(value)s' + +DC_TAGS = ( + 'creator', + 'title', + 'relation.hasPart', + 'contributor.translator', + 'contributor.editor', + 'contributor.technical_editor', + 'contributor.funding', + 'contributor.thanks', + 'publisher', + 'subject.period', + 'subject.type', + 'subject.genre', + 'description', + 'identifier.url', + 'source', + 'source.URL', + 'rights.license', + 'rights', + 'date.pd', + 'format', + 'type', + 'date', + 'audience', + 'language', +) + +IDENTIFIER_PREFIX = 'http://wolnelektury.pl/katalog/lektura/' + + +def dc_desc_element(book): + xml = book.materialize() + tree = etree.fromstring(xml) + return tree.find(".//" + RDFNS("Description")) + + +def distinct_dc_values(tag, desc_elements): + values = set() + for desc in desc_elements: + values.update(elem.text for elem in desc.findall(DCNS(tag))) + return values + + +class Command(BaseCommand): + args = 'slug' + + def handle(self, slug, **options): + children_slugs = [line.strip() for line in sys.stdin] + children = Book.objects.filter(dc_slug__in=children_slugs) + desc_elements = [dc_desc_element(child) for child in children] + title = u'Utwory wybrane' + own_attributes = { + 'title': title, + 'relation.hasPart': [IDENTIFIER_PREFIX + child_slug for child_slug in children_slugs], + 'identifier.url': IDENTIFIER_PREFIX + slug, + 'date': date.today().isoformat(), + } + dc_tags = [] + for tag in DC_TAGS: + if tag in own_attributes: + values = own_attributes[tag] + if not isinstance(values, list): + values = [values] + else: + values = distinct_dc_values(tag, desc_elements) + for value in values: + dc_tags.append(DC_TEMPLATE % {'tag': tag, 'value': value}) + xml = TEMPLATE % {'slug': slug, 'dc': '\n'.join(dc_tags)} + Book.create( + text=xml, + creator=None, + slug=slug, + title=title, + gallery=slug)