def add_thread(request, slug): """Render and process a form to create a new thread. Required arguments: - slug => the slug of the forum to which the new thread should belong (as a string) """ log_page_view(request, 'Add Thread') forum = get_object_or_404(Forum, slug=slug) if request.method == 'POST': form = ThreadForm(request.POST) if form.is_valid(): profile = request.user.get_profile() thread = form.save(commit=False) thread.forum = forum thread.owner = profile thread.slug = slugify(thread.title) thread.save() thread.subscribers.add(profile) thread.save() body = _bb_code_escape(form.cleaned_data.get('post')) post = Post.objects.create(thread=thread, user=profile, updated_by=profile, number=1, deleted=False, body=body) post.save() # create and save the first post belonging to the new thread return HttpResponseRedirect(thread.get_absolute_url()) else: form = ThreadForm() return render(request, 'forums/add_thread.html', {'form': form, 'forum': forum, 'create': True}, context_instance=RequestContext(request))
def edit_thread(request, forum, id, thread): """Render and process a form to modify an existing thread and its first post. Required arguments: - forum => the slug of the forum to which the thread belongs (as a string) - id => the unique ID of the thread to edit (as an integer) - thread => the slug of the thread to edit (as a string) The 'thread' argument is not used by the function, but it is present for URL consistency. If 'delete=true' appears in the request's query string, the thread will be deleted, along with all of its posts. """ log_page_view(request, 'Edit Thread') forum = get_object_or_404(Forum, slug=forum) thread = get_object_or_404(Thread, id=id) first_post = get_object_or_404(Post, thread=thread, number=1) profile = request.user.get_profile() if thread.owner != profile and profile not in thread.forum.moderators.all() and not profile.is_admin(): return HttpResponseRedirect(reverse('forbidden')) if request.method == 'POST': form = ThreadForm(request.POST, instance=thread) if form.is_valid(): form.save() first_post.body = _bb_code_escape(form.cleaned_data.get('post')) first_post.updated_by = profile first_post.save() # update and save the thread's first post return HttpResponseRedirect(thread.get_absolute_url()) else: if 'delete' in request.GET and request.GET.get('delete') == 'true': thread.delete() return HttpResponseRedirect(forum.get_absolute_url()) form = ThreadForm(instance=thread, initial={'post': _bb_code_unescape(first_post.body)}) return render(request, 'forums/add_thread.html', {'form': form, 'forum': forum, 'thread': thread, 'create': False}, context_instance=RequestContext(request))
def edit_thread(request, forum, id, thread): """Render and process a form to modify an existing thread and its first post. Required arguments: - forum => the slug of the forum to which the thread belongs (as a string) - id => the unique ID of the thread to edit (as an integer) - thread => the slug of the thread to edit (as a string) The 'thread' argument is not used by the function, but it is present for URL consistency. If 'delete=true' appears in the request's query string, the thread will be deleted, along with all of its posts. """ log_page_view(request, 'Edit Thread') forum = get_object_or_404(Forum, slug=forum) thread = get_object_or_404(Thread, id=id) first_post = get_object_or_404(Post, thread=thread, number=1) profile = request.user.get_profile() if thread.owner != profile and profile not in thread.forum.moderators.all( ) and not profile.is_admin(): return HttpResponseRedirect(reverse('forbidden')) if request.method == 'POST': form = ThreadForm(request.POST, instance=thread) if form.is_valid(): form.save() first_post.body = _bb_code_escape(form.cleaned_data.get('post')) first_post.updated_by = profile first_post.save() # update and save the thread's first post return HttpResponseRedirect(thread.get_absolute_url()) else: if 'delete' in request.GET and request.GET.get('delete') == 'true': thread.delete() return HttpResponseRedirect(forum.get_absolute_url()) form = ThreadForm(instance=thread, initial={'post': _bb_code_unescape(first_post.body)}) return render(request, 'forums/add_thread.html', { 'form': form, 'forum': forum, 'thread': thread, 'create': False }, context_instance=RequestContext(request))
def add_thread(request, slug): """Render and process a form to create a new thread. Required arguments: - slug => the slug of the forum to which the new thread should belong (as a string) """ log_page_view(request, 'Add Thread') forum = get_object_or_404(Forum, slug=slug) if request.method == 'POST': form = ThreadForm(request.POST) if form.is_valid(): profile = request.user.get_profile() thread = form.save(commit=False) thread.forum = forum thread.owner = profile thread.slug = slugify(thread.title) thread.save() thread.subscribers.add(profile) thread.save() body = _bb_code_escape(form.cleaned_data.get('post')) post = Post.objects.create(thread=thread, user=profile, updated_by=profile, number=1, deleted=False, body=body) post.save( ) # create and save the first post belonging to the new thread return HttpResponseRedirect(thread.get_absolute_url()) else: form = ThreadForm() return render(request, 'forums/add_thread.html', { 'form': form, 'forum': forum, 'create': True }, context_instance=RequestContext(request))