Editor update: fixes, additional logging
[redakcja.git] / scripts / fix_pd.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 datepd = r"(<dc:date.pd[^>]+>)([0-9]+)(</dc:date.pd>)"
22 def fix(book, author, dry_run=True):
23     if len(book) == 0:
24         print "%s does not contain chunks" % book.slug
25         return
26     fc = book[0]
27     txt = fc.materialize()
28
29     if dry_run:
30         m = re.search(datepd, txt)
31         if m:
32             print("%s: %s->%d" % (book.slug, m.groups()[1], int(m.groups()[1])+71))
33         else:
34             print("%s: date.pd not found??" % (book.slug,))
35     else:
36         dates = {}
37         def up_date(match):
38             tagopen, date, tagclose = match.groups()
39             olddate=date
40             date = str(int(date)+71)
41             dates['date'] = date
42             dates['olddate'] = olddate
43             dates['overflow'] = False
44             if int(date) > 2012:
45                dates['overflow'] = True
46                return tagopen+date+tagclose
47
48         new_txt = re.sub(datepd, up_date, txt)
49         if dates:
50             print "%s: %s->%s" % (book.slug, dates['olddate'], dates['date'])
51             if dates['overflow']:
52                 print "oops, new date would overfow to the future, i'm not changing"
53                 return  
54            # fc.commit(new_txt, author=author, description=u"Automatyczne poprawienie daty przejścia do domeny publicznej z %s na %s" % (dates['olddate'], dates['date']))
55         else:
56             print "skipping %s" % book.slug
57 import sys
58 import getopt
59 from django.contrib.auth.models import User
60 opts, oth_ = getopt.getopt(sys.argv[1:],[],[ "seriously"])
61 dry_run = not (("--seriously",'') in opts)
62 me = User.objects.get(username='marcinkoziej')
63 if dry_run:
64     print "This is a dry run, to really change dates, run with --seriously"
65 for b in Book.objects.all():
66     fix(b, me, dry_run)
67
68     
69