def create_topic(forum_pk, title, subtitle, text): """Create a new topic in a forum using the bot. Args: forum: forum instance identifier to post in title: title of the topic subtitle: subtitle of the topic, can be None text: content of the post in markdown """ # Create topic topic = Topic(title=title, subtitle=subtitle, author_id=BOT_USER_PK, pubdate=datetime.now(), forum_id=forum_pk) # Save topic topic.save() # Create first post post = Post(topic=topic, text=text, pubdate=datetime.now(), position_in_topic=1, author_id=BOT_USER_PK) # Save post post.save() # Finally update topic topic.last_message = post topic.save()
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 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 })