示例#1
0
def create_message():
    """给其它用户发送私信"""
    data = request.get_json()
    if not data:
        return bad_request('You must post JSON data.')
    if 'body' not in data or not data.get('body'):
        return bad_request('Body is required.')
    if 'recipient_id' not in data or not data.get('recipient_id'):
        return bad_request('Recipient id is required.')

    user = User.query.get_or_404(int(data.get('recipient_id')))
    if g.current_user == user:
        return bad_request('You cannot send private message to yourself.')
    if user.is_blocking(g.current_user):
        return bad_request('You are in the blacklist of {}'.format(
            user.username))
    message = Message()
    message.from_dict(data)
    message.sender = g.current_user
    message.recipient = user
    db.session.add(message)
    # 给私信接收者发送新私信通知
    user.add_notification('unread_messages_count', user.new_recived_messages())
    db.session.commit()
    response = jsonify(message.to_dict())
    response.status_code = 201
    # HTTP协议要求201响应包含一个值为新资源URL的Location头部
    response.headers['Location'] = url_for('api.get_message', id=message.id)
    return response
def create_message():
    """发送一条私信"""
    json_data = request.json

    if not json_data:
        return bad_request('You must post JSON data')
    if 'body' not in json_data and not json_data.get('body'):
        return bad_request('body is required')
    if 'recipient_id' not in json_data and not json_data.get('recipient_id'):
        return bad_request('recipient_id is required')

    user = User.query.get_or_404(json_data['recipient_id'])
    if g.current_user == user:
        return bad_request('You cannot send private message to yourself.')
    if user.is_blocking(g.current_user):
        return bad_request('You are in the blacklist of {}'.format(user.name if user.name else user.username))

    message = Message()
    message.from_dict(json_data)
    message.sender = g.current_user
    message.recipient = user
    db.session.add(message)
    db.session.commit()

    user.add_notification('unread_messages_count',user.new_recived_messages())

    response = jsonify(message.to_dict())
    response.status_code = 201
    response.headers['Location'] = url_for('api.get_message', id=message.id)

    return response
示例#3
0
def create_message(): 
    data = request.get_json()
    body = data.get('body',None)
    recipient_id = data.get('recipient_id',None)
    err = {}
    if not body:
        err['body'] = "No body in json or body is null."
    if not recipient_id:
        err['recipient_id'] = "No recipient_id in json or recipient_id is null."
    if err:
        return bad_request(err)
    
    tar_user = User.query.get_or_404(recipient_id)
    if tar_user == g.current_user:
        return bad_request('can not send to yourself.')
    message = Message()
    message.from_dict(data)
    # Message里面有外键sernder_id、recipient_id,在哪里给这些外键赋值的? -
    # 答:
    message.sender = g.current_user
    message.recipient = tar_user
    db.session.add(message)
    db.session.commit()
    response = message.to_dict()
    return jsonify(response)
示例#4
0
def create_message():
    '''
    Create new Message
    '''
    data = request.get_json or {}
    if 'message_id' not in data:
        return bad_request('must include message_id')
    if Message.query.filter_by(message_id=data['message_id']).first():
        return bad_request('message_id already exists')
    message = Message()
    message.from_dict(data)
    db.session.add(message)
    db.session.commit()
    response = jsonify(message.to_dict())
    response.status_code = 201
    response.headers['Location'] = url_for(
        'api.get_message', message_id=message.message_id)
    return response