More readable keyboard.
[redakcja.git] / src / documents / management / commands / fixdc.py
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.
3 #
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
8
9
10 class FixDC(XmlUpdater):
11     commit_desc = "auto-fixing DC"
12     retain_publishable = True
13     only_first_chunk = True
14
15     def fix_wluri(elem, change, verbose):
16         try:
17             WLURI.strict(elem.text)
18         except ValidationError:
19             correct_field = str(WLURI.from_slug(
20                                 WLURI(elem.text.strip()).slug))
21             try:
22                 WLURI.strict(correct_field)
23             except ValidationError:
24                 # Can't make a valid WLURI out of it, leave as is.
25                 return False
26             if verbose:
27                 print("Changing %s from %s to %s" % (
28                         elem.tag, elem.text, correct_field
29                     ))
30             elem.text = correct_field
31             return True
32     for field in BookInfo.FIELDS:
33         if field.validator == WLURI:
34             XmlUpdater.fixes_elements('.//' + field.uri)(fix_wluri)
35
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:
42             if verbose:
43                 print("Changing rdf:about from %s to %s" % (
44                         current_about, correct_about
45                     ))
46             elem.set(attr_name, correct_about)
47             return True
48
49
50 class Command(XmlUpdaterCommand):
51     updater = FixDC
52     help = 'Fixes obvious errors in DC: rdf:about and WLURI format.'