def comment_block(target): return { 'target': target, 'comment_form': CommentForm(), 'comment_list': Comment.get_by_target(target), 'comment_count': Comment.get_by_target(target).count() }
def commentslist(request, id_post): try: post = Post.objects.get(id=id_post) except Post.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': comment_serializer = PostCommentsSerializer(post) return Response(comment_serializer.data) elif request.method == 'POST': if request.user.is_authenticated: comment_serializer = CommentsSerializer(Comment, data=request.data) if comment_serializer.is_valid(): comment = Comment(name=request.data['name'], email=request.data['email'], body=request.data['body'], postId=post) comment.save() return Response(request.data) return Response(comment_serializer.errors, status=status.HTTP_400_BAD_REQUEST) else: return Response({'User not authenticated'}, status=status.HTTP_401_UNAUTHORIZED)
def addComment(request,**kwargs): if request.method=='POST': userid=request.user.id if userid != None: #获取当前登录用户 c_Phone = User.objects.get(id=userid).username customer = Customer.objects.get(c_Phone=c_Phone) comment=Comment(c_Id=customer,g_Id=Goods.objects.get(id=request.POST['Goods_id']),c_Content=request.POST.get('comment')) comment.save() return HttpResponse('添加成功') return HttpResponse('添加失败')
def addcom(request,roomid): obj=request.session['name'] if obj is None : return HttpResponse('Login required') id1=User.objects.get(email=obj) r=Room.objects.get(id=roomid) com=request.GET['comment'] obj1=Comment(userid=id1,Desc=com,roomid=r) obj1.save() return HttpResponseRedirect("/Review/"+roomid)
def comment_block(target): """自定义评论标签""" # 在使用地方添加 {% comment_block request.path %} 使用。request.path为参数传递给comment_block() # return给block.html return { 'target': target, 'comment_form': CommentForm(), 'comment_list': Comment.get_by_target(target) }
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) comment_list = Comment.get_by_target(self.request.path) count = comment_list.count() context.update({ 'comment_form': CommentForm, 'comment_list': comment_list, }) print(context) return context
def comment_api(request): if request.POST.get('_method','') == 'put': blog_id = request.POST.get('blog_id','') comment_nickname = request.POST.get('comment_nickname','') comment_email = request.POST.get('comment_email','') comment_content = request.POST.get('comment_content','') comment = Comment( blog_id=blog_id, nickname=comment_nickname, mail=comment_email, content=comment_content) comment.save() comment_id = str(comment.id) resp = jsonresponse.creat_response(200) data = { 'url':'/blogs/blog/?blog_id={blog_id}'.format(blog_id=blog_id) } resp.data = data return resp.get_response()
def viewQuestionByID(request, questionID): '''A question whose Id is passed as the parameter is displayed with the question title, question text, comment list Parameter:request-->All the information of the form is stored in request questionID-->the ID of question to be displayed If the user is not authenticated then this returns an Http Response stating that the user is not logged in If the user is authenticated and the form filled is valid then it returns the page with the question and other details''' question = Question.objects.filter(questionID_f = questionID) if question: question = question[0] form = CommentForm() # if request.user.is_authenticated(): if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): if request.user.is_authenticated(): c = Comment() c.commentText = form['text'].data c.timeStamp = datetime.now() c.isReported = False c.userID = request.user c.questionID = question c.save() form = CommentForm() else: return HttpResponseRedirect("/login?next=%s" %questionID) question.commentList_f = list(Comment.objects.filter(questionID_f = question)) if request.user.is_authenticated(): var = HasSet.objects.filter(userID_f = request.user).filter(questionID_f = question) if var: ismyProblem = True else: ismyProblem = False else: ismyProblem = False return render(request, 'question.html',RequestContext(request, {'question' : question,'form' : form, 'comments' : question.commentList_f, 'ismyProblem' : ismyProblem} ))
def AddcommentView(request): if request.is_ajax(): data = request.POST # 评论内容 new_content = data.get('arciclecomment') # 评论对象文章 comment_post_ID = data.get('comment_post_ID') # 评论者 author = data.get('comment_author') # 评论者网址 #url = data.get('url', '') # 评论对象,父级对象,就是评论的是谁 comment_parent = data.get('comment_parent') # 获取用户信息 auser = User.objects.get(username=author) # 获取文章信息 the_article = Article.objects.get(id=comment_post_ID) if comment_parent == '0': new_comment = Comment(author=auser, content=new_content, belong=the_article, parent=None, rep_to=None) else: comment_repto = data.get('comment_rep') repto = Comment.objects.get(id=comment_repto) parent = Comment.objects.get(id=comment_parent) new_comment = Comment(author=auser, content=new_content, belong=the_article, parent=parent, rep_to=repto) new_comment.save() success = {} success = {"success": "success"} return HttpResponse(json.dumps(success))
def post(self, request, format=None): '''To post a comment on a resource Takes resource_id, comment_id(if replying a comment), content Returns resource with all comments''' try: resource = Resource.objects.get(id=request.data.get('resource_id')) comment = Comment() if request.data.get('comment_id'): try: comment.parent = Comment.objects.get(id=request.data.get('comment_id')) comment.commenter = request.user comment.comment_text = request.data.get('content') comment.save() except Exception as e: return Response({ "error": "Comment query doesn't exists." }, status=status.HTTP_400_BAD_REQUEST) else: comment.parent = None comment.commenter = request.user comment.comment_text = request.data.get('content') comment.save() resource.comments.add(comment) resource.save() allComments = resource.comments.all() serialized_comments = CommentSerializer(allComments, many=True).data serialized_resource = ResourceSerializer(resource, many=False).data serialized_resource['comments'] = serialized_comments return Response(serialized_resource) except Exception as e: return Response({ "error": "Resource query doesn't exists." }, status=status.HTTP_400_BAD_REQUEST)
def submit_comment(request, blog_id): """ 处理提交的评论 :param request: :return: """ blog = request.POST comment = Comment() # 评论 comment.name = blog.get("comment-name", 'Alien') comment.email = blog.get('comment-email') # comment.website = blog.get('website') comment.text = blog.get('comment') comment.blog = Blog.objects.get(id=blog_id) # 回复 comment.reply_to = blog.get('reply_to', 0) comment.reply_email = blog.get('reply_email', '*****@*****.**') comment.root_to = blog.get('root_to', 0) comment.reply_name = blog.get('reply_name', '外星人') comment.save() # async_send_mail.delay(comment.reply_email, comment.text, pk) return redirect(reverse('Blog:detail', kwargs={"blog_id": blog_id}))
def post(self, request, format=None): '''To post a comment in an announcement. Takes annoucement_id, parrent_comment_id and comment_text. returns the announcement object with a list of comments.''' announcement_id = request.data.get('announcement_id') try: announcement = Announcement.objects.get(id=announcement_id) if request.user.username == announcement.classroom.creator.username or request.user in announcement.classroom.moderators.all() or request.user in announcement.classroom.students.all(): comment = Comment() if request.data.get('comment_id'): try: comment.parent = Comment.objects.get(id=request.data.get('comment_id')) comment.commenter = request.user comment.comment_text = request.data.get('content') comment.save() except Exception as e: return Response({ "error": "Comment query doesn't exists." }, status=status.HTTP_400_BAD_REQUEST) else: comment.parent = None comment.commenter = request.user comment.comment_text = request.data.get('content') comment.save() announcement.comment.add(comment) announcement.save() allComments = announcement.comment.all() serialized_comments = CommentSerializer(allComments, many=True).data serialized_announcement = AnnouncementSerializer(announcement, many=False).data serialized_announcement['comments'] = serialized_comments return Response(serialized_announcement) else: return Response({ "error": "You aren't enrolled in this classroom." }, status=status.HTTP_400_BAD_REQUEST) except Exception as e: return Response({ "error": "Announcement query doesn't exists." }, status=status.HTTP_400_BAD_REQUEST)