Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved compatibility issues related to the application context for running on recent versions of SQLAlchemy and Flask. #215

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions scrapydweb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ def handle_db(app):
# self.app = app
# if app is not None:
# self.init_app(app)
db.app = app # https://github.com/viniciuschiele/flask-apscheduler/blob/master/examples/flask_context.py
db.init_app(app) # http://flask-sqlalchemy.pocoo.org/2.3/contexts/
db.create_all()
with app.app_context():
db.app = app # https://github.com/viniciuschiele/flask-apscheduler/blob/master/examples/flask_context.py
db.init_app(app) # http://flask-sqlalchemy.pocoo.org/2.3/contexts/
db.create_all()

# https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-vii-error-handling
@app.teardown_request
Expand Down
2 changes: 1 addition & 1 deletion scrapydweb/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def main():
# "scrapydweb -h" ends up here
update_app_config(app.config, args)
try:
check_app_config(app.config)
check_app_config(app.config, app)
except AssertionError as err:
logger.error("Check app config fail: ")
sys.exit(u"\n{err}\n\nCheck and update your settings in {path}\n".format(
Expand Down
7 changes: 4 additions & 3 deletions scrapydweb/utils/check_app_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
""", re.X)


def check_app_config(config):
def check_app_config(config, app):
def check_assert(key, default, is_instance, allow_zero=True, non_empty=False, containing_type=None):
if is_instance is int:
if allow_zero:
Expand Down Expand Up @@ -110,8 +110,9 @@ def check_assert(key, default, is_instance, allow_zero=True, non_empty=False, co
# Note that check_app_config() is executed multiple times in test
if node not in jobs_table_map:
jobs_table_map[node] = create_jobs_table(re.sub(STRICT_NAME_PATTERN, '_', scrapyd_server))
db.create_all(bind='jobs')
logger.debug("Created %s tables for JobsView", len(jobs_table_map))
with app.app_context():
db.create_all()
logger.debug("Created %s tables for JobsView", len(jobs_table_map))

check_assert('LOCAL_SCRAPYD_LOGS_DIR', '', str)
check_assert('LOCAL_SCRAPYD_SERVER', '', str)
Expand Down
2 changes: 1 addition & 1 deletion scrapydweb/views/baseview.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(self, *args, **kwargs):
self.logger.debug('request.args of %s\n%s', request.url, self.json_dumps(request.args))
if request.form:
self.logger.debug('request.form from %s\n%s', request.url, self.json_dumps(request.form))
if request.json:
if request.is_json:
self.logger.debug('request.json from %s\n%s', request.url, self.json_dumps(request.json))
if request.files:
self.logger.debug('request.files from %s\n\n %s\n', request.url, request.files)
Expand Down
2 changes: 1 addition & 1 deletion scrapydweb/views/utilities/send_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(self):

self.text = self.view_args['text'] or request.args.get('text', None)
if not self.text:
self.text = self.json_dumps(self.form) if self.form else 'test'
self.text = self.form.get('text') if self.form else 'test'
self.logger.debug('text: %s', self.text)

self.js = {}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_a_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_check_app_config(app, client):

# ['username:[email protected]:6800', ]
app.config['SCRAPYD_SERVERS'] = app.config['_SCRAPYD_SERVERS']
check_app_config(app.config)
check_app_config(app.config, app)

strings = []

Expand All @@ -66,7 +66,7 @@ def test_check_app_config(app, client):

# ['username:[email protected]:6800', ]
app.config['SCRAPYD_SERVERS'] = app.config['_SCRAPYD_SERVERS']
check_app_config(app.config)
check_app_config(app.config, app)

assert app.config['LOGPARSER_PID'] is None
assert app.config['POLL_PID'] is None
Expand Down