Ejemplo n.º 1
0
def create_user():
    data = request.get_json() or {}
    if data['name'] == '' or data['password'] == '':
        return bad_request('Name and password are required')
    if 'name' not in data or 'password' not in data:
        return bad_request('Must include username and password fields')
    if User.query.filter_by(name=data['name']).first():
        return bad_request('Please use a different username')
    user = User(public_id=str(uuid.uuid4()),)
    user.from_dict(data)
    db.session.add(user)
    db.session.commit()
    return good_request('Your registration was successful')
Ejemplo n.º 2
0
def create_user():
    data = request.get_json() or {}
    if "username" not in data or "email" not in data or "password" not in data:
        return bad_request("must include username, email and password fields")
    if User.query.filter_by(username=data["username"]).first():
        return bad_request("please use a different username")
    if User.query.filter_by(email=data["email"]).first():
        return bad_request("please use a different email address")
    user = User()
    user.from_dict(data, new_user=True)
    db.session.add(user)
    db.session.commit()
    response = jsonify(user.to_dict())
    response.status_code = 201
    response.headers["Location"] = url_for("api.get_user", id=user.id)
    return response