Skip to content

Commit

Permalink
cli: support for Flask>=0.11
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Kuncar <jiri.kuncar@cern.ch>
  • Loading branch information
jirikuncar committed Jun 24, 2016
1 parent 9d93baa commit 90fd211
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
34 changes: 31 additions & 3 deletions example/app.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015, 2016 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Simple example application for Invenio-DB package."""

import os

from flask import Flask
from flask_cli import FlaskCLI
from flask_sqlalchemy import SQLAlchemy

from invenio_db import InvenioDB

app = Flask('demo')
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get(
'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'
)
FlaskCLI(app)

if not hasattr(app, 'cli'):
from flask_cli import FlaskCLI
FlaskCLI(app)

InvenioDB(app)
5 changes: 3 additions & 2 deletions invenio_db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
Flask 1.0+ or alternatively use the Flask-CLI extension):
>>> from flask import Flask
>>> from flask_cli import FlaskCLI
>>> app = Flask('myapp')
>>> cli = FlaskCLI(app)
>>> if not hasattr(app, 'cli'):
... from flask_cli import FlaskCLI
... cli = FlaskCLI(app)
Next, initialize your extension:
Expand Down
8 changes: 6 additions & 2 deletions invenio_db/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015 CERN.
# Copyright (C) 2015, 2016 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
Expand Down Expand Up @@ -31,11 +31,15 @@
import click
from click import _termui_impl
from flask import current_app
from flask_cli import with_appcontext
from sqlalchemy_utils.functions import create_database, database_exists, \
drop_database
from werkzeug.local import LocalProxy

try:
from flask.cli import with_appcontext
except ImportError: # pragma: no cover
from flask_cli import with_appcontext

_db = LocalProxy(lambda: current_app.extensions['sqlalchemy'].db)

# Fix Python 3 compatibility issue in click
Expand Down
12 changes: 9 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015 CERN.
# Copyright (C) 2015, 2016 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
Expand Down Expand Up @@ -31,7 +31,11 @@

import pytest
from flask import Flask
from flask_cli import FlaskCLI, ScriptInfo

try:
from flask.cli import ScriptInfo
except ImportError:
from flask_cli import ScriptInfo


@pytest.fixture()
Expand All @@ -52,7 +56,9 @@ def app():
SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
'sqlite:///test.db')
)
FlaskCLI(app)
if not hasattr(app, 'cli'):
from flask_cli import FlaskCLI
FlaskCLI(app)
return app


Expand Down
2 changes: 1 addition & 1 deletion tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import pytest
import sqlalchemy as sa
from click.testing import CliRunner
from flask_cli import ScriptInfo
from conftest import ScriptInfo
from mock import patch
from pkg_resources import EntryPoint
from sqlalchemy.exc import IntegrityError
Expand Down

0 comments on commit 90fd211

Please sign in to comment.