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})