def topic_new(request, category_id): category = get_object_or_404(Category, pk=category_id) if not request.user.has_perm('forums.add_topics_category', category): messages.error(request, "You are not allowed to post new topics in this category." ) return redirect(category.get_absolute_url()) topic_form = TopicNewForm() post_form = PostNewForm() extra_context = { 'category': category, } if request.method == 'POST': topic_form = TopicNewForm(request.POST) post_form = PostNewForm(request.POST) if topic_form.is_valid() and post_form.is_valid(): if request.POST.get('preview'): extra_context['post_preview'] = markdown(post_form \ .cleaned_data.get('content')) else: new_topic = topic_form.save(commit=False) new_topic.created_by = request.user new_topic.category = category new_topic.save() new_post = post_form.save(commit=False) new_post.created_by = request.user new_post.topic = new_topic new_post.save() messages.success(request, "Successfully created a new topic.") return redirect(new_topic.get_absolute_url()) extra_context['topic_form'] = topic_form extra_context['post_form'] = post_form return TemplateResponse(request, 'forums/topic_new.html', extra_context)
def post_edit(request, post_id): post = get_object_or_404(Post, pk=post_id) topic = post.topic if not request.user.has_perm('forums.moderate_category', topic.category): if topic.is_closed: messages.error(request, "You are not allowed to edit posts in closed topics." ) return redirect(topic.get_absolute_url()) if not post.created_by == request.user or not post.id == topic.last_post.id: # add a timedelta condition messages.error(request, "You are not allowed to edit this post.") return redirect(topic.get_absolute_url()) form = PostNewForm(instance=post) extra_context = { 'post': post, } if request.method == 'POST': form = PostNewForm(request.POST, instance=post) if form.is_valid(): if request.POST.get('preview'): extra_context['post_preview'] = markdown(form \ .cleaned_data.get('content')) else: form.save() messages.success(request, "{0} has been updated.".format("Your post" \ if request.user == post.created_by else "Post") ) return redirect(post.get_absolute_url()) extra_context['form'] = form return TemplateResponse(request, 'forums/post_edit.html', extra_context)
def post_new(request, topic_id): topic = get_object_or_404(Topic, pk=topic_id) if not request.user.has_perm('forums.add_posts_category', topic.category): messages.error(request, "You are not allowed to post new replies in this category." ) return redirect(topic.get_absolute_url()) if topic.is_closed: if request.user.has_perm('forums.add_posts_category', topic.category): messages.info(request, "This topic is closed.") else: messages.error(request, "You are not allowed to post in closed topics." ) return redirect(topic.get_absolute_url()) form = PostNewForm() extra_context = { 'topic': topic, } if request.method == 'POST': form = PostNewForm(request.POST) if form.is_valid(): if request.POST.get('preview'): extra_context['post_preview'] = markdown(form \ .cleaned_data.get('content')) else: new_post = form.save(commit=False) new_post.created_by = request.user new_post.topic = topic new_post.save() messages.success(request, "Your reply has been saved." ) return redirect(new_post.get_absolute_url()) extra_context['form'] = form return TemplateResponse(request, 'forums/post_new.html', extra_context)