Ahora se usan archivos .env para la configuracion en vez del archivo en la instancia Tambien se paso a usar docker-compose para tambien crear la base de datos Se tienen volumenes separados para la carpeta de uploads y para la base de datos Cambie los comandos para que sea solo uno poble la base de datos con todo lo que implica Se paso a mysql tambien
77 lines
2.1 KiB
Python
77 lines
2.1 KiB
Python
import os
|
|
import click
|
|
|
|
from flask import current_app
|
|
from flask.cli import with_appcontext
|
|
|
|
from werkzeug.security import generate_password_hash
|
|
|
|
from files.models import db, User, Category, File, FileType
|
|
|
|
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 add_defaults():
|
|
if Category.query.filter_by(name='Default').first() is None:
|
|
default_category = Category('Default')
|
|
db.session.add(default_category)
|
|
if FileType.query.filter_by(name='Default').first() is None:
|
|
default_file_type = FileType('Default')
|
|
db.session.add(default_file_type)
|
|
if FileType.query.filter_by(name='Code').first() is None:
|
|
code_file_type = FileType('Code')
|
|
db.session.add(code_file_type)
|
|
if FileType.query.filter_by(name='Image').first() is None:
|
|
image_file_type = FileType('Image')
|
|
db.session.add(image_file_type)
|
|
db.session.commit()
|
|
|
|
|
|
def add_files():
|
|
existing_files = os.listdir(current_app.config['UPLOAD_FOLDER'])
|
|
default_category = Category.query.filter_by(name='Default').first()
|
|
default_file_type = FileType.query.filter_by(name='Default').first()
|
|
added = 0
|
|
|
|
for existing_file in existing_files:
|
|
search = File.query.filter_by(filename=existing_file).first()
|
|
if search is None:
|
|
file = File(
|
|
existing_file,
|
|
0,
|
|
default_category.id,
|
|
default_file_type.id)
|
|
db.session.add(file)
|
|
added += 1
|
|
db.session.commit()
|
|
|
|
return added
|
|
|
|
|
|
@click.command('init-db')
|
|
@with_appcontext
|
|
def init_db_command():
|
|
"""
|
|
Creates and initializes the db with data
|
|
If the db was previously created it just keep it and its data
|
|
"""
|
|
init_db()
|
|
generate_admin()
|
|
add_defaults()
|
|
add_files()
|
|
|
|
|
|
def init_app(app):
|
|
app.cli.add_command(init_db_command)
|