Exemplo n.º 1
0
def art_comment(request, art_pk):
    #book = models.Art.objects.filter(id=int(art_pk))   #方法1
    art = get_object_or_404(models.Art, pk=art_pk)  #方法2
    if request.method == "POST":
        form = CommentForm(data=request.POST)
        if form.is_valid():  #评论表单合法
            cmt = Comment(name=form.cleaned_data['name'],
                          title=form.cleaned_data['title'],
                          text=form.cleaned_data['text'],
                          art=art)
            cmt.save()
            comment_list = art.comment_set.all()  #通过外键反查所有评论
            comment_count = comment_list.count(),
            context = dict(
                art=art,
                form=form,
                comment_list=comment_list,
                comment_count=comment_count,
            )
            return render(request, "book_detail.html", context=context)
        else:
            comment_list = art.comment_set.all()  # 通过外键反查所有评论
            comment_count = comment_list.count(),
            context = dict(
                art=art,
                form=form,
                comment_list=comment_list,
                comment_count=comment_count,
            )
            flash(request, "error", "用户提交评论失败!")
            return render(request, "book_detail.html", context=context)

    return redirect(art)
Exemplo n.º 2
0
    def add_comment(kwargs):
        """
        新增评论
        :param kwargs:
        :return:
        """
        # 不允许包含http
        if 'http://' in kwargs['content']:
            source_ip = kwargs['source_ip']
            logger.info('*' * 30)
            logger.info('评论中包含http,来源:%s' % source_ip)
            logger.info(kwargs['content'])
            logger.info('*' * 30)
            return False, '非法内容,禁止评论。如有疑问请直接联系博主(页面顶部"关于"中有联系方式)'

        blog_id = kwargs['blog_id']
        recomment_email = kwargs['recomment_email']
        kwargs.pop('blog_id')
        kwargs.pop('recomment_email')
        comment_obj = Comment(**kwargs)
        comment_obj.save()

        if kwargs['creator'] == 'admin' or kwargs[
                'obj_type_id'] != 2:  # 管理员留言或者给管理员留言的情况才发送邮件通知
            # MsgService().send_email_by_process('来自loonapp的留言提醒',
            #                                    '你有一条新的留言,请登录查看: http://loonapp.com/blog/{}/'.format(blog_id),
            #                                    [kwargs['email']])

            MsgService().send_multi_email_by_process(
                '来自loonapp的留言提醒',
                '<p>你有一条新的留言,<a href="http://loonapp.com/blog/{}/">请点击查看</a>'.
                format(blog_id), [recomment_email])

        return True, comment_obj
Exemplo n.º 3
0
def community(request):

    comment_user = request.user
    comment_text = request.POST.get('comment_text', '')
    if comment_text.strip() == "":
        message = "评论不能为空"
        return error_response(message)

    content_type = request.POST.get('content_type', '')
    object_id = request.POST.get('object_id', '')
    model_class = ContentType.objects.get(model=content_type)

    comment = Comment()
    comment.object_id = object_id
    comment.comment_user = comment_user
    comment.comment_text = comment_text
    comment.content_type = model_class
    comment.save()

    # user = User.objects.get(id=object_id)
    # print(user.comment_set.filter(comment_user=user)[0].object_id)
    # user.save()

    comment_time = comment.comment_time.strftime('%Y-%m-%d %H:%M:%S')
    return success_response(comment_text, request.user, comment_time)
Exemplo n.º 4
0
def comment(request):
    comment_user = request.user
    comment_text = request.POST.get('comment_text', '')
    if comment_text.strip() == "":
        message = "评论不能为空"
        return error_response(message)

    content_type = request.POST.get('content_type', '')
    object_id = request.POST.get('object_id', '')
    # print(content_type)
    # print(object_id)
    # print(comment_text)
    model_class = ContentType.objects.get(model=content_type)

    comment = Comment()
    comment.object_id = object_id
    comment.comment_user = comment_user
    comment.comment_text = comment_text
    comment.content_type = model_class
    comment.save()

    item = Item.objects.get(id=object_id)
    item.comment = int(item.comment) + 1
    item.save()
    # referer = request.META.get('HTTP_REFERER',reverse('index'))
    # return redirect(referer)
    comment_time = comment.comment_time.strftime('%Y-%m-%d %H:%M:%S')
    return success_response(comment_text, request.user, comment_time)
Exemplo n.º 5
0
def comment(user, claim, text):
  if not can_comment(user, claim):
    raise PermissionDenied
  comment = Comment()
  comment.text = text
  comment.created_by = user
  comment.save()
  claim.comments.add(comment)
  return claim
