2 # -*- coding: utf-8 -*-
3 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
8 sys.path.append('./apps')
9 sys.path.append('./lib')
11 from django.core.management import setup_environ
12 from redakcja import settings
13 from lxml import etree
15 setup_environ(settings)
17 from catalogue.models import Book, Chunk
22 tag_with_name = r"<([^>]+)name=\"([^>]+)>"
24 def fix(book, author, dry_run=True):
26 txt = fc.materialize()
28 newtxt, cnt = re.subn(tag_with_name, r'<\1nazwa="\2>', txt)
30 print "%s ==> nothing changed" % book.slug
34 print "%s ==> changing" % book.slug
35 fc.commit(newtxt, author=author, description=u"Automatyczna zmiana atrybutu name na nazwa")
37 print "%s ==> i would change this" % book.slug
39 def fix_empty_opis(book, author, dry_run=True):
41 txt = fc.materialize()
43 t = etree.fromstring(txt)
44 empty_opis = t.xpath('//opis[not(node())]')
45 empty_cwiczenie = t.xpath('//cwiczenie[not(node())]')
48 print "%s: opis/ x %d" % (book.slug, len(empty_opis))
51 print "%s: cwiczenie/ x %d" % (book.slug, len(empty_cwiczenie))
54 print "%s didn't parse" % b.slug
61 from django.contrib.auth.models import User
62 opts, oth_ = getopt.getopt(sys.argv[1:],
65 dry_run = not (("--seriously",'') in opts)
66 me = User.objects.get(username='marcinkoziej')
68 print "This is a dry run, to really fix something, run with --seriously"
69 for b in Book.objects.all():
71 print "%s ==> does not contain chunks" % b.slug
73 fix_empty_opis(b, me, dry_run)