Decidi pasar todo esto a docker igual para tener todo ahi, parece mas ordenado y mas facil de mantener en mi servidor de paso tambien hice el trabajo de pasar los modelos a sqlalchemy para usar mysql :3
47 lines
996 B
Python
47 lines
996 B
Python
import click
|
|
|
|
from flask import current_app
|
|
from flask.cli import with_appcontext
|
|
|
|
from werkzeug.security import generate_password_hash
|
|
|
|
from www.models import db, User, Now
|
|
|
|
|
|
def init_db():
|
|
db.create_all()
|
|
|
|
|
|
def generate_admin():
|
|
username = current_app.config['USERNAME']
|
|
|
|
if User.query.filter_by(username=username).first() is None:
|
|
password = current_app.config['PASSWORD']
|
|
|
|
user = User(username, generate_password_hash(password))
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
|
|
def generate_base_now():
|
|
now = Now.query.first()
|
|
if now is None:
|
|
now = Now('', '')
|
|
db.session.add(now)
|
|
db.session.commit()
|
|
|
|
|
|
@click.command('init-db')
|
|
@with_appcontext
|
|
def init_db_command():
|
|
"""
|
|
Creates and initializes the db with the necesary data
|
|
If the db existed previously, it keeps it and his data
|
|
"""
|
|
init_db()
|
|
generate_admin()
|
|
generate_base_now()
|
|
|
|
|
|
def init_app(app):
|
|
app.cli.add_command(init_db_command)
|