Exemplo n.º 1
0
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",),
        )
Exemplo n.º 2
0
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",
        ),
    )
Exemplo n.º 3
0
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",
        ),
    )
Exemplo n.º 4
0
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
Exemplo n.º 5
0
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))"
Exemplo n.º 6
0
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
Exemplo n.º 7
0
def postgres_handler(sketch: Sketch):
    if sketch.database == "postgres":
        sketch.add_requirements("psycopg2")
        handle_sql_db(sketch)
        sketch.settings["production"][
            "SQLALCHEMY_DATABASE_URI"
        ] = "postgres://<user>:<password>@<server_ip>/MY_DATABASE"
        return True
Exemplo n.º 8
0
def mysql_handler(sketch: Sketch):
    if sketch.database == "mysql":
        sketch.add_requirements("mysqlclient")
        handle_sql_db(sketch)
        sketch.settings["production"][
            "SQLALCHEMY_DATABASE_URI"
        ] = "mysql+mysqldb://<user>:<password>@<server_ip>/MY_DATABASE"
        return True
Exemplo n.º 9
0
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",),
    )
Exemplo n.º 10
0
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"),
    )
Exemplo n.º 11
0
def restx_handler(sketch: Sketch):
    if sketch.api_framework == "restx":
        sketch.add_requirements(reqs.FLASK_RESTX)

        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_restx_jwtext_tpl",
                templates.api,
                pjoin(sketch.app_folder, "api", "__init__.py"),
            )
        else:
            sketch.write_template(
                "api_init_restx_noauth_tpl",
                templates.api,
                pjoin(sketch.app_folder, "api", "__init__.py"),
            )

        if sketch.api_auth_framework == "none":
            resource_tpl = "api_examples_restx_pet_tpl"
        else:
            resource_tpl = "api_examples_restx_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
Exemplo n.º 12
0
def dynaconf_handler(sketch: Sketch):
    if sketch.config_framework == "dynaconf":
        sketch.add_requirements("dynaconf")

        settings_toml = sort_settings(sketch.settings)
        secrets_toml = sort_settings(sketch.secrets)

        try:
            sketch.dev_extensions.remove("debugtoolbar")
            settings_toml["development"]["EXTENSIONS"] = [
                "flask_debugtoolbar:DebugToolbarExtension",
                "dynaconf_merge_unique",
            ]

        except ValueError:
            settings_toml["development"]["EXTENSIONS"] = [
                "dynaconf_merge_unique"
            ]

        for extension in sketch.extensions:
            ext = extension.rsplit(".", 1)[-1]
            aux = ""
            if len(extension.rsplit(".", 1)) > 1:
                aux = "." + extension.rsplit(".", 1)[0]
            settings_toml["default"]["EXTENSIONS"].append(
                "{}.ext{}.{}:init_app".format(sketch.app_folder_name, aux,
                                              ext))

        with open(pjoin(sketch.project_folder, "settings.toml"), "w") as f:
            toml.dump(settings_toml, f, encoder=FlaskSketchTomlEncoder())

        with open(pjoin(sketch.project_folder, ".secrets.toml"), "w") as f:
            toml.dump(secrets_toml, f, encoder=FlaskSketchTomlEncoder())

        sketch.write_template(
            "config_dynaconf_tpl",
            templates.config,
            pjoin(
                sketch.app_folder,
                "config",
                "__init__.py",
            ),
        )
        sketch.write_template(
            "app_dynaconf_conf_tpl",
            templates.app,
            pjoin(sketch.app_folder, "app.py"),
        )

        return True
Exemplo n.º 13
0
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",),
        )
Exemplo n.º 14
0
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",
            ),
        )
Exemplo n.º 15
0
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
Exemplo n.º 16
0
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"),
    )
Exemplo n.º 17
0
def make_commom(sketch: Sketch):
    paf = sketch.app_folder
    pf = sketch.project_folder

    sketch.secret_key = random_string(32)

    os.makedirs(pjoin(pf, "tests"))
    os.makedirs(paf)
    os.makedirs(pjoin(paf, "ext"))
    os.makedirs(pjoin(paf, "models", "examples"))
    os.makedirs(pjoin(paf, "config"))
    os.makedirs(pjoin(paf, "commands"))
    os.makedirs(pjoin(paf, "utils", "security"))
    os.makedirs(pjoin(paf, "examples"))
    if "admin" in sketch.features:
        os.makedirs(pjoin(paf, "ext", "admin"))
    open(pjoin(paf, "__init__.py"), "a").close()
    open(pjoin(paf, "app.py"), "a").close()
    open(pjoin(paf, "ext", "__init__.py"), "a").close()
    open(pjoin(paf, "models", "__init__.py"), "a").close()
    open(pjoin(paf, "models", "examples", "__init__.py"), "a").close()
    open(pjoin(paf, "config", "__init__.py"), "a").close()
    open(pjoin(paf, "commands", "__init__.py"), "a").close()
    open(pjoin(paf, "utils", "__init__.py"), "a").close()
    open(pjoin(paf, "utils", "security", "__init__.py"), "a").close()
    open(pjoin(paf, "examples", "__init__.py"), "a").close()
    sketch.add_requirements("flask")
    sketch.add_requirements("black", "isort", "flake8", dev=True)

    sketch.write_template(".gitignore_tpl", templates, pjoin(pf, ".gitignore"))
    sketch.write_template("wsgi_tpl", templates, pjoin(pf, "wsgi.py"))
    sketch.write_template(
        "examples_init_tpl",
        templates.examples,
        pjoin(paf, "examples", "__init__.py"),
    )
Exemplo n.º 18
0
def basicauth_handler(sketch: Sketch):
    if sketch.api_auth_framework == "basicauth":
        sketch.add_requirements("flask-basicauth")
        return True
Exemplo n.º 19
0
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
Exemplo n.º 20
0
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
Exemplo n.º 21
0
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
Exemplo n.º 22
0
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
Exemplo n.º 23
0
def basicauth_web_handler(sketch: Sketch):
    if sketch.auth_framework == "basicauth":
        sketch.add_requirements("flask-basicAuth")
        sketch.secrets["default"]["BASIC_AUTH_PASSWORD"] = "******"
        sketch.secrets["default"]["BASIC_AUTH_PASSWORD"] = random_string()
        return True
Exemplo n.º 24
0
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
Exemplo n.º 25
0
def mongodb_handler(sketch: Sketch):
    if sketch.database == "mongodb":
        sketch.add_requirements("flask-mongoengine")
        return True
Exemplo n.º 26
0
def basicauth_handler(sketch: Sketch):
    if sketch.api_auth_framework == "basicauth":
        sketch.add_requirements(reqs.FLASK_BASICAUTH)
        return True