91 lines
2.0 KiB
Python
91 lines
2.0 KiB
Python
import click
|
|
import os
|
|
|
|
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
|
|
|
|
|
|
def init_db():
|
|
db.drop_all()
|
|
db.create_all()
|
|
|
|
|
|
def generate_admin():
|
|
username = current_app.config['USERNAME']
|
|
password = current_app.config['PASSWORD']
|
|
|
|
u = User(username, generate_password_hash(password))
|
|
db.session.add(u)
|
|
db.session.commit()
|
|
|
|
|
|
def add_defaults():
|
|
default_category = Category('Default')
|
|
db.session.add(default_category)
|
|
db.session.commit()
|
|
|
|
|
|
def add_files():
|
|
existing_files = os.listdir(current_app.config['UPLOAD_FOLDER'])
|
|
default_category = Category.query.filter_by(name='Default').first()
|
|
added = 0
|
|
|
|
for f in existing_files:
|
|
search = File.query.filter_by(filename=f).first()
|
|
if search is None:
|
|
file = File(f, 0, default_category.id)
|
|
db.session.add(file)
|
|
added += 1
|
|
db.session.commit()
|
|
|
|
return added
|
|
|
|
|
|
@click.command('init-db')
|
|
@with_appcontext
|
|
def init_db_command():
|
|
"""
|
|
Creates the db file
|
|
"""
|
|
init_db()
|
|
|
|
|
|
@click.command("add-defaults")
|
|
@with_appcontext
|
|
def add_defaults_command():
|
|
"""
|
|
Initializes the database with default data
|
|
"""
|
|
add_defaults()
|
|
click.echo("Defaults added")
|
|
|
|
|
|
@click.command('generate-admin')
|
|
@with_appcontext
|
|
def generate_admin_command():
|
|
"""Creates the admin of the system"""
|
|
generate_admin()
|
|
click.echo('The admin was created')
|
|
|
|
|
|
@click.command('add-files')
|
|
@with_appcontext
|
|
def add_files_command():
|
|
"""
|
|
Generates the rows in the database for the files currently uploaded
|
|
They all will have default privacy, so, public
|
|
"""
|
|
added_size= add_files()
|
|
click.echo(f'added {added_size} files')
|
|
|
|
|
|
def init_app(app):
|
|
app.cli.add_command(init_db_command)
|
|
app.cli.add_command(add_defaults_command)
|
|
app.cli.add_command(generate_admin_command)
|
|
app.cli.add_command(add_files_command)
|