Skip to content
This repository has been archived by the owner on May 19, 2020. It is now read-only.

Lang url prefix #19

Open
wants to merge 2 commits 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 Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
git_update="git fetch origin master && git reset --hard FETCH_HEAD"

help:
return "Make tasks for deployment. Checkout the makefile content."
@echo "Make tasks for deployment. Checkout the makefile content."

deploy:
ssh epidaurus "cd ~/tci-online && " ${git_update}
Expand Down
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from core import app
from core.views import *
from core import views, templates # noqa: F401


# WSGI
Expand Down
13 changes: 13 additions & 0 deletions core/templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from flask import request
from tcii18n.template import flask_methods

from . import app


def get_translations_file():
supported_languages = app.config.get('TRANSLATION_FILES').keys()
lang = (request.accept_languages.best_match(supported_languages) or
supported_languages[0])
return app.config.get('TRANSLATION_FILES').get(lang)

flask_methods(app, get_translations_file)
37 changes: 34 additions & 3 deletions core/views.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
from flask import render_template, request, abort
from flask import render_template, request, abort, redirect, url_for, g
from tcidatabase.models import SurveyToken, Score
from tcidata import get_tci

from . import app


"""
The following 2 functions take care of automagically prepending the `lang_code`
into the url and reading from it.
See http://flask.pocoo.org/docs/0.12/patterns/urlprocessors/.
"""


@app.url_defaults
def add_language_code(endpoint, values):
if 'lang_code' in values or not g.lang_code:
return
if app.url_map.is_endpoint_expecting(endpoint, 'lang_code'):
values['lang_code'] = g.lang_code


@app.url_value_preprocessor
def pull_lang_code(endpoint, values):
g.lang_code = (values.pop('lang_code',
app.config.get('DEFAULT_LANGUAGE', 'en')))


@app.route('/')
def default_home():
return redirect(url_for('home'))


@app.route('/<string:token>')
def default_survey(token):
return redirect(url_for('survey', token=token))


@app.route('/<string(length=2):lang_code>')
def home():
return render_template('error.html'), 401


@app.route('/<string:token>')
@app.route('/<lang_code>/<string:token>')
def survey(token):
try:
token = SurveyToken.objects.get(key=token, usage_date=None)
Expand All @@ -25,7 +56,7 @@ def survey(token):
return render_template('survey.html', questions=questions, token=token)


@app.route('/end', methods=['post'])
@app.route('/<lang_code>/end', methods=['post'])
def end():
token = SurveyToken.objects.get(key=request.form.get('token'))
answers = [int(a) for a in request.form.get('answers').split(',')]
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-e git+ssh://bitbucket.org/anthropedia/tci-database.git#egg=tci-database
-e git+ssh://bitbucket.org/anthropedia/tci-data.git#egg=tci-data
-e git+https://github.com/anthropedia/tci-i18n.git#egg=tci-i18n

Flask==0.12
7 changes: 7 additions & 0 deletions settings.example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@
'host': 'localhost',
'port': 27017,
}

TRANSLATION_FILES = {
'en': '/path/to/my/transfile.en.csv',
'fr': '/path/to/my/transfile.fr.csv',
'sv': '/path/to/my/transfile.sv.csv',
}
DEFAULT_LANGUAGE = 'en'
2 changes: 1 addition & 1 deletion templates/error.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends 'layout.html' %}
{% block main %}
<section>
<h2>Oops, the test didn't load.</h2>
<h2>{{ trans("Oops, the test didn't load.") }}</h2>
<p>There are a couple of reasons this might happen.</p>
<p>Please double-check that you are using a new valid link and not an old test.</p>
<p>Otherwise, you might have an invalid token. If you are sure you have the good link,
Expand Down