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.
4 from librarian import RDFNS, WLURI, ValidationError
5 from librarian.dcparser import BookInfo
6 from documents.management import XmlUpdater
7 from documents.management.commands import XmlUpdaterCommand
10 class FixDC(XmlUpdater):
11 commit_desc = "auto-fixing DC"
12 retain_publishable = True
13 only_first_chunk = True
15 def fix_wluri(elem, change, verbose):
17 WLURI.strict(elem.text)
18 except ValidationError:
19 correct_field = str(WLURI.from_slug(
20 WLURI(elem.text.strip()).slug))
22 WLURI.strict(correct_field)
23 except ValidationError:
24 # Can't make a valid WLURI out of it, leave as is.
27 print("Changing %s from %s to %s" % (
28 elem.tag, elem.text, correct_field
30 elem.text = correct_field
32 for field in BookInfo.FIELDS:
33 if field.validator == WLURI:
34 XmlUpdater.fixes_elements('.//' + field.uri)(fix_wluri)
36 @XmlUpdater.fixes_elements(".//" + RDFNS("Description"))
37 def fix_rdfabout(elem, change, verbose):
38 correct_about = change.tree.book.correct_about()
39 attr_name = RDFNS("about")
40 current_about = elem.get(attr_name)
41 if current_about != correct_about:
43 print("Changing rdf:about from %s to %s" % (
44 current_about, correct_about
46 elem.set(attr_name, correct_about)
50 class Command(XmlUpdaterCommand):
52 help = 'Fixes obvious errors in DC: rdf:about and WLURI format.'