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

Add the load API #4

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
flask==0.12.2
flask==0.12.2
1 change: 1 addition & 0 deletions sinas/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ def create_app():
from sinas.views import api
app.register_blueprint(api)

app.secret_key = 'aslgkjawlegj92039jr23rlkj9'
return app
17 changes: 1 addition & 16 deletions sinas/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')
Expand All @@ -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/<path:path>')
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
42 changes: 42 additions & 0 deletions sinas/views/api.py
Original file line number Diff line number Diff line change
@@ -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/<path:path>')
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/<path:path>')
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)