--- /dev/null
+# -*- coding: utf-8 -*-
+
+from optparse import make_option
+
+from django.contrib.auth.models import User
+from django.core.management.base import BaseCommand
+from django.db import transaction
+
+from catalogue.models import Book
+
+
+class Command(BaseCommand):
+ option_list = BaseCommand.option_list + (
+ make_option('-q', '--quiet', action='store_false', dest='verbose',
+ default=True, help='Less output'),
+ make_option('-d', '--dry-run', action='store_true', dest='dry_run',
+ default=False, help="Don't actually touch anything"),
+ )
+ help = 'Updates the rdf:about metadata field.'
+
+ def handle(self, *args, **options):
+ from lxml import etree
+
+ verbose = options.get('verbose')
+
+ # Start transaction management.
+ transaction.commit_unless_managed()
+ transaction.enter_transaction_management()
+ transaction.managed(True)
+
+ all_books = 0
+ nonxml = 0
+ nordf = 0
+ already = 0
+ done = 0
+
+ for b in Book.objects.all():
+ all_books += 1
+ if verbose:
+ print "%s: " % b.title,
+ chunk = b[0]
+ old_head = chunk.head
+ src = old_head.materialize()
+
+ try:
+ t = etree.fromstring(src)
+ except:
+ nonxml += 1
+ if verbose:
+ print "invalid XML"
+ continue
+ desc = t.find(".//{http://www.w3.org/1999/02/22-rdf-syntax-ns#}Description")
+ if desc is None:
+ nordf += 1
+ if verbose:
+ print "no RDF found"
+ continue
+
+ correct_about = b.correct_about()
+ attr_name = "{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about"
+ if desc.get(attr_name) == correct_about:
+ already += 1
+ if verbose:
+ print "already correct"
+ continue
+ desc.set(attr_name, correct_about)
+ new_head = chunk.commit(etree.tostring(t, encoding=unicode),
+ author_name='platforma redakcyjna',
+ description='auto-update rdf:about'
+ )
+ # retain the publishable status
+ if old_head.publishable:
+ new_head.set_publishable(True)
+ if verbose:
+ print "done"
+ done += 1
+
+ # Print results
+ print "All books: ", all_books
+ print "Invalid XML: ", nonxml
+ print "No RDF found: ", nordf
+ print "Already correct: ", already
+ print "Books updated: ", done
+
+ transaction.commit()
+ transaction.leave_transaction_management()
+
def get_absolute_url(self):
return ("catalogue_book", [self.slug])
+ def correct_about(self):
+ return "http://%s%s" % (
+ Site.objects.get_current().domain,
+ self.get_absolute_url()
+ )
# Creating & manipulating
# =======================
except ValidationError, e:
raise AssertionError(_('Invalid Dublin Core') + ': ' + str(e))
- valid_about = "http://%s%s" % (Site.objects.get_current().domain, self.get_absolute_url())
+ valid_about = self.correct_about()
assert bi.about == valid_about, _("rdf:about is not") + " " + valid_about
def hidden(self):
else:
form = forms.ChunkForm(instance=doc)
- parts = urlsplit(request.META['HTTP_REFERER'])
- parts = ['', ''] + list(parts[2:])
- go_next = urlquote_plus(urlunsplit(parts))
+ referer = request.META.get('HTTP_REFERER')
+ if referer:
+ parts = urlsplit(referer)
+ parts = ['', ''] + list(parts[2:])
+ go_next = urlquote_plus(urlunsplit(parts))
+ else:
+ go_next = ''
return direct_to_template(request, "catalogue/chunk_edit.html", extra_context={
"chunk": doc,