From 06fd86d337355a71d99e4182b4524811662dee72 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Fri, 17 Jun 2016 10:17:44 +0200 Subject: [PATCH] Move remaining stuff in manage.py to application. --- accountant/__init__.py | 25 +++++++++++++++++++++ accountant/views/users.py | 23 ++++++++++++++++--- manage.py | 47 --------------------------------------- 3 files changed, 45 insertions(+), 50 deletions(-) delete mode 100755 manage.py diff --git a/accountant/__init__.py b/accountant/__init__.py index 867655e..6da9cb1 100644 --- a/accountant/__init__.py +++ b/accountant/__init__.py @@ -14,8 +14,12 @@ You should have received a copy of the GNU Affero General Public License along with Accountant. If not, see . """ +import click + from flask import Flask, redirect, url_for +from flask_alembic import Alembic +from flask_alembic.cli.click import cli as alembic_cli from flask_restplus import Api from flask_cors import CORS from flask_sqlalchemy import SQLAlchemy @@ -33,6 +37,27 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True # Database initialization. db = SQLAlchemy(app) +# Database migrations. +alembic = Alembic(app) + +app.cli.add_command(alembic_cli, 'db') + +# Database initialization. +@app.cli.command() +@click.pass_context +def initdb(ctx): + """ Create the database ans stamp it. """ + + click.echo("Get table list.") + tables = db.engine.table_names() + + if 'alembic_version' in tables: + ctx.fail("Database already initialized.") + + #db.metadata.create_all(bind=db.engine) + #alembic.stamp() + click.echo("Database created.") + # API initialization. authorizations = { 'apikey': { diff --git a/accountant/views/users.py b/accountant/views/users.py index 2d05237..495e4f0 100644 --- a/accountant/views/users.py +++ b/accountant/views/users.py @@ -19,12 +19,12 @@ import arrow from functools import wraps +import click + from flask import request, g from flask_restplus import Resource, marshal_with_field -from accountant import app - -from .. import api +from .. import app, api from ..fields import Object @@ -32,6 +32,23 @@ from ..models.users import User from .models import user_model, token_model, login_model +# Define commands to handle users. +@app.cli.group() +def user(): + """ User management. """ + pass + +@user.command() +def add(email, password): + """ Add a new user. """ + user = User() + user.email = email + user.password = User.hash_password(password) + + db.session.add(user) + + click.echo("User '%s' successfully added." % email) + def load_user_from_token(token): return User.verify_auth_token(token) diff --git a/manage.py b/manage.py deleted file mode 100755 index 2f60926..0000000 --- a/manage.py +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/env python -from flask.ext.script import Manager -from flask.ext.migrate import Migrate, MigrateCommand, stamp - -from accountant import app, db - -from accountant.api.models.users import User - -manager = Manager(app) - -migrate = Migrate(app, db) - -manager.add_command('db', MigrateCommand) - - -@manager.command -def initdb(): - """ Create the database ans stamp it. """ - - tables = db.engine.table_names() - - if len(tables) > 1 and 'alembic_version' not in tables: - exit("Database already initialized.") - - db.metadata.create_all(bind=db.engine) - stamp() - print("Database created.") - -user_manager = Manager(usage="Manage users.") - -manager.add_command('user', user_manager) - - -@user_manager.command -def add(email, password): - """ Add a new user. """ - user = User() - user.email = email - user.password = User.hash_password(password) - - db.session.add(user) - - print("User '%s' successfully added." % email) - - -if __name__ == "__main__": - manager.run()