def edit_post(request, id): """Render and process a form to modify an existing post. Required arguments: - id => the unique ID of the post to edit (as an integer) If 'delete=true' appears in the request's query string, the post will be marked as deleted (i.e., post.deleted will be set to True), but the post instance will not actually deleted from the database. """ log_page_view(request, 'Edit Post') post = get_object_or_404(Post, id=id) profile = request.user.get_profile() if post.user != profile and profile not in post.thread.forum.moderators.all() and not profile.is_admin(): return HttpResponseRedirect(reverse('forbidden')) if request.method == 'POST': post.body = _bb_code_unescape(post.body) form = PostForm(request.POST, instance=post) if form.is_valid(): post = form.save(commit=False) post.body = _bb_code_escape(post.body) post.updated_by = profile post.save() return HttpResponseRedirect(post.get_absolute_url()) else: if 'delete' in request.GET and request.GET.get('delete') == 'true': post.deleted = True post.updated_by = profile post.save() return HttpResponseRedirect(post.thread.get_absolute_url()) post.body = _bb_code_unescape(post.body) form = PostForm(instance=post) return render(request, 'forums/add_post.html', {'form': form, 'forum': post.thread.forum, 'thread': post.thread, 'quote': post.quote, 'create': False, 'post': post}, context_instance=RequestContext(request))
def add_post(request, forum, id, thread): """Render and process a form to create a new post. Required arguments: - forum => the slug of the forum to which the new post's thread belongs (as a string) - id => the unique ID of the thread to which the new post should belong (as an integer) - thread => the slug of the thread to which the new post should belong (as a string) """ log_page_view(request, 'Reply to Thread') forum = get_object_or_404(Forum, slug=forum) thread = get_object_or_404(Thread, id=id) if request.method == 'POST': quote = None if 'quote' in request.POST: try: quote = Post.objects.get(thread=thread, number=int(request.POST.get('quote'))) except Post.DoesNotExist: pass form = PostForm(request.POST) if form.is_valid(): profile = request.user.get_profile() post = form.save(commit=False) post.thread = thread post.user = profile post.updated_by = profile post.deleted = False post.number = Post.objects.filter(thread=thread).count() + 1 if quote is not None: post.quote = quote post.body = _bb_code_escape(post.body) post.save() thread.updated = datetime.now() thread.save() # set the thread's updated time to now, since the thread has a new post return HttpResponseRedirect(post.get_absolute_url()) else: quote = None if 'quote' in request.GET: try: quote = Post.objects.get(thread=thread, number=int(request.GET.get('quote'))) except Post.DoesNotExist: pass form = PostForm() return render(request, 'forums/add_post.html', {'form': form, 'forum': forum, 'thread': thread, 'quote': quote, 'create': True}, context_instance=RequestContext(request))
def add_post(request, forum, id, thread): """Render and process a form to create a new post. Required arguments: - forum => the slug of the forum to which the new post's thread belongs (as a string) - id => the unique ID of the thread to which the new post should belong (as an integer) - thread => the slug of the thread to which the new post should belong (as a string) """ log_page_view(request, 'Reply to Thread') forum = get_object_or_404(Forum, slug=forum) thread = get_object_or_404(Thread, id=id) if request.method == 'POST': quote = None if 'quote' in request.POST: try: quote = Post.objects.get(thread=thread, number=int(request.POST.get('quote'))) except Post.DoesNotExist: pass form = PostForm(request.POST) if form.is_valid(): profile = request.user.get_profile() post = form.save(commit=False) post.thread = thread post.user = profile post.updated_by = profile post.deleted = False post.number = Post.objects.filter(thread=thread).count() + 1 if quote is not None: post.quote = quote post.body = _bb_code_escape(post.body) post.save() thread.updated = datetime.now() thread.save( ) # set the thread's updated time to now, since the thread has a new post return HttpResponseRedirect(post.get_absolute_url()) else: quote = None if 'quote' in request.GET: try: quote = Post.objects.get(thread=thread, number=int(request.GET.get('quote'))) except Post.DoesNotExist: pass form = PostForm() return render(request, 'forums/add_post.html', { 'form': form, 'forum': forum, 'thread': thread, 'quote': quote, 'create': True }, context_instance=RequestContext(request))
def edit_post(request, id): """Render and process a form to modify an existing post. Required arguments: - id => the unique ID of the post to edit (as an integer) If 'delete=true' appears in the request's query string, the post will be marked as deleted (i.e., post.deleted will be set to True), but the post instance will not actually deleted from the database. """ log_page_view(request, 'Edit Post') post = get_object_or_404(Post, id=id) profile = request.user.get_profile() if post.user != profile and profile not in post.thread.forum.moderators.all( ) and not profile.is_admin(): return HttpResponseRedirect(reverse('forbidden')) if request.method == 'POST': post.body = _bb_code_unescape(post.body) form = PostForm(request.POST, instance=post) if form.is_valid(): post = form.save(commit=False) post.body = _bb_code_escape(post.body) post.updated_by = profile post.save() return HttpResponseRedirect(post.get_absolute_url()) else: if 'delete' in request.GET and request.GET.get('delete') == 'true': post.deleted = True post.updated_by = profile post.save() return HttpResponseRedirect(post.thread.get_absolute_url()) post.body = _bb_code_unescape(post.body) form = PostForm(instance=post) return render(request, 'forums/add_post.html', { 'form': form, 'forum': post.thread.forum, 'thread': post.thread, 'quote': post.quote, 'create': False, 'post': post }, context_instance=RequestContext(request))