and some more fixes
[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'text')
115         dc(u'date', date.strftime(date.today(), "%Y-%m-%d"))
116         dc(u'audience', options['audience'])
117         dc(u'language', u'pol')
118         p(u'</rdf:Description>')
119         p(u'</rdf:RDF>')
120         p(u'</utwor>')
121
122         return holder['xml']
123
124     def handle(self, *args, **options):
125         commit_args = {
126             "author_name": 'Platforma',
127             "description": 'Automatycznie zaimportowane z EtherPad',
128             "publishable": False,
129         }
130
131         slug = options['slug']
132         if not slug:
133             slug = slughifi(options['title'])
134         existing = Book.objects.filter(slug=slug)
135
136         master = None
137         if existing:
138             overwrite = confirm("%s exists. Overwrite?" % slug, True)
139             if not overwrite:
140                 return
141             master = existing[0]
142         else:
143             master = Book()
144         master.slug = slug
145         master.title = options['title']
146         master.save()
147
148         if len(master) == 0:
149             master.add(slug, options['title'])
150
151         synthetic_modules = []
152         course_modules = []
153         if 'slugs_file' in options:
154             f = open(options['slugs_file'], 'r')
155             try:
156                 titles = [l.strip() for l in f.readlines()]
157
158                 for t in titles:
159                     if not t: continue
160                     try:
161                         b = Book.objects.get(title=t)
162                     except Book.DoesNotExist:
163                         print "Book for title %s does not exist" % t
164                         continue
165                     if self.looks_like_synthetic(t):
166                         wlurl = self.adopt(b, master, 'synthetic', options['audience'], commit_args)
167                         synthetic_modules.append(wlurl)
168                     else:
169                         wlurl = self.adopt(b, master, 'course', options['audience'], commit_args)
170                         course_modules.append(wlurl)
171             except Exception, e:
172                 print "Error getting slug list (file %s): %s" % (options['slugs_file'], e)
173
174         print "synthetic: %s" % [unicode(z) for z in synthetic_modules]
175         print "course: %s" % [unicode(z) for z in course_modules]
176
177         xml = self.gen_xml(options, synthetic_modules, course_modules)
178         c = master[0]
179         print xml
180         if confirm("Commit?", True):
181             c.commit(xml, **commit_args)
182