subject.curriculum.new in select list
[redakcja.git] / apps / 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 def fix_wluri(elem, change, verbose):
13     try:
14         WLURI.strict(elem.text)
15     except ValidationError:
16         correct_field = unicode(WLURI.from_slug(
17             WLURI(elem.text.strip()).slug))
18         try:
19             WLURI.strict(correct_field)
20         except ValidationError:
21             # Can't make a valid WLURI out of it, leave as is.
22             return False
23         if verbose:
24             print "Changing %s from %s to %s" % (
25                 elem.tag, elem.text, correct_field
26             )
27         elem.text = correct_field
28         return True
29
30
31 class FixDC(XmlUpdater):
32     commit_desc = "auto-fixing DC"
33     retain_publishable = True
34     only_first_chunk = True
35
36     for field in BookInfo.FIELDS:
37         if field.validator == WLURI:
38             XmlUpdater.fixes_elements('.//' + field.uri)(fix_wluri)
39
40     # unused
41     @XmlUpdater.fixes_elements(".//" + RDFNS("Description"))
42     def fix_rdfabout(elem, change, verbose):
43         correct_about = change.tree.book.correct_about()
44         attr_name = RDFNS("about")
45         current_about = elem.get(attr_name)
46         if current_about != correct_about:
47             if verbose:
48                 print "Changing rdf:about from %s to %s" % (
49                         current_about, correct_about
50                     )
51             elem.set(attr_name, correct_about)
52             return True
53
54
55 class Command(XmlUpdaterCommand):
56     updater = FixDC
57     help = 'Fixes obvious errors in DC: rdf:about and WLURI format.'