Exemplo n.º 6
0
def comment(user, claim, text):
    if not can_comment(user, claim):
        raise PermissionDenied
    comment = Comment()
    comment.text = text
    comment.created_by = user
    comment.save()
    claim.comments.add(comment)
    return claim
Exemplo n.º 7
0
def comment(user, bounty, text):
  if not can_comment(user, bounty):
    raise PermissionDenied
  comment = Comment()
  comment.text = text
  comment.created_by = user
  comment.save()
  bounty.comments.add(comment)
  return bounty
Exemplo n.º 8
0
def comment_threads(request, comment_id):

    comment = None
    if not comment_id is None:
        try:
            comment = Comment.objects.get(id=int(comment_id))
            content_object = comment.content_object
            content_id = comment.content_object.id
        except:
            pass

    initial_setup = {
        "content_type": content_object.get_instance_content_type,
        "object_id": comment.object_id
    }
    # print(comment.is_parent)
    post = comment.content_object

    form = CommentForm(data=request.POST or None, initial=initial_setup)
    if form.is_valid():
        # print(request.POST)
        cd = form.cleaned_data
        ctype = cd.get('content_type')
        content_type = ContentType.objects.get(model=ctype)
        obj_id = cd.get('object_id')
        content = cd.get('content')
        parent_obj = None
        try:
            parent_id = int(request.POST.get('parent_id'))
        except:
            parent_id = None

        if parent_id:
            parent_qry = Comment.objects.filter(id=parent_id)
            if parent_qry.exists() and parent_qry.count() == 1:
                parent_obj = parent_qry.first()

        comment_obj = Comment(
            user=request.user,
            content=content,
            content_type=content_type,
            object_id=obj_id,
            parent=parent_obj,
        )
        comment_obj.save()
        create_action(request.user, 'replied', comment_obj)
        return redirect('comment:comment-threads',
                        comment_id=comment_id)  # redirect to parent comment

    context = {}
    context['comment'] = comment
    context['post'] = post
    context['form'] = form
    template = 'comment/comment_threads.html'
    return render(request, template, context)
Exemplo n.º 9
0
def post_details(request,post_author_username,post_slug):

	is_auth = functions.is_user_authenticated(request)# user js - to  toggle auth-social-modal 
	post = get_object_or_404(Post,
							author__username = post_author_username,
							slug = post_slug)

	# print(post.__class__)
	authors_post_count = Post.authors_blog_post_count(post)

	post_url = request.build_absolute_uri(post.get_absolute_url())
	facebook_share = "https://www.facebook.com/sharer/sharer.php?u={0}".format(post_url)
	google_plus_share   = "https://plus.google.com/share?url={0}".format(post_url)

	flag = Like.user_has_liked_post(post.id,request.user)
	post_likes_count = post.likes.count()

	context = dict()
	# comment form
	initial_setup = {
	"content_type":post.get_instance_content_type,
	"object_id": post.id
	}
	# print(post.get_instance_content_type)
	form = CommentForm(data = request.POST or None,initial = initial_setup)
	if form.is_valid():
		# print(request.POST)
		cd = form.cleaned_data
		ctype = cd.get('content_type')
		content_type = ContentType.objects.get(model = ctype)
		obj_id = cd.get('object_id')
		content = cd.get('content')
		parent_obj = None
		try:
			parent_id = int(request.POST.get('parent_id'))
		except:
			parent_id = None 

		if parent_id:
			parent_qry = Comment.objects.filter(id = parent_id)
			if parent_qry.exists() and parent_qry.count() == 1:
				parent_obj = parent_qry.first()

		comment 	= Comment(
					  user = request.user,
					  content  = content,
					  content_type = content_type,
					  object_id = obj_id,
					  parent = parent_obj,
					)
		comment.save()
		create_action(request.user,'commented',comment)
		return redirect(post.get_absolute_url())


	context['form'] = form

	comments = post.post_comments

	session_key = 'viewed_post_{}'.format(post.id)
	if not request.session.get(session_key,False):
		post.views += 1
		post.save()
		request.session[session_key] = True

	context['post'] = post
	context['author_posts_count'] = authors_post_count
	context['facebook_share'] = facebook_share
	context['google_plus_share'] = google_plus_share
	context['post_url'] = post_url
	context['is_likes'] = flag
	context['likes_count'] = post_likes_count
	context['is_auth'] = is_auth
	context['comments'] = comments
	
	template = 'blog/detail.html'
	return TemplateResponse(request,template,context)