Example #1
0
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,
    }
Example #2
0
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,
    }