Some housekeeping
[redakcja.git] / src / catalogue / management / __init__.py
index bc3d6c0..6f6f6b6 100644 (file)
@@ -1,5 +1,3 @@
-# -*- 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.
 #
@@ -54,14 +52,14 @@ class XmlUpdater(object):
     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
 
@@ -82,7 +80,7 @@ class XmlUpdater(object):
 
         if not dry_run:
             new_head = chunk.commit(
-                etree.tostring(tree, encoding=unicode),
+                etree.tostring(tree, encoding='unicode'),
                 author=user,
                 description=self.commit_desc
             )
@@ -90,7 +88,7 @@ class XmlUpdater(object):
                 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):
@@ -100,21 +98,17 @@ class XmlUpdater(object):
             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)