def add_reply(request, pk): post = get_object_or_404(Post, pk=pk) if request.method == 'POST': reply_form = ReplyForm(request.POST) if reply_form.is_valid(): reply = reply_form.save(commit=False) reply.user = request.user reply.post = post reply.save() post.increase_reply() post.save() return redirect(post) else: comment_form = CommentForm() reply_list = post.reply_set.all().order_by('created_time') comment_list = [] for reply in reply_list: cur_list = reply.comment_set.all().order_by('created_time') comment_list.append(cur_list) context = { 'post': post, 'comment_form': comment_form, 'reply_form': reply_form, 'reply_list': reply_list, 'comment_list': comment_list, } return render(request, 'bbs/detail.html', context=context) return redirect(post)
def reply(request, topic_id): try: topic = Topic.objects.get(id=topic_id) comment_list = Comment.objects.filter(topic=topic).order_by('created_on') except Topic.DoesNotExist: raise Http404 if request.method == 'POST': form = ReplyForm(request.POST) if form.is_valid(): comment = form.save(commit=False) request.user.comment_num += 1 request.user.calculate_au() request.user.save() comment.author = request.user try: topic = Topic.objects.get(id=topic_id) except Topic.DoesNotExist: raise Http404 comment.topic = topic comment.save() # --- 解析@ --- #team_name_pattern = re.compile('(?<=@)(\w+)', re.UNICODE) team_name_pattern = re.compile('(?<=@)([0-9a-zA-Z.]+)', re.UNICODE) at_name_list = set(re.findall(team_name_pattern, comment.content)) if at_name_list: for at_name in at_name_list: if at_name != comment.author.username and at_name != comment.topic.author.username: try: at_user = User.objects.get(username=at_name) if at_user: notice = Notice(from_user=comment.author,to_user=at_user ,topic=comment.topic,content=comment.content) notice.save() except: pass # --- 解析@ --- topic.num_comments += 1 topic.updated_on = timezone.now() topic.last_reply = request.user topic.save() return HttpResponseRedirect(reverse("bbs:topic" ,args=(topic_id,))) else: form = ReplyForm() return render(request,"bbs/topic.html",{"node":node,"topic":topic,"form":form,"comment_list":comment_list})
def reply(request, topic_id): try: topic = Topic.objects.get(id=topic_id) # comment --- comment_list = Comment.objects.filter( topic=topic).order_by('created_on') paginator = Paginator(comment_list, NUM_COMMENT_PER_PAGE) page = request.GET.get('page') if page == None: page = paginator.num_pages try: comment_list = paginator.page(page) except PageNotAnInteger: comment_list = paginator.page(1) except EmptyPage: comment_list = paginator.page(paginator.num_pages) # comment --- except Topic.DoesNotExist: raise Http404 if request.method == 'POST': form = ReplyForm(request.POST) if form.is_valid(): last_comment = Comment.objects.filter( author=request.user).order_by('-created_on')[:1] last_comment = last_comment.first() if last_comment and last_comment.content == form.clean( )['content'] and ( (timezone.now() - last_comment.created_on).seconds < 10): messages.error(request, '你是否正在尝试连续提交两次相同的回复?') else: comment = form.save(commit=False) request.user.comment_num += 1 request.user.calculate_au() request.user.save() comment.author = request.user try: topic = Topic.objects.get(id=topic_id) except Topic.DoesNotExist: raise Http404 comment.topic = topic comment.save() # --- 解析@ --- #team_name_pattern = re.compile('(?<=@)(\w+)', re.UNICODE) team_name_pattern = re.compile('(?<=@)([0-9a-zA-Z_.]+)', re.UNICODE) at_name_list = set( re.findall(team_name_pattern, comment.content)) if at_name_list: for at_name in at_name_list: if at_name != comment.author.username and at_name != comment.topic.author.username: try: at_user = User.objects.get(username=at_name) if at_user: notice = Notice(from_user=comment.author, to_user=at_user, topic=comment.topic, content=comment.content) notice.save() except: pass # --- 解析@ --- topic.num_comments += 1 topic.updated_on = timezone.now() topic.last_reply = request.user topic.save() return HttpResponseRedirect( reverse("bbs:topic", args=(topic_id, ))) else: form = ReplyForm() return render( request, "bbs/topic.html", { "node": node, "topic": topic, "form": form, "comment_list": comment_list, 'paginator': paginator })
def reply(request, topic_id): try: topic = Topic.objects.get(id=topic_id) # comment --- comment_list = Comment.objects.filter(topic=topic).order_by('created_on') paginator = Paginator(comment_list, NUM_COMMENT_PER_PAGE) page = request.GET.get('page') if page == None: page = paginator.num_pages try: comment_list = paginator.page(page) except PageNotAnInteger: comment_list = paginator.page(1) except EmptyPage: comment_list = paginator.page(paginator.num_pages) # comment --- except Topic.DoesNotExist: raise Http404 if request.method == 'POST': form = ReplyForm(request.POST) if form.is_valid(): last_comment = Comment.objects.filter(author=request.user).order_by('-created_on')[:1] last_comment = last_comment.first() if last_comment and last_comment.content == form.clean()['content'] and ((timezone.now()-last_comment.created_on).seconds < 10): messages.error(request,'你是否正在尝试连续提交两次相同的回复?'); else: comment = form.save(commit=False) request.user.comment_num += 1 request.user.calculate_au() request.user.save() comment.author = request.user try: topic = Topic.objects.get(id=topic_id) except Topic.DoesNotExist: raise Http404 comment.topic = topic comment.save() # --- 解析@ --- #team_name_pattern = re.compile('(?<=@)(\w+)', re.UNICODE) team_name_pattern = re.compile('(?<=@)([0-9a-zA-Z_.]+)', re.UNICODE) at_name_list = set(re.findall(team_name_pattern, comment.content)) if at_name_list: for at_name in at_name_list: if at_name != comment.author.username and at_name != comment.topic.author.username: try: at_user = User.objects.get(username=at_name) if at_user: notice = Notice(from_user=comment.author,to_user=at_user ,topic=comment.topic,content=comment.content) notice.save() except: pass # --- 解析@ --- topic.num_comments += 1 topic.updated_on = timezone.now() topic.last_reply = request.user topic.save() return HttpResponseRedirect(reverse("bbs:topic" ,args=(topic_id,))) else: form = ReplyForm() return render(request,"bbs/topic.html",{"node":node,"topic":topic,"form":form,"comment_list":comment_list,'paginator':paginator})