def post(self, request, post_id): comment = Comment() # 实例化类 comment.post_id = post_id comment.parent_id = request.POST['parent_id'] comment.reply_id = request.POST['reply_id'] comment.nick = request.POST['nick'] comment.mail = request.POST['mail'] comment.content = request.POST['content'] ua = parse_user_agent(request.META.get('HTTP_USER_AGENT', '')) # 解析HTTP_USER_AGENT comment.browser = ua['browser'] comment.client = ua['client'] # 处理回复评论 if request.POST['reply_id'] != '0': comment.comment_type = 'reply' reply_comment = Comment.objects.filter( id=request.POST['reply_id']).first() if reply_comment: comment.to_nick = reply_comment.nick comment.to_mail = reply_comment.mail # 如果是回复评论,则发送邮件通知相关评论人 recipient_list = EMAIL_RECEIVE_LIST + [reply_comment.mail] else: recipient_list = None else: # 如果是新的评论内容,则只需要发送通知博客作者 recipient_list = EMAIL_RECEIVE_LIST comment.save() # 保存评论数据到数据库 redirect_url = request.POST['redirect_url'] + '#comment-{0}'.format( comment.id) if recipient_list: # 发送邮件 try: send_email(url=redirect_url, recipient_list=recipient_list, post_id=post_id) except BaseException as e: print('发送邮件错误: {}'.format(e)) return redirect(redirect_url) # 重定向到指定页面
def commment(request): username = request.session.get('username', default=None) if username: user = User.objects.get(username=username) if request.method == "POST": if request.POST.get('comment_type'): text_form = comment_form(request.POST) comment = Comment() if text_form.is_valid(): comment.comment_text = text_form.cleaned_data[ 'comment_text'] parent_comment_id = request.POST['comment_id'] comment.comment_type = request.POST['comment_type'] comment.comment_img = request.FILES.get('comment_img') comment.comment_user = user parent_comment = Comment.objects.filter(pk=parent_comment_id) if parent_comment: comment.parent_comment = parent_comment[0] comment.root_comment = parent_comment[ 0].root_comment if not parent_comment[ 0].root_comment is None else parent_comment[0] comment.reply_user = parent_comment[0].comment_user else: comment.parent_comment = None comment.root_comment = None question_id = request.POST['question_id'] comment.comment_question = Question.objects.get( pk=question_id) comment.reply_user = Question.objects.get( pk=question_id).user user.integral += 3 comment.save() user.save() data = { 'comment_text': comment.comment_text, 'comment_date': comment.comment_time, 'comment_id': comment.pk } try: data['nickname'] = comment.comment_user.userinfo.all( )[0].nickname except: data['nickname'] = comment.comment_user.username return JsonResponse(data) else: comment_id = request.POST['comment_id'] question_id = request.POST['question'] comment = Comment.objects.get(pk=comment_id) question = Question.objects.get(pk=question_id) if question.user == user: admire_status = request.POST['admire'] admire_record = Admire_record() admire_record.admire_comment = comment admire_record.question = question admire_record.user = user admire_record.admire_user = comment.comment_user if admire_status == "yes": admire_record.is_admired = 1 user.integral += question.reward_integral // 3 + 5 data = {'admire_status': 'is_admired'} else: admire_record.is_admired = 0 data = {'admire_status': 'not_admired'} user.save() admire_record.save() return JsonResponse(data) else: user = User.objects.get(username=username) question_id = request.GET.get("question_id") question = Question.objects.get(pk=question_id) comments = Comment.objects.filter(comment_question=question, parent_comment=None) all_comments = Comment.objects.filter(comment_question=question) history_record = History_record() history_record.user = User.objects.get(username=username) history_record.viewed_question = question history_record.save() admire_record = Admire_record.objects.filter(user=user, question=question) context = {} context['all_comments'] = all_comments context['admire_records'] = admire_record context['comments'] = comments.order_by('comment_time') context['question'] = question context['comment_form'] = comment_form() if question.user == user: context['is_user'] = '******' try: context['your_headimg'] = Userinfo.objects.get( user=user).headimg.url except: context['your_headimg'] = '' return render(request, 'question_detail.html', context)