From d04035572b5a730f522a471b583bd2de10578024 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Mon, 21 Nov 2011 15:51:38 +0100 Subject: [PATCH] #388: change rdf:about links, minor fix: error on no http referer --- .../management/commands/fix_rdf_about.py | 87 +++++++++++++++++++ apps/catalogue/models/book.py | 7 +- apps/catalogue/views.py | 10 ++- 3 files changed, 100 insertions(+), 4 deletions(-) create mode 100755 apps/catalogue/management/commands/fix_rdf_about.py diff --git a/apps/catalogue/management/commands/fix_rdf_about.py b/apps/catalogue/management/commands/fix_rdf_about.py new file mode 100755 index 00000000..8ff68c3d --- /dev/null +++ b/apps/catalogue/management/commands/fix_rdf_about.py @@ -0,0 +1,87 @@ +# -*- 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() + diff --git a/apps/catalogue/models/book.py b/apps/catalogue/models/book.py index 76381b84..8c9e6e6a 100755 --- a/apps/catalogue/models/book.py +++ b/apps/catalogue/models/book.py @@ -72,6 +72,11 @@ class Book(models.Model): 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 # ======================= @@ -230,7 +235,7 @@ class Book(models.Model): 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): diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index 89232ea0..58a9378e 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -324,9 +324,13 @@ def chunk_edit(request, slug, chunk): 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, -- 2.20.1