def index(request): users_online = cache.get('forums_users_online') if users_online == None: users_online = list(User.objects.filter(is_active=True, profile__last_activity_at__gte=(datetime.now()-timedelta(minutes=15)))) cache.set('forums_users_online', users_online) tracker = ObjectTracker(request.session) forums = list(Forum.objects.annotate(num_can_read=Count('can_read')). \ select_related('forums', 'last_post__topic', 'last_post__author')) forums = [i for i in forums if can_access_forum(request, i, return_plain_boolean=True)] for forum in forums: forum.has_new_posts = not tracker.has_viewed(forum.last_post, 'created_at') \ if forum.last_post else False forum.has_new_topics = not tracker.has_viewed(forum.last_topic, 'created_at') \ if forum.last_topic else False posts = cache.get('forums_count_posts') if posts == None: posts = Post.objects.count() cache.set('forums_count_posts', posts) topics = cache.get('forums_count_topics') if topics == None: topics = Topic.objects.count() cache.set('forums_count_topics', topics) users = cache.get('forums_count_users') if users == None: users = User.objects.exclude(is_active=False).count() cache.set('forums_count_users', users) topics = cache.get('forums_latest_topics') if topics == None: topics = list(Topic.objects.order_by('-is_sticky', '-last_post__created_at').select_related('author', 'last_post__author')) cache.set('forums_latest_topics', topics) tracker = ObjectTracker(request.session) for topic in topics: topic.has_new_posts = not tracker.has_viewed(topic.last_post, 'created_at') \ if topic.last_post else False return {'forums': forums, 'users_online': users_online, 'posts': posts, 'topics': topics, 'users': users}
def topic_view(request, topic_id): topic = cache.get('forums_topic_%s' % topic_id) if topic == None: topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id) cache.set('forums_topic_%s' % topic_id, topic) can_access = can_access_forum(request, topic.forum) if not can_access == True: return can_access tracker = ObjectTracker(request.session) Topic.objects.filter(pk=topic_id).update(view_count=F('view_count') + 1) # FIXME ++ with cache posts = cache.get('forums_posts_%s' % topic_id) if posts == None: posts = list(Post.objects.filter(topic__pk=topic_id) \ .select_related('author__profile__group', 'karma')) cache.set('forums_posts_%s' % topic_id, posts) for post in posts: post.is_unread = not tracker.has_viewed(post, 'created_at') tracker.bulk_mark_as_viewed(posts) return {'topic': topic, 'posts': posts, 'first_post_id': posts[0].id}
def forum_view(request, forum_id): forum = cache.get('forums_forum_%s' % forum_id) if forum == None: forum = get_object_or_404(Forum.objects \ .annotate(num_can_read=Count('can_read')), pk=forum_id) cache.set('forums_forum_%s' % forum_id, forum) can_access = can_access_forum(request, forum) if not can_access == True: return can_access topics = cache.get('forums_topics_%s' % forum_id) if topics == None: topics = list(Topic.objects.filter(forum__pk=forum_id). \ order_by('-is_sticky', '-created_at'). \ select_related('author', 'last_post__author')) cache.set('forums_topics_%s' % forum_id, topics) tracker = ObjectTracker(request.session) for topic in topics: topic.has_new_posts = not tracker.has_viewed(topic.last_post, 'created_at') \ if topic.last_post else False return {'forum': forum, 'topics': topics}
def with_tracking(queryset, session, date_field=None): tracker = ObjectTracker(session) for obj in queryset: yield obj, tracker.has_viewed(obj, date_field)
def get_first_unread_post(topic, request): tracker = ObjectTracker(request.session) for post in topic.posts.all(): if not tracker.has_viewed(post, 'created_at'): return post return None