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