1 # -*- coding: utf-8 -*-
3 from optparse import make_option
5 from django.contrib.auth.models import User
6 from django.core.management.base import BaseCommand
7 from django.db import transaction
9 from catalogue.models import Book
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"),
19 help = 'Updates the rdf:about metadata field.'
21 def handle(self, *args, **options):
22 from lxml import etree
24 verbose = options.get('verbose')
26 # Start transaction management.
27 transaction.commit_unless_managed()
28 transaction.enter_transaction_management()
29 transaction.managed(True)
37 for b in Book.objects.all():
40 print "%s: " % b.title,
43 src = old_head.materialize()
46 t = etree.fromstring(src)
52 desc = t.find(".//{http://www.w3.org/1999/02/22-rdf-syntax-ns#}Description")
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:
64 print "already correct"
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'
71 # retain the publishable status
72 if old_head.publishable:
73 new_head.set_publishable(True)
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
86 transaction.leave_transaction_management()