Simplify settings loading a bit. Remove the things which haven't been working for...
[redakcja.git] / src / catalogue / management / commands / fixdc.py
1 # -*- coding: utf-8 -*-
2 #
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.
5 #
6 from librarian import RDFNS, WLURI, ValidationError
7 from librarian.dcparser import BookInfo
8 from catalogue.management import XmlUpdater
9 from catalogue.management.commands import XmlUpdaterCommand
10
11
12 class FixDC(XmlUpdater):
13     commit_desc = "auto-fixing DC"
14     retain_publishable = True
15     only_first_chunk = True
16
17     def fix_wluri(elem, change, verbose):
18         try:
19             WLURI.strict(elem.text)
20         except ValidationError:
21             correct_field = unicode(WLURI.from_slug(
22                                 WLURI(elem.text.strip()).slug))
23             try:
24                 WLURI.strict(correct_field)
25             except ValidationError:
26                 # Can't make a valid WLURI out of it, leave as is.
27                 return False
28             if verbose:
29                 print "Changing %s from %s to %s" % (
30                         elem.tag, elem.text, correct_field
31                     )
32             elem.text = correct_field
33             return True
34     for field in BookInfo.FIELDS:
35         if field.validator == WLURI:
36             XmlUpdater.fixes_elements('.//' + field.uri)(fix_wluri)
37
38     @XmlUpdater.fixes_elements(".//" + RDFNS("Description"))
39     def fix_rdfabout(elem, change, verbose):
40         correct_about = change.tree.book.correct_about()
41         attr_name = RDFNS("about")
42         current_about = elem.get(attr_name)
43         if current_about != correct_about:
44             if verbose:
45                 print "Changing rdf:about from %s to %s" % (
46                         current_about, correct_about
47                     )
48             elem.set(attr_name, correct_about)
49             return True
50
51
52 class Command(XmlUpdaterCommand):
53     updater = FixDC
54     help = 'Fixes obvious errors in DC: rdf:about and WLURI format.'