8ff68c3d412e388c9c6ede22ee93283959144eb5
[redakcja.git] / apps / catalogue / management / commands / fix_rdf_about.py
1 # -*- coding: utf-8 -*-
2
3 from optparse import make_option
4
5 from django.contrib.auth.models import User
6 from django.core.management.base import BaseCommand
7 from django.db import transaction
8
9 from catalogue.models import Book
10
11
12 class Command(BaseCommand):
13     option_list = BaseCommand.option_list + (
14         make_option('-q', '--quiet', action='store_false', dest='verbose',
15             default=True, help='Less output'),
16         make_option('-d', '--dry-run', action='store_true', dest='dry_run',
17             default=False, help="Don't actually touch anything"),
18     )
19     help = 'Updates the rdf:about metadata field.'
20
21     def handle(self, *args, **options):
22         from lxml import etree
23
24         verbose = options.get('verbose')
25
26         # Start transaction management.
27         transaction.commit_unless_managed()
28         transaction.enter_transaction_management()
29         transaction.managed(True)
30
31         all_books = 0
32         nonxml = 0
33         nordf = 0
34         already = 0
35         done = 0
36
37         for b in Book.objects.all():
38             all_books += 1
39             if verbose:
40                 print "%s: " % b.title,
41             chunk = b[0]
42             old_head = chunk.head
43             src = old_head.materialize()
44
45             try:
46                 t = etree.fromstring(src)
47             except:
48                 nonxml += 1
49                 if verbose:
50                     print "invalid XML"
51                 continue
52             desc = t.find(".//{http://www.w3.org/1999/02/22-rdf-syntax-ns#}Description")
53             if desc is None:
54                 nordf += 1
55                 if verbose:
56                     print "no RDF found"
57                 continue
58
59             correct_about = b.correct_about()
60             attr_name = "{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about"
61             if desc.get(attr_name) == correct_about:
62                 already += 1
63                 if verbose:
64                     print "already correct"
65                 continue
66             desc.set(attr_name, correct_about)
67             new_head = chunk.commit(etree.tostring(t, encoding=unicode),
68                 author_name='platforma redakcyjna',
69                 description='auto-update rdf:about'
70                 )
71             # retain the publishable status
72             if old_head.publishable:
73                 new_head.set_publishable(True)
74             if verbose:
75                 print "done"
76             done += 1
77
78         # Print results
79         print "All books: ", all_books
80         print "Invalid XML: ", nonxml
81         print "No RDF found: ", nordf
82         print "Already correct: ", already
83         print "Books updated: ", done
84
85         transaction.commit()
86         transaction.leave_transaction_management()
87