220c66ccfafb5dd13e87ca3acac9351f234b8a9e
[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_syntetic(self, slug):
30         if re.match(r"^(gim|lic) \d[.]? ", slug):
31             return True
32         return False
33
34     def gen_xml(self, options, syntetic_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 syntetic_modules:
56             dc(u'relation.hasChild.syntetic', 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'syntetic, 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
79         return holder['xml']
80
81     def handle(self, *args, **options):
82         slug = options['slug']
83         if not slug:
84             slug = slughifi(options['title'])
85         existing = Book.objects.filter(slug=slug)
86
87         master = None
88         if existing:
89             overwrite = confirm("%s exists. Overwrite?" % slug, True)
90             if not overwrite:
91                 return
92             master = existing[0]
93         else:
94             master = Book()
95         master.slug = slug
96         master.title = options['title']
97         master.save()
98
99         if len(master) == 0:
100             master.add(slug, options['title'])
101
102         syntetic_modules = []
103         course_modules = []
104         if 'slugs_file' in options:
105             f = open(options['slugs_file'], 'r')
106             try:
107                 titles = [l.strip() for l in f.readlines()]
108
109                 for t in titles:
110                     if not t: continue
111                     try:
112                         b = Book.objects.get(title=t)
113                     except Book.DoesNotExist:
114                         print "Book for title %s does not exist" % t
115                         continue
116                     if self.looks_like_syntetic(t):
117                         syntetic_modules.append(b.slug)
118                     else:
119                         course_modules.append(b.slug)
120             except Exception, e:
121                 print "Error getting slug list (file %s): %s" % (options['slugs_file'], e)
122
123         print "syntetic: %s" % syntetic_modules
124         print "course: %s" % course_modules
125
126         xml = self.gen_xml(options, syntetic_modules, course_modules)
127
128         print xml
129
130         #        master.save()