def handle_limiter(sketch: Sketch): sketch.add_requirements("flask-limiter") sketch.settings["default"]["RATELIMIT_DEFAULT"] = "200 per day;50 per hour" sketch.settings["default"]["RATELIMIT_ENABLED"] = True sketch.settings["development"]["RATELIMIT_ENABLED"] = False sketch.add_extensions("limiter") sketch.write_template( "ext_limiter_tpl", templates.ext, pjoin(sketch.app_folder, "ext", "limiter.py"), ) sketch.write_template( "examples_limiter_tpl", templates.examples, pjoin( sketch.app_folder, "examples", "limiter_examples.py", ), ) sketch.write_template( "examples_init_limiter_tpl", templates.examples, pjoin( sketch.app_folder, "examples", "__init__.py", ), )
def handle_debugtoolbar(sketch: Sketch): sketch.add_requirements(reqs.FLASK_DEBUGTOOLBAR, dev=True) sketch.add_extensions("debugtoolbar", dev=True) sketch.settings["development"]["DEBUG_TB_INTERCEPT_REDIRECTS"] = False if sketch.database == "mongodb": sketch.settings["development"]["DEBUG_TB_PANELS"] = [ "flask_debugtoolbar.panels.versions.VersionDebugPanel", "flask_debugtoolbar.panels.timer.TimerDebugPanel", "flask_debugtoolbar.panels.headers.HeaderDebugPanel", "flask_debugtoolbar.panels.request_vars.RequestVarsDebugPanel", "flask_debugtoolbar.panels.template.TemplateDebugPanel", "flask_debugtoolbar.panels.route_list.RouteListDebugPanel", "flask_debugtoolbar.panels.logger.LoggingPanel", "flask_debugtoolbar.panels.profiler.ProfilerDebugPanel", "flask_debugtoolbar.panels.config_vars.ConfigVarsDebugPanel", ] if sketch.config_framework != "dynaconf": sketch.write_template( "ext_debugtoolbar_tpl", templates.ext, pjoin(sketch.app_folder, "ext", "debugtoolbar.py",), )
def handle_sql_db(sketch: Sketch): sketch.add_requirements(reqs.FLASK_SQLALCHEMY) sketch.settings["default"]["SQLALCHEMY_TRACK_MODIFICATIONS"] = False sketch.settings["development"]["SQLALCHEMY_TRACK_MODIFICATIONS"] = True sketch.secrets["default"]["SQLALCHEMY_DATABASE_URI"] = "sqlite:///db.sqlite3" sketch.add_extensions("database") sketch.write_template( "ext_sqlalchemy_tpl", templates.ext, pjoin(sketch.app_folder, "ext", "database.py"), ) sketch.write_template( "utils_sql_tpl", templates.models, pjoin(sketch.app_folder, "models", "utils.py"), ) sketch.template_args["ADMIN_MODEL_ENGINE"] = "sqla" sketch.template_args[ "ADMIN_DATABASE_IMPORT" ] = "from {}.ext.database import db".format(sketch.app_folder_name) sketch.template_args[ "ADMIN_VIEW_USER" ] = "admin.add_view(ProtectedModelView(User, db.session))" sketch.template_args[ "ADMIN_VIEW_ROLE" ] = "admin.add_view(ProtectedModelView(Role, db.session))"
def handle_caching(sketch: Sketch): sketch.add_requirements("flask-caching") sketch.settings["development"]["CACHE_TYPE"] = "simple" sketch.settings["testing"]["CACHE_TYPE"] = "simple" sketch.settings["production"]["CACHE_TYPE"] = "simple" sketch.add_extensions("caching") sketch.write_template( "ext_caching_tpl", templates.ext, pjoin(sketch.app_folder, "ext", "caching.py"), ) sketch.write_template( "examples_caching_tpl", templates.examples, pjoin( sketch.app_folder, "examples", "caching_examples.py", ), ) sketch.write_template( "examples_init_caching_tpl", templates.examples, pjoin( sketch.app_folder, "examples", "__init__.py", ), )
def mongodb_handler(sketch: Sketch): if sketch.database == "mongodb": sketch.add_requirements(reqs.FLASK_MONGOENGINE) sketch.add_extensions("database") sketch.settings["default"][ "MONGODB_HOST" ] = "mongodb://*****:*****@127.0.0.1:27017/database?authSource=admin" sketch.write_template( "ext_mongoengine_tpl", templates.ext, pjoin(sketch.app_folder, "ext", "database.py"), ) sketch.write_template( "utils_mongo_tpl", templates.models, pjoin(sketch.app_folder, "models", "utils.py"), ) sketch.template_args["ADMIN_MODEL_ENGINE"] = "mongoengine" sketch.template_args["ADMIN_DATABASE_IMPORT"] = "" sketch.template_args[ "ADMIN_VIEW_USER" ] = "admin.add_view(ProtectedModelView(User))" sketch.template_args[ "ADMIN_VIEW_ROLE" ] = "admin.add_view(ProtectedModelView(Role))" return True
def jwt_extended_handler(sketch: Sketch): if sketch.api_auth_framework == "jwt_extended": sketch.add_requirements(reqs.FLASK_JWT_EXTENDED, reqs.ARGON2) sketch.settings["default"]["JWT_ACCESS_TOKEN_EXPIRES"] = 3600 sketch.settings["default"]["JWT_REFRESH_TOKEN_EXPIRES"] = 2592000 sketch.add_extensions("api_auth") sketch.write_template( "ext_api_auth_jwt_extended_tpl", templates.ext, pjoin(sketch.app_folder, "ext", "api_auth.py"), ) sketch.write_template( "utils_security_jwt_extended_rbac_tpl", templates.utils.security, pjoin(sketch.app_folder, "utils", "security", "api_rbac.py"), ) if sketch.auth_framework != "security": sketch.write_template( "utils_security_password_hasher_tpl", templates.utils.security, pjoin( sketch.app_folder, "utils", "security", "password_hasher.py", ), mode="w", ) return True
def handle_cors(sketch: Sketch): sketch.add_requirements(reqs.FLASK_CORS) sketch.add_extensions("cors") sketch.write_template( "ext_cors_tpl", templates.ext, pjoin(sketch.app_folder, "ext", "cors.py",), )
def handle_migrate(sketch: Sketch): sketch.add_requirements("flask-migrate") sketch.add_extensions("migrate") sketch.write_template( "ext_migrate_tpl", templates.ext, pjoin(sketch.app_folder, "ext", "migrate.py"), )
def handle_admin(sketch: Sketch): sketch.add_requirements(reqs.FLASK_ADMIN) sketch.add_extensions("admin") sketch.settings["default"]["ADMIN_TEMPLATE_MODE"] = "bootstrap3" sketch.settings["development"]["ADMIN_NAME"] = "{} (Dev)".format( snake_to_camel(sketch.project_name) ) sketch.settings["testing"]["ADMIN_NAME"] = "{} (Testing)".format( snake_to_camel(sketch.project_name) ) sketch.settings["production"]["ADMIN_NAME"] = snake_to_camel( sketch.project_name ) # TODO refact this part to not use a lot of if statements if sketch.auth_framework == "security": sketch.write_template( "ext_admin_security_tpl", templates.ext.admin, pjoin(sketch.app_folder, "ext", "admin", "__init__.py",), ) if sketch.auth_framework == "login": sketch.write_template( "ext_admin_login_tpl", templates.ext.admin, pjoin(sketch.app_folder, "ext", "admin", "__init__.py",), ) if sketch.auth_framework == "none": sketch.add_requirements(reqs.FLASK_BASICAUTH) sketch.add_extensions("admin.basic_auth") sketch.secrets["default"]["BASIC_AUTH_USERNAME"] = "******" sketch.secrets["default"]["BASIC_AUTH_PASSWORD"] = str(uuid4()) sketch.write_template( "ext_basicauth_tpl", templates.ext.admin, pjoin(sketch.app_folder, "ext", "admin", "basic_auth.py",), ) sketch.write_template( "ext_admin_basicauth_tpl", templates.ext.admin, pjoin(sketch.app_folder, "ext", "admin", "__init__.py",), )
def handle_debugtoolbar(sketch: Sketch): sketch.add_requirements("flask-debugtoolbar", dev=True) sketch.add_extensions("debugtoolbar", dev=True) sketch.settings["development"]["DEBUG_TB_INTERCEPT_REDIRECTS"] = False if sketch.config_framework != "dynaconf": sketch.write_template( "ext_debugtoolbar_tpl", templates.ext, pjoin( sketch.app_folder, "ext", "debugtoolbar.py", ), )
def jwt_extended_handler(sketch: Sketch): if sketch.api_auth_framework == "jwt_extended": sketch.add_requirements("flask-jwt-extended", "argon2-cffi") sketch.settings["default"]["JWT_ACCESS_TOKEN_EXPIRES"] = 3600 sketch.settings["default"]["JWT_REFRESH_TOKEN_EXPIRES"] = 2592000 sketch.add_extensions("api_auth") sketch.write_template( "commands_default_tpl", templates.commands, pjoin( sketch.app_folder, "commands", "__init__.py", ), mode="w", ) sketch.write_template( "ext_api_auth_jwt_extended_tpl", templates.ext, pjoin(sketch.app_folder, "ext", "api_auth.py"), ) sketch.write_template( "utils_security_jwt_extended_rbac_tpl", templates.utils.security, pjoin(sketch.app_folder, "utils", "security", "api_rbac.py"), ) if sketch.auth_framework != "security": sketch.write_template( "utils_security_password_hasher_tpl", templates.utils.security, pjoin( sketch.app_folder, "utils", "security", "password_hasher.py", ), mode="w", ) return True
def handle_sql_db(sketch: Sketch): sketch.add_requirements("flask-sqlalchemy") sketch.settings["default"]["SQLALCHEMY_TRACK_MODIFICATIONS"] = False sketch.settings["development"]["SQLALCHEMY_TRACK_MODIFICATIONS"] = True sketch.settings["default"][ "SQLALCHEMY_DATABASE_URI" ] = "sqlite:///db.sqlite3" sketch.add_extensions("database") sketch.write_template( "ext_sqlalchemy_tpl", templates.ext, pjoin(sketch.app_folder, "ext", "database.py"), ) sketch.write_template( "models_utils_tpl", templates.models, pjoin(sketch.app_folder, "models", "utils.py"), )
def smorest_handler(sketch: Sketch): if sketch.api_framework == "smorest": sketch.add_requirements(reqs.FLASK_SMOREST) sketch.settings["default"]["API_TITLE"] = sketch.project_name sketch.settings["default"]["API_VERSION"] = "v1" sketch.settings["default"]["OPENAPI_VERSION"] = "3.0.2" sketch.settings["default"]["OPENAPI_JSON_PATH"] = "api-spec.json" sketch.settings["default"]["OPENAPI_URL_PREFIX"] = "/openapi" sketch.settings["default"]["OPENAPI_REDOC_PATH"] = "/redoc" sketch.settings["default"][ "OPENAPI_REDOC_URL" ] = "https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js" # noqa sketch.settings["default"]["OPENAPI_SWAGGER_UI_PATH"] = "/swagger-ui" sketch.settings["default"][ "OPENAPI_SWAGGER_UI_URL" ] = "https://cdn.jsdelivr.net/npm/swagger-ui-dist/" sketch.add_extensions("api") os.makedirs(pjoin(sketch.app_folder, "api", "resources", "examples")) open( pjoin( sketch.app_folder, "api", "resources", "examples", "__init__.py", ), "a", ).close() if sketch.api_auth_framework == "jwt_extended": sketch.write_template( "api_init_jwt_extended_tpl", templates.api, pjoin(sketch.app_folder, "api", "__init__.py"), ) sketch.write_template( "ext_api_smorest_tpl", templates.ext, pjoin(sketch.app_folder, "ext", "api.py"), ) if sketch.api_auth_framework == "none": resource_tpl = "api_example_smorest_pet_tpl" else: resource_tpl = "api_example_smorest_pet_auth_tpl" sketch.write_template( resource_tpl, templates.api.resources.examples, pjoin(sketch.app_folder, "api", "resources", "examples", "pet.py"), ) if sketch.database == "mongodb": example_tpl_model = "pet_mongo_tpl" else: example_tpl_model = "pet_sql_tpl" sketch.write_template( example_tpl_model, templates.models.examples, pjoin(sketch.app_folder, "models", "examples", "pet.py"), ) return True
def security_web_handler(sketch: Sketch): if sketch.auth_framework == "security": sketch.add_requirements(reqs.FLASK_SECURITY_TOO, reqs.ARGON2) sketch.settings["default"]["SECURITY_REGISTERABLE"] = True sketch.settings["default"]["SECURITY_POST_LOGIN_VIEW"] = "/" sketch.settings["default"]["SECURITY_PASSWORD_HASH"] = "argon2" sketch.add_extensions("auth") sketch.secrets["default"]["SECURITY_PASSWORD_SALT"] = random_string( length=32) sketch.template_args[ "PWD_VERIFIER_METHOD_IMPORT"] = "from flask_security import verify_password" sketch.template_args[ "PWD_VERIFIER_METHOD"] = "verify_password(password, user.password)" sketch.write_template( "site_web_only_init_tpl", templates.site, pjoin(sketch.app_folder, "site", "__init__.py"), ) sketch.write_template( "site_web_only_views_tpl", templates.site, pjoin(sketch.app_folder, "site", "views.py"), ) if sketch.database == "mongodb": auth_tpl = "ext_auth_security_mongo_tpl" else: auth_tpl = "ext_security_web_only_tpl" sketch.write_template( auth_tpl, templates.ext, pjoin(sketch.app_folder, "ext", "auth.py"), ) sketch.write_template( "examples_security_auth_tpl", templates.examples, pjoin( sketch.app_folder, "examples", "auth_examples.py", ), ) sketch.write_template( "examples_init_auth_tpl", templates.examples, pjoin( sketch.app_folder, "examples", "__init__.py", ), ) return True
def login_handler(sketch: Sketch): if sketch.auth_framework == "login": sketch.add_requirements(reqs.FLASK_LOGIN, reqs.ARGON2, reqs.FLASK_WTF, reqs.EMAIL_VALIDATOR) sketch.add_extensions("auth") sketch.write_template( "ext_login_tpl", templates.ext, pjoin(sketch.app_folder, "ext", "auth.py"), ) sketch.write_template( "examples_login_auth_tpl", templates.examples, pjoin( sketch.app_folder, "examples", "auth_examples.py", ), ) sketch.write_template( "examples_init_auth_tpl", templates.examples, pjoin( sketch.app_folder, "examples", "__init__.py", ), ) sketch.write_template( "utils_security_login_rbac_tpl", templates.utils.security, pjoin(sketch.app_folder, "utils", "security", "rbac.py"), ) sketch.write_template( "utils_security_password_hasher_tpl", templates.utils.security, pjoin(sketch.app_folder, "utils", "security", "password_hasher.py"), mode="w", ) sketch.write_template( "site_login_auth_views_tpl", templates.site, pjoin( sketch.app_folder, "site", "views.py", ), ) sketch.write_template( "site_login_auth_forms_tpl", templates.site, pjoin( sketch.app_folder, "site", "forms.py", ), ) sketch.write_template( "site_login_init_tpl", templates.site, pjoin( sketch.app_folder, "site", "__init__.py", ), ) os.makedirs(pjoin(sketch.app_folder, "site", "templates")) sketch.write_template( "login_html_tpl", templates.site.templates, pjoin( sketch.app_folder, "site", "templates", "login.html", ), ) sketch.write_template( "register_html_tpl", templates.site.templates, pjoin( sketch.app_folder, "site", "templates", "register.html", ), ) sketch.write_template( "dashboard_html_tpl", templates.site.templates, pjoin( sketch.app_folder, "site", "templates", "dashboard.html", ), ) return True
def security_web_handler(sketch: Sketch): if sketch.auth_framework == "security": sketch.add_requirements("flask-security-too", "argon2-cffi") sketch.settings["default"]["SECURITY_REGISTERABLE"] = True sketch.settings["default"]["SECURITY_POST_LOGIN_VIEW"] = "/" sketch.settings["default"]["SECURITY_PASSWORD_HASH"] = "argon2" sketch.add_extensions("auth") sketch.secrets["default"]["SECURITY_PASSWORD_SALT"] = random_string( length=32) sketch.template_args[ "PWD_VERIFIER_METHOD_IMPORT"] = "from flask_security import verify_password" sketch.template_args[ "PWD_VERIFIER_METHOD"] = "verify_password(password, user.password)" sketch.write_template( "site_web_only_init_tpl", templates.site, pjoin(sketch.app_folder, "site", "__init__.py"), ) sketch.write_template( "site_web_only_views_tpl", templates.site, pjoin(sketch.app_folder, "site", "views.py"), ) sketch.write_template( "security_web_only_tpl", templates.commands, pjoin( sketch.app_folder, "commands", "__init__.py", ), mode="w", ) sketch.write_template( "ext_security_web_only_tpl", templates.ext, pjoin(sketch.app_folder, "ext", "auth.py"), ) sketch.write_template( "models_security_tpl", templates.models, pjoin(sketch.app_folder, "models", "user.py"), ) sketch.write_template( "examples_security_auth_tpl", templates.examples, pjoin( sketch.app_folder, "examples", "auth_examples.py", ), ) sketch.write_template( "examples_init_auth_tpl", templates.examples, pjoin( sketch.app_folder, "examples", "__init__.py", ), ) return True
def login_handler(sketch: Sketch): if sketch.auth_framework == "login": sketch.add_requirements("flask-login", "argon2-cffi", "flask-wtf", "email_validator") sketch.add_extensions("auth") sketch.write_template( "commands_default_tpl", templates.commands, pjoin( sketch.app_folder, "commands", "__init__.py", ), ) sketch.write_template( "ext_login_tpl", templates.ext, pjoin(sketch.app_folder, "ext", "auth.py"), ) sketch.write_template( "models_login_tpl", templates.models, pjoin(sketch.app_folder, "models", "user.py"), ) sketch.write_template( "examples_login_auth_tpl", templates.examples, pjoin( sketch.app_folder, "examples", "auth_examples.py", ), ) sketch.write_template( "examples_init_auth_tpl", templates.examples, pjoin( sketch.app_folder, "examples", "__init__.py", ), ) sketch.write_template( "utils_security_login_rbac_tpl", templates.utils.security, pjoin(sketch.app_folder, "utils", "security", "rbac.py"), ) sketch.write_template( "utils_security_password_hasher_tpl", templates.utils.security, pjoin(sketch.app_folder, "utils", "security", "password_hasher.py"), mode="w", ) sketch.write_template( "site_login_auth_views_tpl", templates.site, pjoin( sketch.app_folder, "site", "views.py", ), ) sketch.write_template( "site_login_auth_forms_tpl", templates.site, pjoin( sketch.app_folder, "site", "forms.py", ), ) sketch.write_template( "site_login_init_tpl", templates.site, pjoin( sketch.app_folder, "site", "__init__.py", ), ) os.makedirs(pjoin(sketch.app_folder, "site", "templates")) sketch.write_template( "login_html_tpl", templates.site.templates, pjoin( sketch.app_folder, "site", "templates", "login.html", ), ) sketch.write_template( "register_html_tpl", templates.site.templates, pjoin( sketch.app_folder, "site", "templates", "register.html", ), ) sketch.write_template( "dashboard_html_tpl", templates.site.templates, pjoin( sketch.app_folder, "site", "templates", "dashboard.html", ), ) return True