コード例 #1
0
def create_post(id):
    data = request.get_json() or {}
    if 'image' not in data:
        return make_response(400, 'must include image field')
    data['user_id'] = id
    post = Post()
    post.from_dict(data)
    db.session.add(post)
    db.session.commit()
    response = jsonify(post.to_dict())
    response.status_code = 201
    return response
コード例 #2
0
def create_post():
    title = request.json.get('title', None)
    text = request.json.get('text', None)
    media = request.json.get('media', None)

    new_post = Post(title=title, text=text, media=media)
    if new_post:
        new_post.create()
        return jsonify(new_post.to_dict()), 201

    else:
        return {'error': 'Something went wrong'}, 409
コード例 #3
0
ファイル: mutations.py プロジェクト: Shadid12/flask-graphql
def create_post_resolver(obj, info, title, description):
    try:
        today = date.today()
        post = Post(title=title,
                    description=description,
                    created_at=today.strftime("%b-%d-%Y"))
        db.session.add(post)
        db.session.commit()
        payload = {"success": True, "post": post.to_dict()}
    except ValueError:  # date format errors
        payload = {
            "success":
            False,
            "errors": [
                f"Incorrect date format provided. Date should be in "
                f"the format dd-mm-yyyy"
            ]
        }

    return payload