def new(request, forum_pk): """Creates a new topic in a forum. Returns: HttpResponse """ forum = get_object_or_404(Forum, pk=forum_pk) if request.method == 'POST': form = TopicForm(request.POST) if 'preview' in request.POST: return render_template('forum/new.html', { 'forum': forum, 'form': form, 'text': form.data['text'] }) if form.is_valid(): data = form.data # Creating the thread n_topic = Topic() n_topic.forum = forum n_topic.title = data['title'] n_topic.subtitle = data['subtitle'] n_topic.pubdate = datetime.now() n_topic.author = request.user n_topic.save() # Adding the first message post = Post() post.topic = n_topic post.author = request.user post.text = data['text'] post.pubdate = datetime.now() post.position_in_topic = 1 post.save() # Updating the topic n_topic.last_message = post n_topic.save() # Make the current user to follow his created topic follow(n_topic) return redirect(n_topic.get_absolute_url()) else: form = TopicForm() return render_template('forum/new.html', { 'form': form, 'forum': forum })
def new(request, forum_pk): """Creates a new topic in a forum. Returns: HttpResponse """ forum = get_object_or_404(Forum, pk=forum_pk) if request.method == 'POST': form = TopicForm(request.POST) if 'preview' in request.POST: return render_template('forum/new.html', { 'forum': forum, 'form': form, 'text': form.data['text'] }) if form.is_valid(): data = form.data # Creating the thread n_topic = Topic() n_topic.forum = forum n_topic.title = data['title'] n_topic.subtitle = data['subtitle'] n_topic.pubdate = datetime.now() n_topic.author = request.user n_topic.save() # Adding the first message post = Post() post.topic = n_topic post.author = request.user post.text = data['text'] post.pubdate = datetime.now() post.position_in_topic = 1 post.save() # Updating the topic n_topic.last_message = post n_topic.save() # Make the current user to follow his created topic follow(n_topic) return redirect(n_topic.get_absolute_url()) else: form = TopicForm() return render_template('forum/new.html', {'form': form, 'forum': forum})
def answer(request, topic_pk): """Adds an answer from an user to a topic. Returns: HttpResponse """ g_topic = get_object_or_404(Topic, pk=topic_pk) posts = Post.objects.filter(topic=g_topic) \ .order_by('-pubdate')[:3] last_post_pk = g_topic.last_message.pk # Making sure posting is allowed if g_topic.is_locked: raise PermissionDenied # Check that the user isn't spamming if g_topic.antispam(request.user): raise PermissionDenied # If we just sent data if request.method == 'POST': data = request.POST newpost = last_post_pk != int(data['last_post']) # Using the preview button, the more button or new post if 'preview' in data or 'more' in data or newpost: return render_template('forum/answer.html', { 'text': data['text'], 'topic': g_topic, 'posts': posts, 'last_post_pk': last_post_pk, 'newpost': newpost }) # Saving the message else: form = PostForm(request.POST) if form.is_valid(): data = form.data post = Post() post.topic = g_topic post.author = request.user post.text = data['text'] post.pubdate = datetime.now() post.position_in_topic = g_topic.get_post_count() + 1 post.save() g_topic.last_message = post g_topic.save() # Follow topic on answering if not g_topic.is_followed(): follow(g_topic) return redirect(post.get_absolute_url()) else: raise Http404 else: text = '' # Using the quote button if 'cite' in request.GET: post_cite_pk = request.GET['cite'] post_cite = Post.objects.get(pk=post_cite_pk) if post_cite.is_moderated: raise PermissionDenied for line in post_cite.text.splitlines(): text = text + '> ' + line + '\n' text = u'**{0} a écrit :**\n{1}\n'.format( post_cite.author.username, text) return render_template('forum/answer.html', { 'topic': g_topic, 'text': text, 'posts': posts, 'last_post_pk': last_post_pk })
def edit(request): """Edit a topic. Returns: HttpResponse """ try: topic_pk = request.POST['topic'] except KeyError: raise Http404 try: page = int(request.POST['page']) except KeyError: page = 1 data = request.POST resp = {} g_topic = get_object_or_404(Topic, pk=topic_pk) if request.user.is_authenticated(): # User actions if 'follow' in data: resp['follow'] = follow(g_topic) if request.user == g_topic.author: # Author actions if 'solved' in data: g_topic.is_solved = not g_topic.is_solved resp['solved'] = g_topic.is_solved if request.user.has_perm('forum.change_topic'): # Staff actions using AJAX, we are using the op_ prefix in order to # distinguish staff commands from user commands, since staff commands # need to be parsed differently since we are using some tricky JS to # retrieve the value of the Foundation switch buttons. if 'op_lock' in data: g_topic.is_locked = data['op_lock'] == 'true' if 'op_sticky' in data: g_topic.is_sticky = data['op_sticky'] == 'true' if 'op_solved' in data: g_topic.is_solved = data['op_solved'] == 'true' if 'move' in data: try: forum_pk = int(request.POST['move_target']) except KeyError: raise Http404 forum = get_object_or_404(Forum, pk=forum_pk) g_topic.forum = forum # Save the changes made on the topic g_topic.save() if request.is_ajax(): # If our request is made with AJAX, we return a JSON document in order # to update the buttons on the same page without reolading it. resp = { 'lock': g_topic.is_locked, 'sticky': g_topic.is_sticky, 'solved': g_topic.is_solved, 'follow': g_topic.is_followed(request.user), } return HttpResponse(json.dumps(resp), content_type='application/json') else: # Elsewise this is a regular POST request so we redirect the user back # to the topic. return redirect(u'{}?page={}'.format(g_topic.get_absolute_url(), page))
def answer(request, topic_pk): """Adds an answer from an user to a topic. Returns: HttpResponse """ g_topic = get_object_or_404(Topic, pk=topic_pk) posts = Post.objects.filter(topic=g_topic) \ .order_by('-pubdate')[:3] last_post_pk = g_topic.last_message.pk # Making sure posting is allowed if g_topic.is_locked: raise PermissionDenied # Check that the user isn't spamming if g_topic.antispam(request.user): raise PermissionDenied # If we just sent data if request.method == 'POST': data = request.POST newpost = last_post_pk != int(data['last_post']) # Using the preview button, the more button or new post if 'preview' in data or 'more' in data or newpost: return render_template( 'forum/answer.html', { 'text': data['text'], 'topic': g_topic, 'posts': posts, 'last_post_pk': last_post_pk, 'newpost': newpost }) # Saving the message else: form = PostForm(request.POST) if form.is_valid(): data = form.data post = Post() post.topic = g_topic post.author = request.user post.text = data['text'] post.pubdate = datetime.now() post.position_in_topic = g_topic.get_post_count() + 1 post.save() g_topic.last_message = post g_topic.save() # Follow topic on answering if not g_topic.is_followed(): follow(g_topic) return redirect(post.get_absolute_url()) else: raise Http404 else: text = '' # Using the quote button if 'cite' in request.GET: post_cite_pk = request.GET['cite'] post_cite = Post.objects.get(pk=post_cite_pk) if post_cite.is_moderated: raise PermissionDenied for line in post_cite.text.splitlines(): text = text + '> ' + line + '\n' text = u'**{0} a écrit :**\n{1}\n'.format( post_cite.author.username, text) return render_template( 'forum/answer.html', { 'topic': g_topic, 'text': text, 'posts': posts, 'last_post_pk': last_post_pk })