Skip to content

Commit

Permalink
Migrate form Flask-Script to the built-in integration of the click co…
Browse files Browse the repository at this point in the history
…mmand line interface of Flask.
  • Loading branch information
cedricbonhomme committed Apr 7, 2020
1 parent 33c0806 commit ab73f80
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 42 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,21 @@ $ cd MOSP/
$ npm install
$ poetry install
$ poetry shell
$ python manager.py db_create
$ python manager.py db_init
$ python manager.py import_licenses_from_spdx
$ python manager.py create_admin <username> <password>
$ pybabel compile -d mosp/translations
$ python runserver.py
$ export FLASK_APP=runserver.py
$ export FLASK_ENV=development
$ python db_create
$ python db_init
$ python import_licenses_from_spdx
$ python create_admin --nickname <nickname> --password <password>
$ flask run
* Serving Flask app "runserver" (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 221-873-938
```


Expand All @@ -65,7 +74,7 @@ $ heroku buildpacks:add --index 1 heroku/python
$ heroku buildpacks:add --index 2 https://github.com/heroku/heroku-buildpack-nodejs
$ git push heroku master
$ heroku run init
$ heroku run python manager.py import_licenses_from_spdx
$ heroku run flask import_licenses_from_spdx
$ heroku ps:scale web=1
```

Expand All @@ -79,8 +88,8 @@ password.
If you want to create other users programmatically:

```bash
$ heroku run python manager.py create_user <login> <password>
$ heroku run python manager.py create_admin <login> <password>
$ heroku run flask create_user --nickname <nickname> --password <password>
$ heroku run flask create_admin --nickname <nickname> --password <password>
```

## Contributing
Expand Down
32 changes: 14 additions & 18 deletions manager.py → mosp/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,68 @@
# -*- coding: utf-8 -*-

import logging
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand

import click

import mosp.scripts
import mosp.models
from mosp.bootstrap import application, db

logger = logging.getLogger('manager')

Migrate(application, db)
manager = Manager(application)
manager.add_command('db', MigrateCommand)
logger = logging.getLogger('commands')


@manager.command
@application.cli.command("uml_graph")
def uml_graph():
"UML graph from the models."
with application.app_context():
mosp.models.uml_graph(db)


@manager.command
@application.cli.command("db_empty")
def db_empty():
"Will drop every datas stocked in db."
with application.app_context():
mosp.models.db_empty(db)


@manager.command
@application.cli.command("db_create")
def db_create():
"Will create the database."
with application.app_context():
mosp.models.db_create(db, application.config['DB_CONFIG_DICT'],
application.config['DATABASE_NAME'])


@manager.command
@application.cli.command("db_init")
def db_init():
"Will create the database from conf parameters."
with application.app_context():
mosp.models.db_init(db)


@manager.command
@application.cli.command("import_licenses_from_spdx")
def import_licenses_from_spdx():
"Import licenses from spdx.org."
print("Importing licenses from spdx.org...")
with application.app_context():
mosp.scripts.import_licenses_from_spdx()


@manager.command
@application.cli.command("create_user")
@click.option('--login', default='admin', help='Login')
@click.option('--password', default='password', help='Password')
def create_user(login, password):
"Initializes a user"
print("Creation of the user {} ...".format(login))
with application.app_context():
mosp.scripts.create_user(login, password, False)


@manager.command
@application.cli.command("create_admin")
@click.option('--login', default='admin', help='Login')
@click.option('--password', default='password', help='Password')
def create_admin(login, password):
"Initializes an admin user"
print("Creation of the admin user {} ...".format(login))
with application.app_context():
mosp.scripts.create_user(login, password, True)


if __name__ == '__main__':
manager.run()
16 changes: 1 addition & 15 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Flask-Login = "^0.5.0"
Flask-Principal = "^0.4.0"
Flask-WTF = "^0.14.2"
Flask-Migrate = "^2.5.2"
Flask-Script = "^2.0.6"
Flask-Admin = "^1.5.4"
Flask-paginate = "^0.5.5"
Flask-Mail = "^0.9.1"
Expand Down
15 changes: 15 additions & 0 deletions runserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
# -*- coding: utf-8 -*-

from mosp.bootstrap import application
from mosp import commands


def register_commands(app):
"""Register Click commands."""
app.cli.add_command(commands.uml_graph)
app.cli.add_command(commands.db_empty)
app.cli.add_command(commands.db_create)
app.cli.add_command(commands.db_init)
app.cli.add_command(commands.import_licenses_from_spdx)
app.cli.add_command(commands.create_admin)
app.cli.add_command(commands.create_user)


with application.app_context():

Expand All @@ -20,6 +33,8 @@
application.register_blueprint(views.api.v1.blueprint_schema)
application.register_blueprint(views.api.v1.blueprint_object)

register_commands(application)


if __name__ == '__main__':
application.run(host=application.config['HOST'],
Expand Down

0 comments on commit ab73f80

Please sign in to comment.