def edit_post(request, post_pk): """Edit a specific post. Returns: HttpResponse """ post = get_object_or_404(Post, pk=post_pk) # If we are editing the first post, we also want to edit the topic g_topic = None if post.position_in_topic == 1: g_topic = get_object_or_404(Topic, pk=post.topic.pk) by_staff = (post.author != request.user) \ and request.user.has_perm('forum.change_post') # - non-moderated posts may be edited by their author or moderators; # - moderated posts only by moderators if (post.author != request.user) \ and not by_staff: raise PermissionDenied if by_staff and request.method == 'GET': messages.add_message( request, messages.WARNING, u'Vous vous apprêtez à éditer un message en tant que modérateur ' u'(auteur : {}).' .format(post.author.username)) if request.method == 'POST': # Using the preview button if 'preview' in request.POST: if g_topic: g_topic = Topic(title=request.POST['title'], subtitle=request.POST['subtitle']) return render_template('forum/edit_post.html', { 'post': post, 'topic': g_topic, 'text': request.POST['text'], }) # Else save changes to database post.text = request.POST['text'] post.update = datetime.now() post.save() # Modifying the thread info if first post if g_topic: g_topic.title = request.POST['title'] g_topic.subtitle = request.POST['subtitle'] g_topic.save() return redirect(post.get_absolute_url()) else: return render_template('forum/edit_post.html', { 'post': post, 'topic': g_topic, 'text': post.text })
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 edit_post(request, post_pk): """Edit a specific post. Returns: HttpResponse """ post = get_object_or_404(Post, pk=post_pk) # If we are editing the first post, we also want to edit the topic g_topic = None if post.position_in_topic == 1: g_topic = get_object_or_404(Topic, pk=post.topic.pk) by_staff = (post.author != request.user) \ and request.user.has_perm('forum.change_post') # - non-moderated posts may be edited by their author or moderators; # - moderated posts only by moderators if (post.author != request.user) and not by_staff: raise PermissionDenied if by_staff and request.method == 'GET': messages.add_message( request, messages.WARNING, u'Vous vous apprêtez à éditer un message en tant que modérateur ' u'(auteur : {}).'.format(post.author.username)) if request.method == 'POST': # Using the preview button if 'preview' in request.POST: if g_topic: g_topic = Topic(title=request.POST['title'], subtitle=request.POST['subtitle']) return render_template('forum/edit_post.html', { 'post': post, 'topic': g_topic, 'text': request.POST['text'], }) # Else save changes to database post.text = request.POST['text'] post.update = datetime.now() post.save() # Modifying the thread info if first post if g_topic: g_topic.title = request.POST['title'] g_topic.subtitle = request.POST['subtitle'] g_topic.save() return redirect(post.get_absolute_url()) else: return render_template('forum/edit_post.html', { 'post': post, 'topic': g_topic, 'text': post.text })
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})