本文整理汇总了Python中models.Message.to_json方法的典型用法代码示例。如果您正苦于以下问题:Python Message.to_json方法的具体用法?Python Message.to_json怎么用?Python Message.to_json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Message
的用法示例。
在下文中一共展示了Message.to_json方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send_message
# 需要导入模块: from models import Message [as 别名]
# 或者: from models.Message import to_json [as 别名]
def send_message():
if request.method == 'POST':
print request.json
sender = db.session.query(User).filter_by(nickname=request.json['sender']).first()
receiver = db.session.query(User).filter_by(nickname=request.json['receiver']).first()
if sender and receiver:
message = Message(body=request.json['body'], sent_from_id=sender.id, sent_to_id=receiver.id)
db.session.add(message)
db.session.commit();
return json.dumps(message.to_json())
else:
return json.dumps(dict(feedback="message not sent"))
示例2: delete_user_from_chat
# 需要导入模块: from models import Message [as 别名]
# 或者: from models.Message import to_json [as 别名]
def delete_user_from_chat(request):
username = request.user
chat_id = request.POST['chat_id']
try:
chat = Chat.objects.get(id=chat_id)
chat.users.remove(username)
mess = Message(
chat_id = chat,
user_id = request.user,
message = str(request.user) + " left from chat",
post_time = timezone.now(),
_type = 2,
)
mess.save()
return HttpResponse(json.dumps(mess.to_json()))
except Exception, e:
return HttpResponse("Error", status = 400)
示例3: post_message
# 需要导入模块: from models import Message [as 别名]
# 或者: from models.Message import to_json [as 别名]
def post_message(request, chat_id):
if Chat.objects.filter(id = chat_id) is not None:
if request.user.is_authenticated():
message = request.POST['message']
user = request.user
post_time = timezone.now()
chat = Chat.objects.get(id = chat_id)
m = Message(user_id = user,
chat_id = chat,
message = message,
post_time = post_time,
_type = 0
)
print message
m.save()
print message
return HttpResponse(json.dumps(m.to_json()))
else:
return HttpResponse("Error", status = 400)
else:
return HttpResponse("Error", status = 400)