aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilo Casagrande <milo.casagrande@linaro.org>2014-12-17 12:36:22 +0100
committerMilo Casagrande <milo.casagrande@linaro.org>2014-12-17 12:37:18 +0100
commite6435b5db62a21dfabec37bd81b2f05ae4929767 (patch)
tree1dc91a6f15dcac19bbce2b384280576990181163
parentb308f3a301145717b262e87477034aa860a98b9c (diff)
Rename app module into __init__.
* Rename module into normal __init__ name. * Add flask-cache initialization. * Define default settings for the cache. Change-Id: I91c3cf66dd783638df8451a21364335db05e19d7
-rw-r--r--app/dashboard/__init__.py208
-rw-r--r--app/dashboard/app.py208
-rw-r--r--app/dashboard/default_settings.py10
-rwxr-xr-xapp/server.py2
4 files changed, 217 insertions, 211 deletions
diff --git a/app/dashboard/__init__.py b/app/dashboard/__init__.py
index e527629..df75aa1 100644
--- a/app/dashboard/__init__.py
+++ b/app/dashboard/__init__.py
@@ -1,2 +1,210 @@
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program 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 Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+import os
+import random
+
+from flask import (
+ Flask,
+ Markup,
+ abort,
+ render_template,
+ request,
+)
+from flask_wtf.csrf import (
+ CsrfProtect,
+ generate_csrf,
+ validate_csrf,
+)
+from flask.ext.cache import Cache
+
__version__ = "2014.12.3"
__versionfull__ = __version__
+
+CRSF_TOKEN_H = "X-Csrftoken"
+
+DEFAULT_CONFIG_FILE = "/etc/linaro/kernelci-frontend.cfg"
+# Name of the environment variable that will be lookep up for app
+# configuration parameters.
+APP_ENVVAR = "FLASK_SETTINGS"
+
+
+def generate_csrf_token():
+ """Custom function for tokens generation.
+
+ It returns a CSRF token with a random time limit between 30 and
+ 120 seconds.
+
+ :return A random CSRF token.
+ """
+ return generate_csrf(time_limit=random.randint(30, 120))
+
+
+app = Flask("kernelci-frontend")
+
+app.root_path = os.path.abspath(os.path.dirname(__file__))
+
+app.config.from_object("dashboard.default_settings")
+if os.path.isfile(DEFAULT_CONFIG_FILE):
+ app.config.from_pyfile(DEFAULT_CONFIG_FILE)
+
+if os.environ.get(APP_ENVVAR):
+ app.config.from_envvar(APP_ENVVAR)
+
+# Save the function.
+app_conf_get = app.config.get
+
+app.cache = Cache(app)
+app.csrf = CsrfProtect(app)
+
+# Use the custom CSRF token generation.
+app.jinja_env.globals["csrf_token_r"] = generate_csrf_token
+
+# Initialize the app routes, config and other necessary stuff.
+# The app context here is needed since we are using variables defined in the
+# config files and we need to access them.
+with app.app_context():
+ import utils.backend as backend
+ import dashboard.utils.route as route
+
+ route.init()
+
+
+@app.context_processor
+def inject_variables():
+ return dict(
+ analytics=app_conf_get("GOOGLE_ANALYTICS_ID"),
+ is_mobile=backend.is_mobile_browser(request),
+ is_old_browser=backend.is_old_browser(request),
+ server_date=backend.today_date(),
+ front_version=__version__
+ )
+
+
+@app.errorhandler(404)
+def page_not_found(e):
+ path = os.path.join(app.root_path, "static", "html", "404-content.html")
+ page_content = ""
+
+ with open(path) as content_file:
+ page_content = Markup(content_file.read())
+
+ return render_template("404.html", page_content=page_content), 404
+
+
+@app.errorhandler(500)
+def internal_server_error(e):
+ path = os.path.join(app.root_path, "static", "html", "500-content.html")
+ page_content = ""
+
+ with open(path) as content_file:
+ page_content = Markup(content_file.read())
+
+ return render_template("500.html", page_content=page_content), 500
+
+
+@app.errorhandler(400)
+def bad_request_error(e):
+ path = os.path.join(app.root_path, "static", "html", "400-content.html")
+ page_content = ""
+
+ with open(path) as content_file:
+ page_content = Markup(content_file.read())
+
+ return render_template("400.html", page_content=page_content), 400
+
+
+@app.route("/static/js/<path:path>")
+def static_js_proxy(path):
+ return app.send_static_file(os.path.join("js", path))
+
+
+@app.route("/static/html/<path:path>")
+def static_html_proxy(path):
+ return app.send_static_file(os.path.join("html", path))
+
+
+@app.route("/_ajax/job")
+def ajax_job():
+ if validate_csrf(request.headers.get(CRSF_TOKEN_H, None)):
+ return backend.ajax_get(request, app_conf_get("JOB_API_ENDPOINT"))
+ else:
+ abort(400)
+
+
+@app.route("/_ajax/defconf")
+def ajax_defconf():
+ if validate_csrf(request.headers.get(CRSF_TOKEN_H, None)):
+ return backend.ajax_get(
+ request, app_conf_get("DEFCONFIG_API_ENDPOINT"))
+ else:
+ abort(400)
+
+
+@app.route("/_ajax/boot")
+def ajax_boot():
+ if validate_csrf(request.headers.get(CRSF_TOKEN_H, None)):
+ return backend.ajax_get(request, app_conf_get("BOOT_API_ENDPOINT"))
+ else:
+ abort(400)
+
+
+@app.route("/_ajax/count")
+@app.route("/_ajax/count/<string:collection>")
+def ajax_count(collection=None):
+ if validate_csrf(request.headers.get(CRSF_TOKEN_H, None)):
+ return backend.ajax_count_get(
+ request, app_conf_get("COUNT_API_ENDPOINT"),
+ collection
+ )
+ else:
+ abort(400)
+
+
+@app.route("/_ajax/batch", methods=("POST", "OPTIONS"))
+def ajax_batch():
+ if validate_csrf(request.headers.get(CRSF_TOKEN_H, None)):
+ if request.data:
+ return backend.ajax_batch_post(
+ request, app_conf_get("BATCH_API_ENDPOINT")
+ )
+ else:
+ abort(400)
+ else:
+ abort(400)
+
+
+@app.route("/_ajax/bisect/<string:collection>/<string:doc_id>")
+def ajax_bisect_call(collection=None, doc_id=None):
+ if validate_csrf(request.headers.get(CRSF_TOKEN_H, None)):
+ if all([collection, doc_id]):
+ return backend.ajax_bisect(
+ request, collection, doc_id,
+ app_conf_get("BISECT_API_ENDPOINT")
+ )
+ else:
+ abort(400)
+ else:
+ abort(400)
+
+
+@app.route("/_ajax/version", methods=["GET"])
+def ajax_version():
+ if validate_csrf(request.headers.get(CRSF_TOKEN_H, None)):
+ # Cache the version for two days, hard for it to change that often.
+ return backend.ajax_get(
+ request,
+ app_conf_get("VERSION_API_ENDPOINT"),
+ timeout=60*60*24*2)
+ else:
+ abort(400)
diff --git a/app/dashboard/app.py b/app/dashboard/app.py
deleted file mode 100644
index 449356f..0000000
--- a/app/dashboard/app.py
+++ /dev/null
@@ -1,208 +0,0 @@
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as
-# published by the Free Software Foundation, either version 3 of the
-# License, or (at your option) any later version.
-#
-# This program 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 Affero General Public License for more details.
-#
-# You should have received a copy of the GNU Affero General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-import os
-import random
-
-from flask import (
- Flask,
- Markup,
- abort,
- render_template,
- request,
-)
-from flask_wtf.csrf import (
- CsrfProtect,
- generate_csrf,
- validate_csrf,
-)
-
-from dashboard import __version__
-
-from utils.backend import (
- ajax_batch_post,
- ajax_bisect,
- ajax_count_get,
- ajax_get,
- is_mobile_browser,
- is_old_browser,
- today_date,
-)
-
-CRSF_TOKEN_H = "X-Csrftoken"
-
-
-def generate_csrf_token():
- """Custom function for tokens generation.
-
- It returns a CSRF token with a random time limit between 25 and
- 90 seconds.
-
- :return A random CSRF token.
- """
- return generate_csrf(time_limit=random.randint(25, 90))
-
-
-DEFAULT_CONFIG_FILE = "/etc/linaro/kernelci-frontend.cfg"
-
-# Name of the environment variable that will be lookep up for app
-# configuration parameters.
-APP_ENVVAR = "FLASK_SETTINGS"
-
-app = Flask("kernelci-frontend")
-
-app.root_path = os.path.abspath(os.path.dirname(__file__))
-
-app.config.from_object("dashboard.default_settings")
-if os.path.isfile(DEFAULT_CONFIG_FILE):
- app.config.from_pyfile(DEFAULT_CONFIG_FILE)
-
-if os.environ.get(APP_ENVVAR):
- app.config.from_envvar(APP_ENVVAR)
-
-CsrfProtect(app)
-
-# Use the custom CSRF token generation.
-app.jinja_env.globals["csrf_token_r"] = generate_csrf_token
-
-# Initialize the app routes.
-# The app context here is needed since we are using variables defined in the
-# config files and we need to access them.
-with app.app_context():
- import dashboard.utils.route as route
- route.init(app)
-
-
-@app.context_processor
-def inject_variables():
- return dict(
- analytics=app.config.get("GOOGLE_ANALYTICS_ID"),
- is_mobile=is_mobile_browser(request),
- is_old_browser=is_old_browser(request),
- server_date=today_date(),
- front_version=__version__
- )
-
-
-@app.errorhandler(404)
-def page_not_found(e):
- path = os.path.join(app.root_path, "static", "html", "404-content.html")
- page_content = ""
-
- with open(path) as content_file:
- page_content = Markup(content_file.read())
-
- return render_template("404.html", page_content=page_content), 404
-
-
-@app.errorhandler(500)
-def internal_server_error(e):
- path = os.path.join(app.root_path, "static", "html", "500-content.html")
- page_content = ""
-
- with open(path) as content_file:
- page_content = Markup(content_file.read())
-
- return render_template("500.html", page_content=page_content), 500
-
-
-@app.errorhandler(400)
-def bad_request_error(e):
- path = os.path.join(app.root_path, "static", "html", "400-content.html")
- page_content = ""
-
- with open(path) as content_file:
- page_content = Markup(content_file.read())
-
- return render_template("400.html", page_content=page_content), 400
-
-
-@app.route("/static/js/<path:path>")
-def static_js_proxy(path):
- return app.send_static_file(os.path.join("js", path))
-
-
-@app.route("/static/html/<path:path>")
-def static_html_proxy(path):
- return app.send_static_file(os.path.join("html", path))
-
-
-@app.route("/_ajax/job")
-def ajax_job():
- if validate_csrf(request.headers.get(CRSF_TOKEN_H, None)):
- return ajax_get(request, app.config.get("JOB_API_ENDPOINT"))
- else:
- abort(400)
-
-
-@app.route("/_ajax/defconf")
-def ajax_defconf():
- if validate_csrf(request.headers.get(CRSF_TOKEN_H, None)):
- return ajax_get(request, app.config.get("DEFCONFIG_API_ENDPOINT"))
- else:
- abort(400)
-
-
-@app.route("/_ajax/boot")
-def ajax_boot():
- if validate_csrf(request.headers.get(CRSF_TOKEN_H, None)):
- return ajax_get(request, app.config.get("BOOT_API_ENDPOINT"))
- else:
- abort(400)
-
-
-@app.route("/_ajax/count")
-@app.route("/_ajax/count/<string:collection>")
-def ajax_count(collection=None):
- if validate_csrf(request.headers.get(CRSF_TOKEN_H, None)):
- return ajax_count_get(
- request, app.config.get("COUNT_API_ENDPOINT"),
- collection
- )
- else:
- abort(400)
-
-
-@app.route("/_ajax/batch", methods=("POST", "OPTIONS"))
-def ajax_batch():
- if validate_csrf(request.headers.get(CRSF_TOKEN_H, None)):
- if request.data:
- return ajax_batch_post(
- request, app.config.get("BATCH_API_ENDPOINT")
- )
- else:
- abort(400)
- else:
- abort(400)
-
-
-@app.route("/_ajax/bisect/<string:collection>/<string:doc_id>")
-def ajax_bisect_call(collection=None, doc_id=None):
- if validate_csrf(request.headers.get(CRSF_TOKEN_H, None)):
- if all([collection, doc_id]):
- return ajax_bisect(
- request, collection, doc_id,
- app.config.get("BISECT_API_ENDPOINT")
- )
- else:
- abort(400)
- else:
- abort(400)
-
-
-@app.route("/_ajax/version", methods=["GET"])
-def ajax_version():
- if validate_csrf(request.headers.get(CRSF_TOKEN_H, None)):
- return ajax_get(request, app.config.get("VERSION_API_ENDPOINT"))
- else:
- abort(400)
diff --git a/app/dashboard/default_settings.py b/app/dashboard/default_settings.py
index eda35e2..b72082a 100644
--- a/app/dashboard/default_settings.py
+++ b/app/dashboard/default_settings.py
@@ -1,5 +1,3 @@
-# Copyright (C) 2014 Linaro Ltd.
-#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
@@ -76,6 +74,14 @@ DATE_RANGE = 5
# Google Analytics code.
GOOGLE_ANALYTICS_ID = None
+# Redis cache values: need to override USE_CACHE to make use of the cache.
+CACHE_TYPE = 'simple'
+CACHE_KEY_PREFIX = 'kernelcifrontend|'
+CACHE_REDIS_HOST = 'localhost'
+CACHE_REDIS_PORT = 6379
+CACHE_REDIS_DB = 0
+CACHE_DEFAULT_TIMEOUT = 420
+
DEBUG = True
TESTING = DEBUG
THREADED = False
diff --git a/app/server.py b/app/server.py
index 5b3e2cc..d359574 100755
--- a/app/server.py
+++ b/app/server.py
@@ -13,7 +13,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-from dashboard.app import app
+from dashboard import app
if __name__ == "__main__":