From 1a5a156e83d56817dbefcd78e69c6eb48c1f4fa1 Mon Sep 17 00:00:00 2001 From: Jelle Besseling Date: Wed, 29 Nov 2017 21:52:55 +0100 Subject: [PATCH] Add the load API Should be executed when opening a project. --- requirements.txt | 2 +- sinas/app.py | 1 + sinas/views/__init__.py | 17 +---------------- sinas/views/api.py | 42 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 sinas/views/api.py diff --git a/requirements.txt b/requirements.txt index 69ca547..484d3c5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -flask==0.12.2 \ No newline at end of file +flask==0.12.2 diff --git a/sinas/app.py b/sinas/app.py index a17f61e..f803177 100644 --- a/sinas/app.py +++ b/sinas/app.py @@ -11,4 +11,5 @@ def create_app(): from sinas.views import api app.register_blueprint(api) + app.secret_key = 'aslgkjawlegj92039jr23rlkj9' return app diff --git a/sinas/views/__init__.py b/sinas/views/__init__.py index 64a21a5..6c87144 100644 --- a/sinas/views/__init__.py +++ b/sinas/views/__init__.py @@ -3,10 +3,9 @@ from flask import Blueprint, send_from_directory -from sinas.local import build_jekyll +from sinas.views.api import api frontend = Blueprint('frontend', __name__, template_folder='templates') -api = Blueprint('api', __name__) @frontend.route('/') @@ -18,17 +17,3 @@ def home(): def live(path, file): """Views the latest build of the project at `path`""" return send_from_directory(os.path.join(Path.home(), path, 'build'), file) - - -@api.route('/api/build/') -def build(path): - """Builds the project at `path` - - This function is intented to be used with an async REST call because the server waits until the build is done - before returning. """ - # TODO: use the builder that is configured for the project - # TODO: return better data - if build_jekyll('jekyll', path, os.path.join(path, 'build')) == 0: - return 'OK', 200 - else: - return 500 diff --git a/sinas/views/api.py b/sinas/views/api.py new file mode 100644 index 0000000..02f769e --- /dev/null +++ b/sinas/views/api.py @@ -0,0 +1,42 @@ +import os +from pathlib import Path + +import yaml +from flask import Blueprint, session, jsonify, make_response + +from sinas.local import build_jekyll + +api = Blueprint('api', __name__) + + +@api.route('/api/build/') +def build(path): + """Builds the project at `path` + + This function is intented to be used with an async REST call because the server waits until the build is done + before returning. """ + # TODO: use the builder that is configured for the project + # TODO: return better data + if build_jekyll('jekyll', path, os.path.join(path, 'build')) == 0: + return jsonify({'success': True}) + else: + return make_response(jsonify({'success': False}), 400) + + +@api.route('/api/load/') +def load(path): + """Loads the sinas.yaml and generator config into session memory""" + + try: + with open(os.path.join(Path.home(), path, 'sinas.yaml')) as f: + session[path] = {'config': yaml.load(f.read())} + if session[path]['config']['generator'] == 'jekyll': + with open(os.path.join(Path.home(), path, '_config.yml')) as f: + session[path]['generator_config'] = yaml.load(f.read()) + return jsonify({'success': True}) + except KeyError as e: + return make_response(jsonify({'success': False, 'error': 'malformed_config'}), 400) + except yaml.YAMLError as e: + return make_response(jsonify({'success': False, 'error': 'parse_error'}), 400) + except FileNotFoundError as e: + return make_response(jsonify({'success': False, 'error': 'not_found'}), 404)