#388: change rdf:about links,
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Mon, 21 Nov 2011 14:51:38 +0000 (15:51 +0100)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Mon, 21 Nov 2011 14:51:38 +0000 (15:51 +0100)
minor fix: error on no http referer

apps/catalogue/management/commands/fix_rdf_about.py [new file with mode: 0755]
apps/catalogue/models/book.py
apps/catalogue/views.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 (executable)
index 0000000..8ff68c3
--- /dev/null
@@ -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()
+
index 76381b8..8c9e6e6 100755 (executable)
@@ -72,6 +72,11 @@ class Book(models.Model):
     def get_absolute_url(self):
         return ("catalogue_book", [self.slug])
 
     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
     # =======================
 
     # Creating & manipulating
     # =======================
@@ -230,7 +235,7 @@ class Book(models.Model):
         except ValidationError, e:
             raise AssertionError(_('Invalid Dublin Core') + ': ' + str(e))
 
         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):
         assert bi.about == valid_about, _("rdf:about is not") + " " + valid_about
 
     def hidden(self):
index 89232ea..58a9378 100644 (file)
@@ -324,9 +324,13 @@ def chunk_edit(request, slug, chunk):
     else:
         form = forms.ChunkForm(instance=doc)
 
     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,
 
     return direct_to_template(request, "catalogue/chunk_edit.html", extra_context={
         "chunk": doc,