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.
6 from functools import wraps
9 from django import template
11 from catalogue.models import Book, BookMedia
14 register = template.Library()
16 class StatsNode(template.Node):
17 def __init__(self, value, varname=None):
19 self.varname = varname
21 def render(self, context):
23 context[self.varname] = self.value
29 def register_counter(f):
30 """Turns a simple counting function into a registered counter tag.
32 You can run a counter tag as a simple {% tag_name %} tag, or
33 as {% tag_name var_name %} to store the result in a variable.
37 def wrapped(parser, token):
39 tag_name, args = token.contents.split(None, 1)
42 return StatsNode(f(), args)
44 return register.tag(wrapped)
48 def count_books_all():
49 return Book.objects.all().count()
52 def count_books_nonempty():
53 return Book.objects.exclude(html_file='').count()
56 def count_books_empty():
57 return Book.objects.filter(html_file='').count()
60 def count_books_root():
61 return Book.objects.filter(parent=None).count()