コード例 #1
0
    def post(self):
        try:
            json_data = request.get_json()
        except Exception as e:
            raise MyBadRequestException
        check_user = False
        try:
            check_user = User.get_by_username(json_data["username"])
        except Exception as e:
            raise MyInternalServerErrorException
        if check_user:
            raise MyBadRequestException

        data, errors = user_schema.load(data=json_data)
        user = User(**data)
        if json_data["is_admin"] == 1:
            user.is_admin = True
        else:
            user.is_admin = False
        try:
            user.save()
        except Exception as e:
            raise MyInternalServerErrorException

        return user_schema.dump(user).data, HTTPStatus.CREATED
コード例 #2
0
def user(db):
    name = "bob"
    password = "******"

    new_user = User(name=name, password=password)
    new_user.save()

    return new_user
コード例 #3
0
def test_create_user(db):
    # given
    name = "bob"
    password = "******"

    user = User(name=name, password=password)

    # execute
    user.save()

    # expect
    user_result = User.find(name=name)

    assert user_result
    assert name == user_result.name
    assert password == user_result.password
コード例 #4
0
ファイル: setup_users.py プロジェクト: nathan5280/Snippets
def main(argv: List[str]) -> None:
    parser = argparse.ArgumentParser(description='Create and populate User DB')

    parser.add_argument('-c',
                        '--cfg-path',
                        required=True,
                        help="Path to the user configuration data to load.")

    parser.add_argument('-d',
                        '--db-path',
                        required=True,
                        help='Path to database file.')

    cfg = vars(parser.parse_args(argv))

    # Load the user configuration data.
    with open(cfg["cfg_path"], 'rt') as fp:
        users_cfg = json.load(fp=fp)

    # Delete the database file if it exists.
    if os.path.exists(cfg["db_path"]):
        os.remove(cfg["db_path"])

    # Create the Flask application as the container for the User object and database access.
    app = Flask(__name__)
    app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{cfg['db_path']}"
    app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

    # Initialize the database
    auth_db.init_app(app)
    app.app_context().push()
    auth_db.create_all()

    # Create the users in the DB
    for user_data in users_cfg["users"]:
        user = User(**user_data)
        user.save()