summaryrefslogtreecommitdiffstats
path: root/todolists/utils.py
blob: 894f3f1d6d42b1e7c7a2f3a4f363ffc9f954e8aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from django.db.models import Count

from main.models import Todolist

def get_annotated_todolists():
    qs = Todolist.objects.all()
    lists = qs.select_related('creator').annotate(
            pkg_count=Count('todolistpkg')).order_by('-date_added')
    incomplete = qs.filter(todolistpkg__complete=False).annotate(
            Count('todolistpkg')).values_list('id', 'todolistpkg__count')

    # tag each list with an incomplete package count
    lookup = dict(incomplete)
    for todolist in lists:
        todolist.incomplete_count = lookup.get(todolist.id, 0)

    return lists

# vim: set ts=4 sw=4 et: