fixed master file generation command
authorMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Tue, 5 Feb 2013 12:41:02 +0000 (13:41 +0100)
committerMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Tue, 5 Feb 2013 12:41:02 +0000 (13:41 +0100)
apps/catalogue/management/commands/make_master.py

index 5d907b9..1d7bab5 100644 (file)
@@ -8,6 +8,7 @@ from optparse import make_option
 from datetime import date
 import re
 from slughifi import slughifi
+from lxml import etree
 
 
 dc_fixed = {
@@ -16,6 +17,7 @@ dc_fixed = {
     'rights_license': u'http://creativecommons.org/licenses/by-sa/3.0/',
     }
 
+dc_namespaces = { "dc": "http://purl.org/dc/elements/1.1/" }
 
 class Command(BaseCommand):
     option_list = BaseCommand.option_list + (
@@ -31,6 +33,41 @@ class Command(BaseCommand):
             return True
         return False
 
+    def fix_part(self, child, master, typ_, audience_, commit_args):
+        print "checking child %s" % child.slug
+        fc = child[0]
+        txt = fc.materialize()
+        changed = False
+        try:
+            t = etree.fromstring(txt)
+        except etree.XMLSyntaxError, e:
+            print "cannot read xml in part: %s" % child.slug
+            print unicode(e)
+            return
+        typ = t.xpath("//dc:type", namespaces=dc_namespaces)
+        if not typ:
+            print "no type in DC, inserting under format" 
+            fmt = t.xpath("//dc:format", namespaces=dc_namespaces)
+            container = fmt.getparent()
+            typ = etree.SubElement(container, etree.QName('dc', 'type'))
+            container.insert(typ, container.index(fmt)+1)
+            changed = True
+        else:
+            typ = typ[0]
+        if typ.text != typ_:
+            print "type is '%s', setting to '%s'" % (typ.text, typ_)
+            changed = True
+            typ.text = typ_
+        audience = t.xpath("//dc:audience", namespaces=dc_namespaces)[0]
+        if audience.text != audience_:
+            print "audience is '%s', setting to '%s'" % (audience.text, audience_)
+            changed = True
+            audience.text = audience_
+        if changed:
+            print "will commit."
+            fc.commit(etree.tostring(t, encoding=unicode), **commit_args)
+
+
     def gen_xml(self, options, synthetic_modules=[], course_modules=[], project_modules=[]):
         holder = {}
         holder['xml'] = u""
@@ -53,11 +90,11 @@ class Command(BaseCommand):
 
         dc(u'title', options['title'])
         for slug in synthetic_modules:
-            dc(u'relation.hasChild.synthetic', slug_url(slug))
+            dc(u'relation.hasPart', slug_url(slug))
         for slug in course_modules:
-            dc(u'relation.hasChild.course', slug_url(slug))
+            dc(u'relation.hasPart', slug_url(slug))
         for slug in project_modules:
-            dc(u'relation.hasChild.project', slug_url(slug))
+            dc(u'relation.hasPart', slug_url(slug))
         dc(u'publisher', u'Fundacja Nowoczesna Polska')
         #        dc(u'subject.competence', meta.get(u'Wybrana kompetencja z Katalogu', u''))
         #        dc(u'subject.curriculum', meta.get(u'Odniesienie do podstawy programowej', u''))
@@ -68,7 +105,7 @@ class Command(BaseCommand):
         dc(u'identifier.url', u'http://edukacjamedialna.edu.pl/%s' % options['slug'])
         dc(u'rights', dc_fixed['rights'])
         dc(u'rights.license', dc_fixed['rights_license'])
-        dc(u'format', u'synthetic, course, project')
+        dc(u'format', u'xml')
         dc(u'type', u'text')
         dc(u'date', date.strftime(date.today(), "%Y-%m-%d"))
         dc(u'audience', options['audience'])
@@ -122,8 +159,10 @@ class Command(BaseCommand):
                         continue
                     if self.looks_like_synthetic(t):
                         synthetic_modules.append(b.slug)
+                        self.fix_part(b, master, 'synthetic', options['audience'], commit_args)
                     else:
                         course_modules.append(b.slug)
+                        self.fix_part(b, master, 'course', options['audience'], commit_args)
             except Exception, e:
                 print "Error getting slug list (file %s): %s" % (options['slugs_file'], e)