-# -*- coding: utf-8 -*-
-#
# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
def fix_chunk(self, chunk, user, verbose=0, dry_run=False):
"""Runs the update for a single chunk."""
if verbose >= 2:
- print chunk.get_absolute_url()
+ print(chunk.get_absolute_url())
old_head = chunk.head
src = old_head.materialize()
try:
tree = etree.fromstring(src)
except:
if verbose:
- print "%s: invalid XML" % chunk.get_absolute_url()
+ print("%s: invalid XML" % chunk.get_absolute_url())
self.counters['Bad XML'] += 1
return
if not dry_run:
new_head = chunk.commit(
- etree.tostring(tree, encoding=unicode),
+ etree.tostring(tree, encoding='unicode'),
author=user,
description=self.commit_desc
)
if old_head.publishable:
new_head.set_publishable(True)
if verbose >= 2:
- print "done"
+ print("done")
self.counters['Updated chunks'] += 1
def run(self, user, verbose=0, dry_run=False, books=None):
books = Book.objects.all()
# Start transaction management.
- transaction.enter_transaction_management()
-
- for book in books:
- self.counters['All books'] += 1
- chunks = book.chunk_set.all()
- if self.only_first_chunk:
- chunks = chunks[:1]
- for chunk in chunks:
- self.counters['All chunks'] += 1
- self.fix_chunk(chunk, user, verbose, dry_run)
-
- transaction.commit()
- transaction.leave_transaction_management()
+ with transaction.atomic():
+ for book in books:
+ self.counters['All books'] += 1
+ chunks = book.chunk_set.all()
+ if self.only_first_chunk:
+ chunks = chunks[:1]
+ for chunk in chunks:
+ self.counters['All chunks'] += 1
+ self.fix_chunk(chunk, user, verbose, dry_run)
def print_results(self):
"""Prints the counters."""
for item in sorted(self.counters.items()):
- print "%s: %d" % item
+ print("%s: %d" % item)