~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to pybb/unread.py

  • Committer: Holger Rapp
  • Date: 2009-02-25 16:55:36 UTC
  • Revision ID: sirver@kallisto.local-20090225165536-3abfhjx8qsgtzyru
- Added my hacked version of pybb. Remerging new versions is very difficult at this point :(

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from pybb.models import Topic, Post, Read
 
2
 
 
3
def cache_unreads(qs, user):
 
4
    if not len(qs) or not user.is_authenticated():
 
5
        return qs
 
6
    if isinstance(qs[0], Topic):
 
7
        reads = Read.objects.filter(topic__pk__in=set(x.id for x in qs),
 
8
            user=user).select_related()
 
9
        read_map = dict((x.topic.id, x) for x in reads)
 
10
 
 
11
        for topic in qs:
 
12
            topic._read = read_map.get(topic.id, None)
 
13
        return qs
 
14
    elif isinstance(qs[0], Post):
 
15
        ids = set(x.topic.id for x in qs)
 
16
        reads = Read.objects.filter(topic__pk__in=ids, user=user).select_related()
 
17
        read_map = dict((x.topic.id, x) for x in reads)
 
18
 
 
19
        for post in qs:
 
20
            post.topic._read = read_map.get(post.topic.id, None)
 
21
        return qs
 
22
    else:
 
23
        raise Exception('cache_unreads could process only Post or Topic querysets')