diff --git a/fractalis/__init__.py b/fractalis/__init__.py index 86e12d2bc5061531c447795ae820d157d7251844..e206b78b0652342fe384e3469377a646e95a2feb 100644 --- a/fractalis/__init__.py +++ b/fractalis/__init__.py @@ -5,17 +5,15 @@ Modules in this package: """ from flask import Flask -from fractalis.celery import make_celery from fractalis.session import RedisSessionInterface from fractalis.analytics.controllers import analytics_blueprint app = Flask(__name__) app.config.from_object('fractalis.config') -celery_app = make_celery(app) +# app.config.from_envvar('FRACTALIS_CONFIG') app.session_interface = RedisSessionInterface(app.config) app.register_blueprint(analytics_blueprint, url_prefix='/analytics') if __name__ == '__main__': - app.config.from_envvar('FRACTALIS_CONFIG') app.run() diff --git a/fractalis/analytics/controllers.py b/fractalis/analytics/controllers.py index 7be14804f707bd2269fa8dd22fce3f4559cbdd7c..b61e3e0035b06d285cb33049ce1098f033883962 100644 --- a/fractalis/analytics/controllers.py +++ b/fractalis/analytics/controllers.py @@ -1,23 +1,25 @@ import json -import uuid -from flask import Blueprint +from flask import Blueprint, abort + +import fractalis.analytics.scripts # noqa +from fractalis.celery import app +from fractalis.analytics.form import POSTAnalyticsForm -import fractalis.analytics.scripts -from fractalis import celery_app analytics_blueprint = Blueprint('analytics_blueprint', __name__) def get_celery_task(task): - celery_task = eval('fractalis.analytics.scripts.{}'.format(task)) - return celery_task + task = eval('fractalis.analytics.scripts.{}'.format(task)) + return task @analytics_blueprint.route('', methods=['POST']) def create_job(): - body = json.dumps({'job_id': str(uuid.uuid4())}) - return body, 201 + form = POSTAnalyticsForm() + if not form.validate(): + abort(400) @analytics_blueprint.route('/<uuid:job_id>', methods=['GET']) diff --git a/fractalis/analytics/form.py b/fractalis/analytics/form.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..dd2403c11ffdeecdcd204729af0435ba1b28d25f 100644 --- a/fractalis/analytics/form.py +++ b/fractalis/analytics/form.py @@ -0,0 +1,7 @@ +from flask_wtf import FlaskForm +from wtforms import StringField +from wtforms.validators import DataRequired + + +class POSTAnalyticsForm(FlaskForm): + task = StringField('task', validators=[DataRequired()]) diff --git a/fractalis/analytics/scripts/test/tasks.py b/fractalis/analytics/scripts/test/tasks.py index f80b1edd8b4de7ec4d69a038dad740ab19c248d3..1fb7ef303dedbd59fbf303d23c37389a4724036a 100644 --- a/fractalis/analytics/scripts/test/tasks.py +++ b/fractalis/analytics/scripts/test/tasks.py @@ -1,18 +1,18 @@ from time import sleep -from fractalis import celery_app +from fractalis.celery import app -@celery_app.task +@app.task def add(a, b): return a + b -@celery_app.task +@app.task def do_nothing(time): sleep(time) -@celery_app.task +@app.task def div(a, b): return a / b diff --git a/fractalis/celery.py b/fractalis/celery.py index 3096fe7ec55f48a4cb5f352fe290ff246f3a2644..2758cfd8862c073e862b2ae0e73c6949f34ed2fc 100644 --- a/fractalis/celery.py +++ b/fractalis/celery.py @@ -18,11 +18,7 @@ def get_scripts_packages(): packages.append(package) return packages - -def make_celery(app): - celery = Celery(app.import_name, - backend=app.config['CELERY_RESULT_BACKEND'], - broker=app.config['CELERY_BROKER_URL']) - celery.conf.update(app.config) - celery.autodiscover_tasks(packages=get_scripts_packages()) - return celery +app = Celery(__name__) +app.config_from_object('fractalis.config') +# app.config_from_envvar('FRACTALIS_CONFIG') +app.autodiscover_tasks(packages=get_scripts_packages()) diff --git a/fractalis/session.py b/fractalis/session.py index 25a8fe89d0c42ef2560e1b181b478834ec1c0f2d..302a6ede4884e892bdc0bc32ed8105b713c9bceb 100644 --- a/fractalis/session.py +++ b/fractalis/session.py @@ -25,7 +25,7 @@ class RedisSessionInterface(SecureCookieSessionInterface): """ def __init__(self, app_config): - self.redis = StrictRedis(host=app_config['REDIS_HOSTNAME'], + self.redis = StrictRedis(host=app_config['REDIS_HOST'], port=app_config['REDIS_PORT']) def open_session(self, app, request): @@ -55,6 +55,7 @@ class RedisSessionInterface(SecureCookieSessionInterface): expires=expiration_times['cookies'], httponly=True, domain=domain) + # TODO: change seconds to timedelta def get_expiration_times(self, app, session): """Get dictionary that contains redis session and cookie expiration times in the correct format. diff --git a/setup.py b/setup.py index 516c826f22d60db1b0dea0ae371b9679dae1cca3..7df6dd5f9769111cfbab4a4e940d79db844d0eec 100644 --- a/setup.py +++ b/setup.py @@ -8,6 +8,7 @@ setup( packages=find_packages(), install_requires=[ 'Flask', + 'Flask-WTF', 'celery[redis]', 'redis' ], diff --git a/tests/test_session.py b/tests/test_session.py index e0e0f5409798e23b8fbae811256f5fe583b511ff..a95a6345bf4cf3700aa7d669c36d5ed3956bbdf6 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -20,7 +20,7 @@ class TestSession(object): @pytest.fixture(scope='module') def redis(self, app): - redis = StrictRedis(host=app.config['REDIS_HOSTNAME'], + redis = StrictRedis(host=app.config['REDIS_HOST'], port=app.config['REDIS_PORT']) return redis