1 # -*- coding: utf-8 -*-
3 from django.core.management.base import BaseCommand
4 from django.core.management.color import color_style
5 from catalogue.management.prompt import confirm
6 from catalogue.models import Book
7 from optparse import make_option
8 from datetime import date
10 from slughifi import slughifi
11 from lxml import etree
12 from librarian import WLURI
16 'description': u'Publikacja zrealizowana w ramach projektu Cyfrowa Przyszłość (http://edukacjamedialna.edu.pl).',
17 'rights': u'Creative Commons Uznanie autorstwa - Na tych samych warunkach 3.0',
18 'rights_license': u'http://creativecommons.org/licenses/by-sa/3.0/',
21 dc_namespaces = { "dc": "http://purl.org/dc/elements/1.1/" }
23 class Command(BaseCommand):
24 option_list = BaseCommand.option_list + (
25 make_option('-s', '--slug', dest='slug', help="Slug for master module (if empty will be generated from title)"),
26 make_option('-F', '--file', dest='slugs_file', help="file with child module titles per line"),
27 make_option('-t', '--title', dest='title', default='', help="title of master module"),
28 make_option('-l', '--level', dest='audience', default='', help='Audience level'),
30 help = 'Create a master module skeleton'
32 def looks_like_synthetic(self, title):
33 if re.match(r"^(gim|lic)_\d[.]? ", title):
37 def adopt(self, child, master, typ_, audience_, commit_args):
39 txt = fc.materialize()
42 t = etree.fromstring(txt)
43 except etree.XMLSyntaxError, e:
44 print "cannot read xml in part: %s" % child.slug
47 wluri = WLURI(t.xpath("//dc:identifier.url", namespaces=dc_namespaces)[0].text)
48 typ = t.xpath("//dc:type", namespaces=dc_namespaces)
50 print "no type in DC, inserting under format"
51 fmt = t.xpath("//dc:format", namespaces=dc_namespaces)
52 container = fmt.getparent()
53 typ = etree.SubElement(container, etree.QName('dc', 'type'))
54 container.insert(typ, container.index(fmt)+1)
59 print "type is '%s', setting to '%s'" % (typ.text, typ_)
62 audience = t.xpath("//dc:audience", namespaces=dc_namespaces)[0]
63 if audience.text != audience_:
64 print "audience is '%s', setting to '%s'" % (audience.text, audience_)
66 audience.text = audience_
69 fc.commit(etree.tostring(t, encoding=unicode), **commit_args)
73 def gen_xml(self, options, synthetic_modules=[], course_modules=[], project_modules=[]):
76 slug = options['slug']
78 slug = slughifi(options['title'])
81 holder['xml'] += u"%s\n" % t
84 p(u'<dc:%s xml:lang="pl" xmlns:dc="http://purl.org/dc/elements/1.1/">%s</dc:%s>' % (k, v, k))
87 p(u'<%s>%s</%s>' % (tag, ct, tag))
90 return u"http://edukacjamedialna/%s/" % slug
93 p(u'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">')
94 p(u'<rdf:Description rdf:about="http://redakcja.edukacjamedialna.edu.pl/documents/">')
96 dc(u'title', options['title'])
97 for s in synthetic_modules:
98 dc(u'relation.hasPart', unicode(s))
99 for s in course_modules:
100 dc(u'relation.hasPart', unicode(s))
101 for s in project_modules:
102 dc(u'relation.hasPart', unicode(s))
103 dc(u'publisher', u'Fundacja Nowoczesna Polska')
104 # dc(u'subject.competence', meta.get(u'Wybrana kompetencja z Katalogu', u''))
105 # dc(u'subject.curriculum', meta.get(u'Odniesienie do podstawy programowej', u''))
106 ## for keyword in meta.get(u'Słowa kluczowe', u'').split(u','):
107 ## keyword = keyword.strip()
108 ## dc(u'subject', keyword)
109 dc(u'description', dc_fixed['description'])
110 dc(u'identifier.url', u'http://edukacjamedialna.edu.pl/%s' % slug)
111 dc(u'rights', dc_fixed['rights'])
112 dc(u'rights.license', dc_fixed['rights_license'])
113 dc(u'format', u'xml')
114 dc(u'type', u'section')
115 dc(u'date', date.strftime(date.today(), "%Y-%m-%d"))
116 dc(u'language', u'pol')
117 p(u'</rdf:Description>')
123 def handle(self, *args, **options):
125 "author_name": 'Platforma',
126 "description": 'Automatycznie zaimportowane z EtherPad',
127 "publishable": False,
130 slug = options['slug']
132 slug = slughifi(options['title'])
133 existing = Book.objects.filter(slug=slug)
137 overwrite = confirm("%s exists. Overwrite?" % slug, True)
144 master.title = options['title']
148 master.add(slug, options['title'])
150 synthetic_modules = []
152 if 'slugs_file' in options:
153 f = open(options['slugs_file'], 'r')
155 titles = [l.strip() for l in f.readlines()]
160 b = Book.objects.get(title=t)
161 except Book.DoesNotExist:
162 print "Book for title %s does not exist" % t
164 if self.looks_like_synthetic(t):
165 wlurl = self.adopt(b, master, 'synthetic', options['audience'], commit_args)
166 synthetic_modules.append(wlurl)
168 wlurl = self.adopt(b, master, 'course', options['audience'], commit_args)
169 course_modules.append(wlurl)
171 print "Error getting slug list (file %s): %s" % (options['slugs_file'], e)
173 print "synthetic: %s" % [unicode(z) for z in synthetic_modules]
174 print "course: %s" % [unicode(z) for z in course_modules]
176 xml = self.gen_xml(options, synthetic_modules, course_modules)
179 if confirm("Commit?", True):
180 c.commit(xml, **commit_args)