- with transaction.atomic():
- cursor = connection.cursor()
- if connection.vendor == 'postgres':
- cursor.execute("TRUNCATE catalogue_book_ancestor")
- cursor.execute("""
- WITH RECURSIVE ancestry AS (
- SELECT book.id, book.parent_id
- FROM catalogue_book AS book
- WHERE book.parent_id IS NOT NULL
- UNION
- SELECT ancestor.id, book.parent_id
- FROM ancestry AS ancestor, catalogue_book AS book
- WHERE ancestor.parent_id = book.id
- AND book.parent_id IS NOT NULL
- )
- INSERT INTO catalogue_book_ancestor
- (from_book_id, to_book_id)
- SELECT id, parent_id
- FROM ancestry
- ORDER BY id;
- """)
- else:
- cursor.execute("DELETE FROM catalogue_book_ancestor")
- for b in cls.objects.exclude(parent=None):
- parent = b.parent
- while parent is not None:
- b.ancestor.add(parent)
- parent = parent.parent
-
- def flush_includes(self, languages=True):
- if not languages:
- return
- if languages is True:
- languages = [lc for (lc, _ln) in settings.LANGUAGES]
- flush_ssi_includes([
- template % (self.pk, lang)
- for template in [
- '/katalog/b/%d/mini.%s.html',
- '/katalog/b/%d/mini_nolink.%s.html',
- '/katalog/b/%d/short.%s.html',
- '/katalog/b/%d/wide.%s.html',
- '/api/include/book/%d.%s.json',
- '/api/include/book/%d.%s.xml',
- ]
- for lang in languages
- ])
+ cursor = connection.cursor()
+ if connection.vendor == 'postgres':
+ cursor.execute("TRUNCATE catalogue_book_ancestor")
+ cursor.execute("""
+ WITH RECURSIVE ancestry AS (
+ SELECT book.id, book.parent_id
+ FROM catalogue_book AS book
+ WHERE book.parent_id IS NOT NULL
+ UNION
+ SELECT ancestor.id, book.parent_id
+ FROM ancestry AS ancestor, catalogue_book AS book
+ WHERE ancestor.parent_id = book.id
+ AND book.parent_id IS NOT NULL
+ )
+ INSERT INTO catalogue_book_ancestor
+ (from_book_id, to_book_id)
+ SELECT id, parent_id
+ FROM ancestry
+ ORDER BY id;
+ """)
+ else:
+ cursor.execute("DELETE FROM catalogue_book_ancestor")
+ for b in cls.objects.exclude(parent=None):
+ parent = b.parent
+ while parent is not None:
+ b.ancestor.add(parent)
+ parent = parent.parent
+
+ @property
+ def ancestors(self):
+ if self.parent:
+ for anc in self.parent.ancestors:
+ yield anc
+ yield self.parent
+ else:
+ return []
+
+ def clear_cache(self):
+ clear_cached_renders(self.mini_box)
+ clear_cached_renders(self.mini_box_nolink)