From 67e3d752351c7676d87c3150c85036a8fc857e6c Mon Sep 17 00:00:00 2001 From: pi3c Date: Tue, 19 Sep 2023 09:40:10 +0300 Subject: [PATCH] cli create-superuser --- poetry.lock | 2 +- pyproger/app.py | 4 ++++ pyproger/cli/__init__.py | 0 pyproger/cli/commands.py | 44 ++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 1 + 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 pyproger/cli/__init__.py create mode 100644 pyproger/cli/commands.py diff --git a/poetry.lock b/poetry.lock index d925115..11012d5 100644 --- a/poetry.lock +++ b/poetry.lock @@ -636,4 +636,4 @@ email = ["email-validator"] [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "0b3e5f508d905b937d323c2545bbba6bcf2ee7a5f1227c549cf0d12e49ffac99" +content-hash = "5e2949c8ed8b545cd39083eb1486141cda4bb10ee12a445f5fab80c64c77aa9a" diff --git a/pyproger/app.py b/pyproger/app.py index ee816f3..61d1e88 100644 --- a/pyproger/app.py +++ b/pyproger/app.py @@ -41,6 +41,10 @@ def create_app(test_config=None): admin.add_view(TagView(Tag, db.session, category="posts")) admin.add_view(PostView(Post, db.session, category="posts")) + from pyproger.cli.commands import bp_cli + + app.register_blueprint(bp_cli) + @security.context_processor def security_context_processor(): return dict( diff --git a/pyproger/cli/__init__.py b/pyproger/cli/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pyproger/cli/commands.py b/pyproger/cli/commands.py new file mode 100644 index 0000000..184b823 --- /dev/null +++ b/pyproger/cli/commands.py @@ -0,0 +1,44 @@ +import click +from flask import Blueprint, current_app +from flask_security.utils import hash_password + +from pyproger.dbase import db, user_datastore +from pyproger.dbase.models import Role + +bp_cli = Blueprint("bp_cli", __name__, cli_group=None) + + +@bp_cli.cli.command("create-superuser") +@click.argument("name") +def create_superuser(name): + """Создание учетной записи админа + + Использование: + create-superuser + Аргументы: + : Ник администратора + """ + + fname = input("Введите Ваше имя:\n") + lname = input("Введиде Вашу фамилию:\n") + email = input("Ваша электронная почта:\n") + password = input("Пароль:\n") + + with current_app.app_context(): + user_role = Role(name="user") + super_user_role = Role(name="superuser") + db.session.add(user_role) + db.session.add(super_user_role) + db.session.commit() + + super_user = user_datastore.create_user( + username=name, + first_name=fname, + last_name=lname, + email=email, + password=hash_password(password), + roles=[user_role, super_user_role], + ) + + db.session.commit() + return diff --git a/pyproject.toml b/pyproject.toml index ede1c93..9a50847 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,7 @@ flask-security-too = "^5.3.0" flask-migrate = "^4.0.5" flask-admin = "^1.6.1" psycopg2 = "^2.9.7" +click = "^8.1.7" [build-system]