def thread(request, board_id, thread_id): profile = get_object_or_404(UserProfile, user=request.user) thread = get_object_or_404(Thread, pk=thread_id) post_form = PostForm() posts = thread.post_set.all().order_by('date_posted') paginator = Paginator(posts, 20) page = request.GET.get('page') try: posts = paginator.page(page) except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: posts = paginator.page(paginator.num_pages) #user submits a post if request.method == 'POST': if 'addpost' in request.POST: post_form = PostForm(request.POST) if post_form.is_valid(): post = Post(user=profile, thread=thread, message_markdown=request.POST['message_markdown']) post.save() return HttpResponseRedirect(REDIRECT_THREAD % (board_id, thread_id)) elif 'gotopage' in request.POST: page_number = request.POST['pagenumber'] return HttpResponseRedirect(REDIRECT_THREAD_PAGE % (board_id, thread_id, page_number)) context = { 'thread': thread, 'posts': posts, 'post_form': post_form, 'profile': profile, } return render(request, 'bulletinboard/threadIndex.html', context)
def board(request, board_id): profile = get_object_or_404(UserProfile, user=request.user) board = get_object_or_404(Board, pk=board_id) thread_form = ThreadForm() post_form = PostForm() if request.method == 'POST': #user clicks the 'Add Thread' button if 'addthreadpost' in request.POST: thread_form = ThreadForm(request.POST) post_form = PostForm(request.POST) if thread_form.is_valid() and post_form.is_valid(): thread = Thread(name=request.POST['name'], user=profile, board=board) thread.save() post = Post(user=profile, thread=thread, message_markdown=request.POST['message_markdown']) post.save() return HttpResponseRedirect(REDIRECT_BOARD % board_id) elif 'gotopage' in request.POST: page_number = request.POST['pagenumber'] return HttpResponseRedirect(REDIRECT_BOARD_PAGE % (board_id, page_number)) #sort threads according to their most recent post threads = Thread.objects.filter(board=board).annotate(latest_post=Max('post__date_posted')).order_by('thread_type', 'latest_post') posts = [] for thread in threads: p = Post.objects.get(Q(thread=thread), Q(date_posted=thread.latest_post)) posts.append(p) paginator = Paginator(threads, 20) page = request.GET.get('page') try: threads = paginator.page(page) except PageNotAnInteger: threads = paginator.page(1) except EmptyPage: threads = paginator.page(paginator.num_pages) context = { 'board': board, 'thread_form': thread_form, 'post_form': post_form, 'threads': threads, 'posts': posts, 'profile': profile, } return render(request, 'bulletinboard/boardIndex.html', context)
def submit_post(request, board_id, thread_id, post_id): post = get_object_or_404(Post, pk=post_id) profile = get_object_or_404(UserProfile, user=request.user) thread = get_object_or_404(Thread, pk=thread_id) if post.user.user != request.user or profile.is_banned is True or thread.is_locked is True: return HttpResponse('<h2>Permission Denied.</h2>') if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): post.date_posted = datetime.datetime.now() post.message_markdown = request.POST['message_markdown'] post.save() return HttpResponseRedirect(REDIRECT_THREAD % (board_id, thread_id)) context = { 'form': form, 'post': post, } return render(request, 'bulletinboard/editPost.html', context)