def search(request): """ Shows forum search results as post list """ if request.GET: search_form = ForumSearchForm(request.GET) search_term = '' post_list = [] if search_form.is_valid(): search_term = search_form.cleaned_data['term'] group = Group.group_for_user(request.user) visible_forums_ids = list(group.permission_set.values_list('forum_id', flat=True)) posts = Post.objects.get_visible_posts().\ filter(topic__forum__in=visible_forums_ids, message__icontains=search_term).\ select_related('profile', 'profile__user', 'profile__group', 'topic', 'topic__first_post').\ order_by('-date') post_list = paginate(request, posts, settings.POSTS_PER_PAGE) return { 'search_form': search_form, 'search_term': search_term, 'post_list': post_list, } return HttpResponseRedirect(reverse('forum'))
def accounts(request, sort_order): """ Shows registered user list, which can be searched and sorted according to several criteria """ profiles = UserProfile.objects.exclude(user__is_active=False).select_related('user') if isinstance(sort_order, (list, tuple)): profiles = profiles.order_by(*sort_order) else: profiles = profiles.order_by(sort_order) # Filter user account list with search term search_term = '' if request.GET: search_form = AccountSearchForm(request.GET) if search_form.is_valid(): term = search_form.cleaned_data['term'] if term: profiles = profiles.filter(Q(user__first_name__icontains=term) | Q(user__last_name__icontains=term)) search_term = u'term=%s' % term else: search_form = AccountSearchForm() page = paginate(request, profiles, settings.ACCOUNTS_PER_PAGE) return { 'page': page, 'search_form': search_form, 'search_term': search_term, }
def forum_subscription(request): """ Shows forum subscriptions """ profile = request.user.get_profile() subscription = load_content_objects(profile.forum_subscription.order_by('-date')) subscription_list = paginate(request, subscription, settings.SUBSCRIPTIONS_PER_PAGE) return { 'subscription_list': subscription_list, }
def topic_list(request, forum_id): """ Shows the list of topics in the specified forum """ forum = get_object_or_404(Forum, id=forum_id) group, perms = get_group_perms_or_404(request.user, forum) subscribed = is_subscribed(request.user, forum) topics = forum.topics.order_by('-is_sticky', '-last_post__date').\ select_related('last_post', 'last_post__profile', 'last_post__profile__user', 'first_post', 'first_post__profile', 'first_post__profile__user') topic_list = paginate(request, topics, settings.TOPICS_PER_PAGE) mark_unread_topics(request.user, topic_list.object_list) return { 'forum': forum, 'subscribed': subscribed, 'forum_perms': perms, 'topic_list': topic_list, }
def post_list(request, topic_id): """ Shows posts list in specified topic and process add new post """ topic = get_object_or_404(Topic.objects.get_visible_topics(), id=topic_id) forum = topic.forum group, perms = get_group_perms_or_404(request.user, forum) subscribed = is_subscribed(request.user, topic) if perms.can_add_post(topic): if request.method == 'POST': post_form = PostForm(request.POST, prefix='post_form') if post_form.is_valid(): profile = request.user.get_profile() # Glue two consecutive posts from same user if time interval between them less than GLUE_MESSAGE_TIMEOUT delta = (datetime.today()-topic.last_post.date).seconds if (topic.last_post.profile == profile) and (delta < settings.GLUE_MESSAGE_TIMEOUT): post = topic.last_post glue_message = post.message + settings.GLUE_MESSAGE % (timesince(post.date), post_form.cleaned_data['message']) data = {'message': glue_message} if PostForm(data).is_valid(): post.message = glue_message post.save() return HttpResponseRedirect(post.get_absolute_url()) ip_address = request.META.get('REMOTE_ADDR', None) post = post_form.save(commit=False) post.topic = topic post.profile = profile post.ip_address = ip_address post.save() # Inform subscribed users about new post inform_new_post(post) return HttpResponseRedirect(post.get_absolute_url()) else: post_form = PostForm(prefix='post_form') else: post_form = None posts = topic.posts.order_by('date').select_related('profile', 'profile__user', 'profile__group', 'topic', 'topic__first_post') post_list = paginate(request, posts, settings.POSTS_PER_PAGE) # Generate poll list for the first topic page if post_list.number == 1: polls = topic.polls.annotate(max_votes_count=Max('choices__votes_count')) polls_ids = list(polls.values_list('id', flat=True)) # Select dictionary of user choices for topic polls if request.user.is_authenticated(): profile = request.user.get_profile() poll_votes = dict(PollVote.objects.filter(profile=profile, poll__in=polls_ids).values_list('poll', 'choice')) else: poll_votes = {} # For every poll in sequence set: # - user_can_vote, ability user to vote in specified poll # - user_vote, user vote for specified poll for poll in polls: poll.user_can_vote = not bool(poll.expired() or request.user.is_anonymous() or bool(poll.id in poll_votes.keys())) if poll.id in poll_votes.keys(): poll.user_vote = poll_votes[poll.id] else: polls = [] mark_read(request.user, post_list.object_list) return { 'forum': forum, 'topic': topic, 'polls': polls, 'subscribed': subscribed, 'forum_perms': perms, 'post_list': post_list, 'post_form': post_form, }