1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from functools import wraps
6 from django import template
7 from catalogue.models import Book
10 register = template.Library()
13 class StatsNode(template.Node):
14 def __init__(self, value, varname=None):
16 self.varname = varname
18 def render(self, context):
20 context[self.varname] = self.value
26 def register_counter(f):
27 """Turns a simple counting function into a registered counter tag.
29 You can run a counter tag as a simple {% tag_name %} tag, or
30 as {% tag_name var_name %} to store the result in a variable.
34 def wrapped(parser, token):
36 tag_name, args = token.contents.split(None, 1)
39 return StatsNode(f(), args)
41 return register.tag(wrapped)
45 def count_books_all():
46 return Book.objects.all().count()
51 return Book.objects.filter(children=None).count()
55 def count_books_parent():
56 return Book.objects.exclude(children=None).count()
60 def count_books_root():
61 return Book.objects.filter(parent=None).count()