pulling dictionary
[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
12
13 dc_fixed = {
14     'description': u'Publikacja zrealizowana w ramach projektu Cyfrowa Przyszłość (http://edukacjamedialna.edu.pl).',
15     'rights': u'Creative Commons Uznanie autorstwa - Na tych samych warunkach 3.0',
16     'rights_license': u'http://creativecommons.org/licenses/by-sa/3.0/',
17     }
18
19
20 class Command(BaseCommand):
21     option_list = BaseCommand.option_list + (
22         make_option('-s', '--slug', dest='slug', help="Slug for master module"),
23         make_option('-F', '--file', dest='slugs_file', help="file with child module slugs per line"),
24         make_option('-t', '--title', dest='title', default='', help="title of master module"),
25         make_option('-l', '--level', dest='audience', default='', help='Audience level'),
26     )
27     help = 'Create a master module skeleton'
28
29     def looks_like_synthetic(self, title):
30         if re.match(r"^(gim|lic)_\d[.]? ", title):
31             return True
32         return False
33
34     def gen_xml(self, options, synthetic_modules=[], course_modules=[], project_modules=[]):
35         holder = {}
36         holder['xml'] = u""
37
38         def p(t):
39             holder['xml'] += u"%s\n" % t
40
41         def dc(k, v):
42             p(u'<dc:%s xml:lang="pl" xmlns:dc="http://purl.org/dc/elements/1.1/">%s</dc:%s>' % (k, v, k))
43
44         def t(tag, ct):
45             p(u'<%s>%s</%s>' % (tag, ct, tag))
46
47         def slug_url(slug):
48             return u"http://edukacjamedialna/%s/" % slug
49
50         p("<utwor>")
51         p(u'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">')
52         p(u'<rdf:Description rdf:about="http://redakcja.edukacjamedialna.edu.pl/documents/">')
53
54         dc(u'title', options['title'])
55         for slug in synthetic_modules:
56             dc(u'relation.hasChild.synthetic', slug_url(slug))
57         for slug in course_modules:
58             dc(u'relation.hasChild.course', slug_url(slug))
59         for slug in project_modules:
60             dc(u'relation.hasChild.project', slug_url(slug))
61         dc(u'publisher', u'Fundacja Nowoczesna Polska')
62         #        dc(u'subject.competence', meta.get(u'Wybrana kompetencja z Katalogu', u''))
63         #        dc(u'subject.curriculum', meta.get(u'Odniesienie do podstawy programowej', u''))
64         ## for keyword in meta.get(u'Słowa kluczowe', u'').split(u','):
65         ##     keyword = keyword.strip()
66         ##     dc(u'subject', keyword)
67         dc(u'description', dc_fixed['description'])
68         dc(u'identifier.url', u'http://edukacjamedialna.edu.pl/%s' % options['slug'])
69         dc(u'rights', dc_fixed['rights'])
70         dc(u'rights.license', dc_fixed['rights_license'])
71         dc(u'format', u'synthetic, course, project')
72         dc(u'type', u'text')
73         dc(u'date', date.strftime(date.today(), "%Y-%m-%d"))
74         dc(u'audience', options['audience'])
75         dc(u'language', u'pol')
76         p(u'</rdf:Description>')
77         p(u'</rdf:RDF>')
78         p(u'</utwor>')
79
80         return holder['xml']
81
82     def handle(self, *args, **options):
83         commit_args = {
84             "author_name": 'Platforma',
85             "description": 'Automatycznie zaimportowane z EtherPad',
86             "publishable": False,
87         }
88
89         slug = options['slug']
90         if not slug:
91             slug = slughifi(options['title'])
92         existing = Book.objects.filter(slug=slug)
93
94         master = None
95         if existing:
96             overwrite = confirm("%s exists. Overwrite?" % slug, True)
97             if not overwrite:
98                 return
99             master = existing[0]
100         else:
101             master = Book()
102         master.slug = slug
103         master.title = options['title']
104         master.save()
105
106         if len(master) == 0:
107             master.add(slug, options['title'])
108
109         synthetic_modules = []
110         course_modules = []
111         if 'slugs_file' in options:
112             f = open(options['slugs_file'], 'r')
113             try:
114                 titles = [l.strip() for l in f.readlines()]
115
116                 for t in titles:
117                     if not t: continue
118                     try:
119                         b = Book.objects.get(title=t)
120                     except Book.DoesNotExist:
121                         print "Book for title %s does not exist" % t
122                         continue
123                     if self.looks_like_synthetic(t):
124                         synthetic_modules.append(b.slug)
125                     else:
126                         course_modules.append(b.slug)
127             except Exception, e:
128                 print "Error getting slug list (file %s): %s" % (options['slugs_file'], e)
129
130         print "synthetic: %s" % synthetic_modules
131         print "course: %s" % course_modules
132
133         xml = self.gen_xml(options, synthetic_modules, course_modules)
134         c = master[0]
135         print xml
136         if confirm("Commit?", True):
137             c.commit(xml, **commit_args)
138