Added default admin account creation if database is empty
This commit is contained in:
parent
abde1ac590
commit
983b787bbb
@ -1,6 +1,6 @@
|
||||
from flask import Flask
|
||||
from flask_jwt_extended import JWTManager
|
||||
from models import db
|
||||
from models import db, init_db
|
||||
from user_views import user_bp
|
||||
from task_views import task_bp
|
||||
from dotenv import load_dotenv
|
||||
@ -18,4 +18,5 @@ if __name__ == "__main__":
|
||||
jwt = JWTManager(app)
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
init_db()
|
||||
app.run(debug=True, host='0.0.0.0')
|
||||
|
@ -1,14 +1,25 @@
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from werkzeug.security import generate_password_hash
|
||||
import os
|
||||
|
||||
db = SQLAlchemy()
|
||||
|
||||
def init_db():
|
||||
"""Create default admin account if database is empty"""
|
||||
with db.session.begin():
|
||||
if not User.query.first(): # Check if user table is empty
|
||||
admin_password = os.getenv("TODOLIST_ADMIN_PASSWORD", "admin")
|
||||
hashed_password = generate_password_hash(admin_password)
|
||||
admin = User(username='admin', email='admin@example.pl', password=hashed_password, role='Administrator')
|
||||
db.session.add(admin)
|
||||
db.session.commit()
|
||||
|
||||
class User(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
username = db.Column(db.String(20), unique=True, nullable=False)
|
||||
email = db.Column(db.String(120), unique=True, nullable=False)
|
||||
role = db.Column(db.Enum('Administrator', 'User'), default='User')
|
||||
password = db.Column(db.String(60), nullable=False)
|
||||
password = db.Column(db.String(162), nullable=False)
|
||||
|
||||
def to_dict(self):
|
||||
return {"id": self.id, "username": self.username, "email": self.email, "role": self.role}
|
||||
|
Loading…
x
Reference in New Issue
Block a user