Move remaining stuff in manage.py to application.
This commit is contained in:
parent
7947ebd81f
commit
06fd86d337
@ -14,8 +14,12 @@
|
|||||||
You should have received a copy of the GNU Affero General Public License
|
You should have received a copy of the GNU Affero General Public License
|
||||||
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
|
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
|
||||||
"""
|
"""
|
||||||
|
import click
|
||||||
|
|
||||||
from flask import Flask, redirect, url_for
|
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_restplus import Api
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
@ -33,6 +37,27 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
|
|||||||
# Database initialization.
|
# Database initialization.
|
||||||
db = SQLAlchemy(app)
|
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.
|
# API initialization.
|
||||||
authorizations = {
|
authorizations = {
|
||||||
'apikey': {
|
'apikey': {
|
||||||
|
@ -19,12 +19,12 @@ import arrow
|
|||||||
|
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
|
import click
|
||||||
|
|
||||||
from flask import request, g
|
from flask import request, g
|
||||||
from flask_restplus import Resource, marshal_with_field
|
from flask_restplus import Resource, marshal_with_field
|
||||||
|
|
||||||
from accountant import app
|
from .. import app, api
|
||||||
|
|
||||||
from .. import api
|
|
||||||
|
|
||||||
from ..fields import Object
|
from ..fields import Object
|
||||||
|
|
||||||
@ -32,6 +32,23 @@ from ..models.users import User
|
|||||||
|
|
||||||
from .models import user_model, token_model, login_model
|
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):
|
def load_user_from_token(token):
|
||||||
return User.verify_auth_token(token)
|
return User.verify_auth_token(token)
|
||||||
|
47
manage.py
47
manage.py
@ -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()
|
|
Loading…
Reference in New Issue
Block a user