def latest_threads(request): effective_prefs = (LatestThreadsForumPreference .get_effective_preferences( request.user if request.user.is_authenticated() else None)) excluded_forums = [ fpk for fpk, include in effective_prefs.items() if not include] threads = (Thread.objects.all() .filter(~Q(forum_id__in=excluded_forums)) .order_by('-last_update') .select_related('author')) threads_per_page = utils.get_config('threads_per_forum_page') paginator = utils.MappingPaginator(threads, threads_per_page) paginator.install_map_func(lambda t: utils.ThreadFascet(t, request)) page = utils.page_by_request(paginator, request) # We can apparently do aggregate queries faster than the ORM, so do that. # This is ugly but this is one of the highest traffic pages in the project # and we can make a _big_ perf difference (as in an order of magnitude) by # doing these queries together like this. with connection.cursor() as cursor: cursor.execute(""" SELECT post.thread_id, COUNT(*) FROM "ISS_post" AS post WHERE post.thread_id = ANY(%s) GROUP BY post.thread_id """, ([tf._thread.pk for tf in page],)) counts = dict(cursor.fetchall()) for tf in page: tf._thread.post_count = counts[tf._thread.pk] if request.user.is_authenticated(): ppk = request.user.pk flags = ThreadFlag.objects.raw(""" SELECT tf.* FROM "ISS_thread" AS thread JOIN "ISS_threadflag" AS tf ON tf.thread_id = thread.id AND tf.poster_id = %s WHERE thread.id = ANY(%s) """, (ppk, [tf._thread.pk for tf in page])) fd = dict([(flag.thread_id, flag) for flag in flags]) for tf in page: if tf._thread.pk in fd: tf._thread._flag_cache[ppk] = fd[tf._thread.pk] ctx = { 'rel_page': page, 'threads': page } return render(request, 'latest_threads.html', ctx)
def latest_threads(request): threads = (Thread.objects.all().filter( forum__is_trash=False).order_by('-last_update')) threads_per_page = utils.get_config('threads_per_forum_page') paginator = utils.MappingPaginator(threads, threads_per_page) paginator.install_map_func(lambda t: utils.ThreadFascet(t, request)) page = utils.page_by_request(paginator, request) ctx = {'rel_page': page, 'threads': page} return render(request, 'latest_threads.html', ctx)
def usercp(request): threads = (Thread.objects.all().filter( threadflag__poster_id=request.user.id, threadflag__subscribed=True, last_update__gt=F('threadflag__last_read_date')).order_by( '-last_update')) threads_per_page = utils.get_config('threads_per_forum_page') paginator = utils.MappingPaginator(threads, threads_per_page) paginator.install_map_func(lambda t: utils.ThreadFascet(t, request)) page = utils.page_by_request(paginator, request) ctx = {'rel_page': page, 'threads': page} return render(request, 'user_cp.html', ctx)
def thread_index(request, forum_id): forum = get_object_or_404(Forum, pk=forum_id) threads = forum.thread_set.order_by('-stickied', '-last_update') threads_per_page = utils.get_config('threads_per_forum_page') paginator = utils.MappingPaginator(threads, threads_per_page) paginator.install_map_func(lambda t: utils.ThreadFascet(t, request)) page = utils.page_by_request(paginator, request) ctx = { 'rel_page': page, 'forum': forum, 'threads': page, 'can_start_thread': forum.create_thread_pack.check_request(request) } if request.user.is_authenticated(): forum.mark_read(request.user) return render(request, 'thread_index.html', ctx)