skrypt do poprawiania daty przejścia do domeny publicznej
[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])+70))
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)+70)
41             dates['date'] = date
42             dates['olddate'] = olddate
43             return tagopen+date+tagclose
44         new_txt = re.sub(datepd, up_date, txt)
45         print "%s: %s->%s" % (book.slug, dates['olddate'], dates['date'])
46         fc.commit(new_txt, author=author, description=u"Automatyczne poprawienie daty przejścia do domeny publicznej z %s na %s" % (dates['olddate'], dates['date']))
47
48 import sys
49 import getopt
50 from django.contrib.auth.models import User
51 opts, oth_ = getopt.getopt(sys.argv[1:],[],[ "seriously"])
52 dry_run = not (("--seriously",'') in opts)
53 me = User.objects.get(username='marcinkoziej')
54 if dry_run:
55     print "This is a dry run, to really change dates, run with --seriously"
56 for b in Book.objects.all():
57     fix(b, me, dry_run)
58
59     
60