Exemplo n.º 1
0
def submit_comment(request):
    comment_form = CommentFrom(request.POST, user=request.user)
    data = {}
    if comment_form.is_valid():
        comment = Comment()
        comment.text = comment_form.cleaned_data['text']
        comment.user = comment_form.cleaned_data['user']
        comment.content_object = comment_form.cleaned_data['content_obj']
        comment.object_id = comment_form.cleaned_data['object_id']

        parent = comment_form.cleaned_data['parent']
        if parent:
            comment.parent = parent
            comment.root = parent.root if parent.root else parent
            comment.reply_to = parent.user
        comment.save()

        data['status'] = 'SUCCESS'
        data['nickname'] = comment.user.nickname
        data['text'] = comment.text
        data['icon'] = comment.user.icon.url
        data['created_time'] = comment.created_time.strftime('%Y-%m-%d %H:%M:%S')
        data['reply_to'] = comment.reply_to.nickname if parent else ''

        data['id'] = comment.id
        data['root_id'] = comment.root.id if comment.root else None
    else:
        data['status'] = 'ERROR'
        data['message'] = '评论内容不能为空'

    return JsonResponse(data)
Exemplo n.º 2
0
def update_comment(request):
	referer = request.META.get('HTTP_REFERER', '/')  # 从哪个页面过来的,前一个页面的链接
	comment_form = CommentForm(request.POST, user=request.user)  # 额外给Form类传值
	data = {}
	if comment_form.is_valid():
		object_id = comment_form.cleaned_data.get('object_id')
		comment_text = comment_form.cleaned_data.get('comment_text')
		content_type = comment_form.cleaned_data.get('content_type')
		content_object = comment_form.cleaned_data.get('content_object')
		user = comment_form.cleaned_data.get('user')

		parent = comment_form.cleaned_data.get('parent')
		if not parent is None:
			root = parent.root if not parent.root is None else parent
			reply_to = parent.user  #  reply_to记录的是父对象
		else:
			root = None
			reply_to = None

		# conditions = {
		# 	"object_id": object_id,
		# 	"comment_content": comment_text,
		# 	"content_object": content_object,
		# 	"parent": parent,
		# 	"user": user,
		# 	"root": root,
		# 	"reply_to": reply_to,
		# }
		obj = Comment()
		obj.object_id = object_id
		obj.comment_content = comment_text
		obj.content_object = content_object
		obj.parent = parent
		obj.user = user
		obj.root = root
		obj.reply_to = reply_to
		obj.save()

		if parent is not None:
			data['reply_to'] = obj.reply_to.get_nickname_or_username()
		else:
			data['reply_to'] = ''
			
		data['status'] = 'success'
		data['pk'] = obj.id
		data['username'] = obj.user.get_nickname_or_username()
		data['created_time'] = obj.cteated_time.strftime("%Y-%m-%d %H:%M:%S")  # 格式化datime对象
		data['comment_content'] = obj.comment_content
		data['root_pk'] = obj.root_id if not obj.root is None else None
		data['content_type'] = ContentType.objects.get_for_model(obj).model
	else:
		data['status'] = 'failed'
		data['message'] = list(comment_form.errors.values())[0][0]
	return JsonResponse(data)
Exemplo n.º 3
0
def responses_to_comments():
	for pk, fields in respuestas.iteritems():

		c = Comment()
		c.comment = fields['content']
		c.published = True
		c.content_object = Dateo.objects.get(pk=fields['map_items'][0])
		c.object_id = fields['map_items'][0]
		c.user_id = get_user(fields['user'])
		c.client_domain = datea
		c.save()

		created = date_parser(fields['created'])
		Comment.objects.filter(pk=c.pk).update(created=created)
Exemplo n.º 4
0
def create_comments():

	Comment.objects.all().delete()

	for pk, fields in comentarios.iteritems():

		c = Comment(pk=pk)
		c.comment = fields['comment']
		c.published = fields['published']
		c.user_id = get_user(fields['user'])
		c.content_object = Dateo.objects.get(pk=fields['object_id'])
		c.object_id = fields['object_id']
		c.client_domain = datea
		c.save()

		created = date_parser(fields['created'])
		Comment.objects.filter(pk=c.pk).update(created=created)