no audience in master file
[redakcja.git] / apps / catalogue / management / commands / make_master.py
1 # -*- coding: utf-8 -*-
2
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
9 import re
10 from slughifi import slughifi
11 from lxml import etree
12 from librarian import WLURI
13
14
15 dc_fixed = {
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/',
19     }
20
21 dc_namespaces = { "dc": "http://purl.org/dc/elements/1.1/" }
22
23 class Command(BaseCommand):
24     option_list = BaseCommand.option_list + (
25         make_option('-s', '--slug', dest='slug', help="Slug for master module"),
26         make_option('-F', '--file', dest='slugs_file', help="file with child module slugs 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'),
29     )
30     help = 'Create a master module skeleton'
31
32     def looks_like_synthetic(self, title):
33         if re.match(r"^(gim|lic)_\d[.]? ", title):
34             return True
35         return False
36
37     def adopt(self, child, master, typ_, audience_, commit_args):
38         fc = child[0]
39         txt = fc.materialize()
40         changed = False
41         try:
42             t = etree.fromstring(txt)
43         except etree.XMLSyntaxError, e:
44             print "cannot read xml in part: %s" % child.slug
45             print unicode(e)
46             return
47         wluri = WLURI(t.xpath("//dc:identifier.url", namespaces=dc_namespaces)[0].text)
48         typ = t.xpath("//dc:type", namespaces=dc_namespaces)
49         if not typ:
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)
55             changed = True
56         else:
57             typ = typ[0]
58         if typ.text != typ_:
59             print "type is '%s', setting to '%s'" % (typ.text, typ_)
60             changed = True
61             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_)
65             changed = True
66             audience.text = audience_
67         if changed:
68             print "will commit."
69             fc.commit(etree.tostring(t, encoding=unicode), **commit_args)
70         return wluri
71
72
73     def gen_xml(self, options, synthetic_modules=[], course_modules=[], project_modules=[]):
74         holder = {}
75         holder['xml'] = u""
76         slug = options['slug']
77         if not slug:
78             slug = slughifi(options['title'])
79
80         def p(t):
81             holder['xml'] += u"%s\n" % t
82
83         def dc(k, v):
84             p(u'<dc:%s xml:lang="pl" xmlns:dc="http://purl.org/dc/elements/1.1/">%s</dc:%s>' % (k, v, k))
85
86         def t(tag, ct):
87             p(u'<%s>%s</%s>' % (tag, ct, tag))
88
89         def slug_url(slug):
90             return u"http://edukacjamedialna/%s/" % slug
91
92         p("<utwor>")
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/">')
95
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>')
118         p(u'</rdf:RDF>')
119         p(u'</utwor>')
120
121         return holder['xml']
122
123     def handle(self, *args, **options):
124         commit_args = {
125             "author_name": 'Platforma',
126             "description": 'Automatycznie zaimportowane z EtherPad',
127             "publishable": False,
128         }
129
130         slug = options['slug']
131         if not slug:
132             slug = slughifi(options['title'])
133         existing = Book.objects.filter(slug=slug)
134
135         master = None
136         if existing:
137             overwrite = confirm("%s exists. Overwrite?" % slug, True)
138             if not overwrite:
139                 return
140             master = existing[0]
141         else:
142             master = Book()
143         master.slug = slug
144         master.title = options['title']
145         master.save()
146
147         if len(master) == 0:
148             master.add(slug, options['title'])
149
150         synthetic_modules = []
151         course_modules = []
152         if 'slugs_file' in options:
153             f = open(options['slugs_file'], 'r')
154             try:
155                 titles = [l.strip() for l in f.readlines()]
156
157                 for t in titles:
158                     if not t: continue
159                     try:
160                         b = Book.objects.get(title=t)
161                     except Book.DoesNotExist:
162                         print "Book for title %s does not exist" % t
163                         continue
164                     if self.looks_like_synthetic(t):
165                         wlurl = self.adopt(b, master, 'synthetic', options['audience'], commit_args)
166                         synthetic_modules.append(wlurl)
167                     else:
168                         wlurl = self.adopt(b, master, 'course', options['audience'], commit_args)
169                         course_modules.append(wlurl)
170             except Exception, e:
171                 print "Error getting slug list (file %s): %s" % (options['slugs_file'], e)
172
173         print "synthetic: %s" % [unicode(z) for z in synthetic_modules]
174         print "course: %s" % [unicode(z) for z in course_modules]
175
176         xml = self.gen_xml(options, synthetic_modules, course_modules)
177         c = master[0]
178         print xml
179         if confirm("Commit?", True):
180             c.commit(xml, **commit_args)
181