def items(self, topic): """ Returns new posts in the specified topic, if anonymous user can access it """ get_group_perms_or_404(AnonymousUser(), topic.forum) posts = topic.posts.order_by('-date').select_related()[:settings.FORUM_FEED_ITEMS_COUNT] return posts
def items(self, forum): """ Returns new topics in the specified forum, if anonymous users can access it """ get_group_perms_or_404(AnonymousUser(), forum) topics = forum.topics.order_by('-id').select_related()[:settings.FORUM_FEED_ITEMS_COUNT] return topics
def topic_mark_read(request, topic_id): """ Mark all posts in specified topic as read and redirect to the topic list page """ topic = get_object_or_404(Topic, id=topic_id) get_group_perms_or_404(request.user, topic.forum) profile = request.user.get_profile() readtracking = profile.readtracking readtracking.last_read[topic.id] = topic.last_post.id readtracking.save() return HttpResponseRedirect(topic.forum.get_absolute_url())
def post_edit(request, post_id): """ Post edit """ post = get_object_or_404(Post, id=post_id) topic = post.topic forum = topic.forum group, perms = get_group_perms_or_404(request.user, forum) if perms.can_change_post(request.user, post): if request.method == 'POST': post_form = PostForm(request.POST, instance=post, prefix='post_form') if post_form.is_valid(): post = post_form.save() return HttpResponseRedirect(post.get_absolute_url()) else: post_form = PostForm(instance=post, prefix='post_form') else: return HttpResponseRedirect(post.get_absolute_url()) return { 'forum': forum, 'topic': topic, 'post': post, 'post_form': post_form, }
def topic_split(request, topic_id): """ Split topic """ topic = get_object_or_404(Topic, id=topic_id) forum = topic.forum group, perms = get_group_perms_or_404(request.user, forum) if perms.can_split_topic(request.user, topic): if request.method == 'POST': topic_form = TopicSplitForm(request.POST, topic=topic) if topic_form.is_valid(): new_topic = topic_form.save() return HttpResponseRedirect(new_topic.get_absolute_url()) else: topic_form = TopicSplitForm(topic=topic) else: return HttpResponseRedirect(topic.get_absolute_url()) return { 'forum': forum, 'topic': topic, 'topic_form': topic_form, }
def post_move(request, topic_id): """ Move posts to another topic """ topic = get_object_or_404(Topic, id=topic_id) forum = topic.forum group, perms = get_group_perms_or_404(request.user, forum) if perms.can_move_post(request.user, topic): if request.method == 'POST': topic_form = PostMoveForm(request.POST, user=request.user, topic_src=topic) if topic_form.is_valid(): other_topic = topic_form.save() return HttpResponseRedirect(other_topic.get_absolute_url()) else: topic_form = PostMoveForm(user=request.user, topic_src=topic) else: return HttpResponseRedirect(topic.get_absolute_url()) return { 'forum': forum, 'topic': topic, 'topic_form': topic_form, }
def topic_move(request, topic_id): """ Move topic to another forum """ topic = get_object_or_404(Topic, id=topic_id) forum = topic.forum group, perms = get_group_perms_or_404(request.user, forum) if perms.can_move_topic(request.user, topic): if request.method == 'POST': topic_form = TopicMoveForm(request.POST, instance=topic) if topic_form.is_valid(): topic_form.save() forum.update_topics_count() forum.update_posts_count() forum.update_last_post() forum.save() return HttpResponseRedirect(topic.get_absolute_url()) else: topic_form = TopicMoveForm(instance=topic) else: return HttpResponseRedirect(topic.get_absolute_url()) return { 'forum': forum, 'topic': topic, 'topic_form': topic_form, }
def forum_mark_read(request, forum_id): """ Marks all topics in the specified forum as read and redirects to the forum main page """ forum = get_object_or_404(Forum, id=forum_id) get_group_perms_or_404(request.user, forum) profile = request.user.get_profile() readtracking = profile.readtracking for topic in forum.topics.only('id', 'last_post').select_related('last_post'): readtracking.last_read[topic.id] = topic.last_post.id readtracking.save() return HttpResponseRedirect(reverse('forum'))
def topic_delete(request, topic_id): """ Delete topic and redirect to topic list page """ topic = get_object_or_404(Topic, id=topic_id) group, perms = get_group_perms_or_404(request.user, topic.forum) if perms.can_delete_topic(request.user, topic): topic.remove() return HttpResponseRedirect(topic.forum.get_absolute_url())
def topic_close(request, topic_id): """ Close / open topic """ topic = get_object_or_404(Topic, id=topic_id) group, perms = get_group_perms_or_404(request.user, topic.forum) if perms.can_close_topic(request.user, topic): topic.is_closed = not topic.is_closed topic.save() return HttpResponseRedirect(topic.get_absolute_url())
def topic_stick(request, topic_id): """ Stick / unstick topic """ topic = get_object_or_404(Topic, id=topic_id) group, perms = get_group_perms_or_404(request.user, topic.forum) if perms.can_stick_topic(request.user, topic): topic.is_sticky = not topic.is_sticky topic.save() return HttpResponseRedirect(topic.get_absolute_url())
def post_delete(request, post_id): """ Delete post and redirect back to page """ post = get_object_or_404(Post, id=post_id) group, perms = get_group_perms_or_404(request.user, post.topic.forum) if perms.can_delete_post(request.user, post): post.remove() # Redirect to post before deleted one prev_post = post.topic.posts.filter(date__lt=post.date).reverse()[0] return HttpResponseRedirect(prev_post.get_absolute_url())
def topic_edit(request, topic_id): """ Edit topic and first post """ topic = get_object_or_404(Topic, id=topic_id) post = topic.first_post forum = topic.forum group, perms = get_group_perms_or_404(request.user, forum) if perms.can_change_topic(request.user, topic): PollFormset = inlineformset_factory(Topic, Poll, fk_name='topic', form=PollForm, max_num=10, extra=10) if request.method == 'POST': topic_form = TopicForm(request.POST, instance=topic, prefix='topic_form') post_form = PostForm(request.POST, instance=post, prefix='post_form') poll_formset = PollFormset(request.POST, instance=topic) if topic_form.is_valid() and post_form.is_valid() and poll_formset.is_valid(): topic = topic_form.save() post = post_form.save() poll_formset.save() # Save poll choices for form in poll_formset.forms: if form in poll_formset.deleted_forms: continue if form.instance.total_votes == 0: form.save_choices() return HttpResponseRedirect(post.get_absolute_url()) else: topic_form = TopicForm(instance=topic, prefix='topic_form') post_form = PostForm(instance=post, prefix='post_form') poll_formset = PollFormset(instance=topic) else: return HttpResponseRedirect(topic.get_absolute_url()) return { 'forum': forum, 'topic': topic, 'post': post, 'topic_form': topic_form, 'post_form': post_form, 'poll_formset': poll_formset, }
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, }
def topic_add(request, forum_id): """ Adds a new topic """ forum = get_object_or_404(Forum, id=forum_id) group, perms = get_group_perms_or_404(request.user, forum) if perms.can_add_topic(): PollFormset = inlineformset_factory(Topic, Poll, fk_name='topic', form=PollForm, max_num=10, extra=10) if request.method == 'POST': topic_form = TopicForm(request.POST, prefix='topic_form') post_form = PostForm(request.POST, prefix='post_form') poll_formset = PollFormset(request.POST) if topic_form.is_valid() and post_form.is_valid() and poll_formset.is_valid(): profile = request.user.get_profile() ip_address = request.META.get('REMOTE_ADDR', None) topic = topic_form.save(commit=False) topic.forum = forum topic.save() post = post_form.save(commit=False) post.topic = topic post.profile = profile post.ip_address = ip_address post.save() topic.first_post = post topic.save() # Save poll poll_formset.instance = topic for form in poll_formset.forms: if hasattr(form, 'cleaned_data') and form.cleaned_data: form.cleaned_data['topic'] = topic poll_formset.save() # Save poll choices for form in poll_formset.forms: form.save_choices() # Inform subscribed users about new topic inform_new_topic(topic) # Auto subscribe user to his topic do_subscribe(profile, topic) return HttpResponseRedirect(topic.get_absolute_url()) else: topic_form = TopicForm(prefix='topic_form') post_form = PostForm(prefix='post_form') poll_formset = PollFormset() else: raise Http404 return { 'forum': forum, 'topic_form': topic_form, 'post_form': post_form, 'poll_formset': poll_formset, }