Commits

Josh VanderLinden committed f2a5c45

Added the pages_read templatetag.

  • Participants
  • Parent commits 830e80e

Comments (0)

Files changed (1)

bibliophile/templatetags/bibliophile_tags.py

 from django import template
+from django.contrib.auth.models import User
+from django.contrib.humanize.templatetags.humanize import intcomma
+from django.db.models import Max
 from bibliophile.models import Book, Progress
 
 register = template.Library()
 
+@register.simple_tag
+def pages_read(user=None):
+    if not user:
+        user = User.objects.get(pk=1)
+    
+    total_pages = 0
+    for book in user.book_set.filter(progress_mode='p'):
+        max_mile = book.progress_set.aggregate(Max('milestone'))
+        total_pages += max_mile.get('milestone__max', 0) or 0
+    return intcomma(total_pages)
+
 class BooksNode(template.Node):
     """
     Injects a list of finished/unfinished/planned books into the context for