Fix something script
[redakcja.git] / scripts / fix_something.py
1 #!/usr/bin/env python
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.
5 #
6 import sys
7 sys.path.append('.')
8 sys.path.append('./apps')
9 sys.path.append('./lib')
10
11 from django.core.management import setup_environ
12 from redakcja import settings
13
14 setup_environ(settings)
15
16 from catalogue.models import Book, Chunk
17 import re
18
19 fixed = {}
20
21 tag_with_name = r"<([^>]+)name=\"([^>]+)>"
22
23 def fix(book, author, dry_run=True):
24     if len(book) == 0:
25         print "%s does not contain chunks" % book.slug
26         return
27     fc = book[0]
28     txt = fc.materialize()
29
30     newtxt, cnt = re.subn(tag_with_name, r'<\1nazwa="\2>', txt)
31     if cnt == 0:
32         print "%s nothing changed" % book
33         return
34     
35     if not dry_run:
36         print "%s changing" % book
37         fc.commit(newtxt, author=author, description=u"Automatyczna zmiana atrybutu name na nazwa")
38
39
40 import sys
41 import getopt
42 from django.contrib.auth.models import User
43 opts, oth_ = getopt.getopt(sys.argv[1:],
44     [],
45     [ "seriously"])
46 dry_run = not (("--seriously",'') in opts)
47 me = User.objects.get(username='marcinkoziej')
48 if dry_run:
49     print "This is a dry run, to really change dates, run with --seriously"
50 for b in Book.objects.all():
51     fix(b, me, dry_run)
52
53     
54