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}
def process_post_form(request, forum_slug, thread_slug, post_id=None): """ Processes a form for a post, both create and edit. """ created = not post_id # Makes sure that the user is posting to an existing thread thread = get_object_or_404(Thread.objects.select_related('forum'), slug=thread_slug, forum__slug=forum_slug, forum__site=settings.SITE_ID) if not created: # User is editing a post post = get_object_or_404(Post, pk=post_id) if post.author != request.user: raise HttpResponseForbidden() else: # User is creating a post post = Post(author=request.user, thread=thread) form = PostForm(request.POST, instance=post) if form.is_valid(): form.save() if created: # Saves the thread without any changes so that its modified field gets updated # and it gets moved to the top of its forum's thread list # Only do this if it's a new post, not an edit of an existing post thread.save() if not created: messages.success(request, "Your post was updated successfully.") else: messages.success(request, "Your post was successful.") return redirect(thread.get_absolute_url())
def edit_post(request, post): post = get_object_or_404(Post, pk=post) user = request.user if not (user.is_authenticated() and (user==post.user or user.has_perm('forums.can_moderate') or user.is_superuser)): raise PermissionDenied if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): if form.cleaned_data['thread_id'] != post.thread.id or form.cleaned_data['post_id'] != post.id: raise PermissionDenied post.text = form.cleaned_data['text'] post.save() return HttpResponseRedirect(reverse('thread-page_view', args=(post.thread.id,1))) else: form = PostForm({'text': post.text, 'thread_id': post.thread.id, 'post_id': post.id}) return render_to_response('forums/edit_post.html',{'form':form,'post':post}, context_instance=RequestContext(request))
def show_thread(request, thread, page=1): thread = get_object_or_404(Thread, pk=thread) if request.method == 'GET': form = None if request.user.is_authenticated(): posts_per_page = request.user.profile.ppp if thread.category in request.user.profile.group.allowed_reply.all(): if not thread.locked: form = PostForm({'thread_id':thread.id}) if thread.category not in request.user.profile.group.allowed_read.all(): raise PermissionDenied tracker = thread.getReadTracker(request.user) tracker.update() else: posts_per_page = 10 if not thread.category.anon_viewable : raise PermissionDenied elif request.method == 'POST': if not request.user.is_authenticated() or thread.locked : return HttpResponseRedirect(reverse('thread-page_view', args=(thread.id,page))) form = PostForm(request.POST) if form.is_valid(): if form.cleaned_data['thread_id'] != thread.id: raise PermissionDenied post = Post(user = request.user, thread = thread, text = form.cleaned_data['text']) post.save() page = (thread.post_count()/request.user.profile.ppp)+1 return HttpResponseRedirect(reverse('thread-page_view', args=(thread.id,page))) else: posts_per_page = request.user.profile.ppp pages = Paginator(thread.post_set.all().order_by('pk'), posts_per_page) try: posts = pages.page(page) except PageNotAnInteger: posts = pages.page(1) except EmptyPage: posts = pages.page(pages.num_pages) return render_to_response('forums/thread.html',{'thread':thread, 'posts':posts, 'form': form}, context_instance=RequestContext(request))
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}
def topic_thread(request, slug): template_name = 'topics/thread.html' context = {} if slug is None or slug == '': return redirect('not-found') topic = Topic.objects.get(slug=slug) if topic is None: return redirect('not-found') form = PostForm() context['topic'] = topic context['form'] = form return render(request, template_name, context)