cli create-superuser

main
Сергей Ванюшкин 2023-09-19 09:40:10 +03:00
parent 5852352286
commit 67e3d75235
5 changed files with 50 additions and 1 deletions

2
poetry.lock generated
View File

@ -636,4 +636,4 @@ email = ["email-validator"]
[metadata]
lock-version = "2.0"
python-versions = "^3.11"
content-hash = "0b3e5f508d905b937d323c2545bbba6bcf2ee7a5f1227c549cf0d12e49ffac99"
content-hash = "5e2949c8ed8b545cd39083eb1486141cda4bb10ee12a445f5fab80c64c77aa9a"

View File

@ -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(

0
pyproger/cli/__init__.py Normal file
View File

44
pyproger/cli/commands.py Normal file
View File

@ -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 <name>
Аргументы:
<name>: Ник администратора
"""
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

View File

@ -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]