Exemple #1
0
def post_new(request, topic_id, quoted_post_id=None):
	topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id)
	if topic.is_closed:
		if not request.user.is_staff:
			messages.error(request, "You are not allowed to post in closed topics.")
			return redirect(topic.get_absolute_url())
		else:
			messages.info(request, "Note: This topic is closed.")
	if not can_post_reply(request.user, topic.forum):
		messages.error(request, "You are not allowed to reply on this forum.")
		return redirect(topic.get_absolute_url())
	if request.method == 'POST':
		form = PostForm(request.POST)
		if form.is_valid():
			content = form.cleaned_data['content']
			post = Post(topic=topic, author=request.user,
				author_ip=request.META['REMOTE_ADDR'], content=content)
			post.save()
			messages.success(request, "Your reply has been saved.")
			return redirect(post.get_absolute_url())
	else:
		form = PostForm()
		if quoted_post_id:
			try:
				quoted_post = Post.objects.get(pk=quoted_post_id, topic=topic)
				form.initial = {'content': "[quote=%s]%s[/quote]" %
					(quoted_post.author, quoted_post.content)}
			except Post.DoesNotExist:
				messages.warning(request, "You tried to quote a post which "
					"doesn't exist or it doesn't belong to topic you are "
					"replying to. Nice try, Kevin.")
	return {'topic': topic, 'form': form}
Exemple #2
0
def post_edit(request, post_id):
	post = get_object_or_404(Post.objects.select_related(), pk=post_id)
	if not request.user.is_staff:
		if post.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.author == request.user:
			messages.error(request, "You are not allowed to edit this post.")
			return redirect(topic.get_absolute_url())
	if request.method == 'POST':
		form = PostForm(request.POST)
		if form.is_valid():
			content = form.cleaned_data['content']
			post.content = content
			post.modified_by = request.user
			post.save()
			messages.success(request, "Post has been saved.")
			return redirect(post.get_absolute_url())
	else:
		form = PostForm()
		form.initial = {'content': post.content}
	return {'post': post, 'form': form}