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')
25 dry_run = options.get('dry_run')
27 # Start transaction management.
28 transaction.commit_unless_managed()
29 transaction.enter_transaction_management()
30 transaction.managed(True)
38 for b in Book.objects.all():
41 print "%s: " % b.title,
44 src = old_head.materialize()
47 t = etree.fromstring(src)
53 desc = t.find(".//{http://www.w3.org/1999/02/22-rdf-syntax-ns#}Description")
60 correct_about = b.correct_about()
61 attr_name = "{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about"
62 if desc.get(attr_name) == correct_about:
65 print "already correct"
67 desc.set(attr_name, correct_about)
69 new_head = chunk.commit(etree.tostring(t, encoding=unicode),
70 author_name='platforma redakcyjna',
71 description='auto-update rdf:about'
73 # retain the publishable status
74 if old_head.publishable:
75 new_head.set_publishable(True)
81 print "All books: ", all_books
82 print "Invalid XML: ", nonxml
83 print "No RDF found: ", nordf
84 print "Already correct: ", already
85 print "Books updated: ", done
88 transaction.leave_transaction_management()