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 qmod_topic_split(request, topic): post_ids = request.POST.getlist('posts_selected') if not post_ids: messages.error(request, "You haven't selected any post.") return if not 'confirmation' in request.POST: messages.info(request, "Are you sure you want to split selected posts?") return { 'form': TopicNewForm() } form = TopicNewForm(request.POST) if form.is_valid(): posts = Post.objects.filter(topic=topic, pk__in=post_ids) if topic.first_post_id in post_ids: posts.exclude(pk=topic.first_post_id) if not len(posts): return new_topic = form.save(commit=False) new_topic.created_by = posts[0].created_by new_topic.title = form.cleaned_data.get('title') new_topic.forum = topic.forum new_topic.save() posts.update(topic=new_topic) messages.success(request, "Selected posts have been splitted out of original topic.") return redirect(new_topic.get_absolute_url())