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 dead simple API #34

Merged
merged 3 commits into from
Aug 10, 2018
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/_build/
# Elsa output
_build

### OSX
*.DS_Store
Expand Down
3 changes: 3 additions & 0 deletions pyvecorg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ def run(self, *args, **kwargs):


app = Flask('pyvecorg')
app.config['JSON_AS_ASCII'] = False


from pyvecorg import views
from pyvecorg import templating


__all__ = ['app', 'views', 'templating']
3 changes: 3 additions & 0 deletions pyvecorg/data/meta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@ downloads:
resources:
cs: grafika
en: resources
json_data:
cs: tato stránka jako JSON
en: this page as JSON
3 changes: 3 additions & 0 deletions pyvecorg/data/meta_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
},
"resources": {
"$ref": "definitions/translated_text_schema.json#"
},
"json_data": {
"$ref": "definitions/translated_text_schema.json#"
}
},
"additionalProperties": false
Expand Down
9 changes: 8 additions & 1 deletion pyvecorg/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<head>
<meta charset="utf-8">

<link rel="shortcut icon" type="image/x-icon" href="{{ url_for('static', filename='favicon.ico') }}">
<link rel="shortcut icon" type="image/vnd.microsoft.icon" href="{{ url_for('static', filename='favicon.ico') }}">
<link rel="alternate" type="application/json" href="{{ url_for('api', lang=lang) }}">

<meta property="og:image" content="{{ url_for('static', filename='img/cover.jpg', _external=True) }}">
<meta property="twitter:card" content="summary_large_image">
<meta property="twitter:site" content="@pyvec">
Expand Down Expand Up @@ -240,8 +242,13 @@ <h3>{{ meta.downloads.heading }}</h3>
<i class="fa fa-file-text-o" aria-hidden="true"></i>
<a href="{{ url_for('static', filename='charter/stanovy.rtf') }}">{{ meta.downloads.charter }}.rtf</a>

<br>

<i class="fa fa-file-image-o" aria-hidden="true"></i>
<a href="https://github.com/pyvec/resources">{{ meta.downloads.resources }}</a>

<i class="fa fa-file-code-o" aria-hidden="true"></i>
<a href="{{ url_for('api', lang=lang) }}">{{ meta.downloads.json_data }}</a>
</p>
</div>
<div class="w-100 d-block d-md-none"></div>
Expand Down
19 changes: 11 additions & 8 deletions pyvecorg/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime

from flask import (request, render_template, redirect, url_for,
send_from_directory)
send_from_directory, jsonify)

from pyvecorg import app
from pyvecorg.data import load_data, select_language
Expand All @@ -11,23 +11,26 @@
data = load_data()


@app.route('/')
def index_redirect():
if request.accept_languages.best_match(['en', 'cs', 'sk']) == 'en':
return redirect(url_for('index', lang='en'))
return redirect(url_for('index', lang='cs'))


@app.route('/favicon.ico')
def favicon():
static = os.path.join(app.root_path, 'static')
return send_from_directory(static, 'favicon.ico',
mimetype='image/vnd.microsoft.icon')


@app.route('/')
def index_redirect():
return redirect(url_for('index', lang='cs'))


@app.route('/<lang>/')
def index(lang):
context = select_language(data, lang)
context['lang'] = lang
context['now'] = datetime.now()
return render_template('index.html', **context)


@app.route('/<lang>/api.json')
def api(lang):
return jsonify(select_language(data, lang))