rb@186: import os rb@186: import codecs rb@186: rb@186: from docutils.core import publish_parts peter@1: from flask import Flask peter@1: from flask import render_template peter@1: from flask import request rb@95: from flask.ext.babel import gettext as _ peter@1: from flask.ext.babel import Babel rb@95: from config import LANGUAGES peter@141: from sayings import get_saying rb@161: rb@161: rb@122: LANGUAGE_SELECTED = "de" rb@122: #ToDo after engelish is implemented set LANGUAGE_SELECTED = None rb@95: peter@1: app = Flask(__name__) peter@1: babel = Babel(app) peter@1: peter@145: app.config['BABEL_DEFAULT_LOCALE'] = 'de' peter@145: rb@186: rb@162: def get_content(filename): rb@162: content = u"" rb@162: if os.path.isfile(filename): rb@162: with codecs.open(filename, 'r', 'utf-8') as f: rb@162: rst_data = f.read() rb@162: f.close() rb@162: content = publish_parts(rst_data, writer_name='html')['html_body'] rb@162: return content rb@162: peter@142: def get_topmenue(): rb@186: menue = [('/competition', _(u'Competition')), rb@154: ('/task', _(u'Task')), rb@154: ('/submission', _(u'Submission')), rb@154: ('/coursematerial', _(u'Coursematerial')), rb@186: ] peter@145: return menue peter@142: peter@142: app.jinja_env.globals.update(get_topmenue=get_topmenue) peter@142: peter@142: peter@1: @babel.localeselector peter@1: def get_locale(): peter@109: """ToDo: if translation is completed, switch to en """ rb@118: return LANGUAGE_SELECTED or request.accept_languages.best_match(LANGUAGES.keys()) or 'de' peter@1: rb@100: peter@1: @app.route("/") rb@100: @app.route("/index") peter@1: def index(): peter@141: saying, author = get_saying() peter@160: return render_template("/index.html", rb@186: saying=saying, rb@186: author=author, rb@169: competition_info=_(u'About Competition'), rb@173: dates=_(u'Dates'), rb@173: impressions=_(u'Impressions')) peter@1: rb@118: @app.route('/de') rb@118: def de(): rb@118: global LANGUAGE_SELECTED rb@118: LANGUAGE_SELECTED = "de" peter@146: saying, author = get_saying() peter@160: return render_template("/index.html", rb@186: saying=saying, rb@186: author=author, rb@169: competition_info=_(u'About Competition'), rb@173: dates=_(u'Dates'), rb@173: impressions=_(u'Impressions')) rb@118: rb@118: @app.route('/en') rb@118: def en(): peter@146: saying, author = get_saying() rb@118: global LANGUAGE_SELECTED rb@118: LANGUAGE_SELECTED = "en" peter@160: return render_template("/index.html", rb@186: saying=saying, rb@186: author=author, rb@169: competition_info=_(u'About Competition'), rb@173: dates=_(u'Dates'), rb@173: impressions=_(u'Impressions')) rb@118: rb@100: @app.route("/competition") peter@1: def competition(): rb@162: filename = os.path.join("templates", get_locale(), "rst", "competition.rst") rb@162: content = get_content(filename) rb@170: return render_template("/content.html", act="competition", content=content) peter@1: rb@100: @app.route("/task") peter@1: def task(): rb@165: filename = os.path.join("templates", get_locale(), "rst", "task.rst") rb@165: content = get_content(filename) rb@170: return render_template("/content.html", act="task", content=content) peter@1: rb@100: @app.route("/submission") peter@1: def submission(): rb@167: filename = os.path.join("templates", get_locale(), "rst", "submission.rst") rb@167: content = get_content(filename) rb@170: return render_template("/content.html", act="submission", content=content) peter@1: rb@100: @app.route("/coursematerial") peter@1: def coursematerial(): rb@166: filename = os.path.join("templates", get_locale(), "rst", "coursematerial.rst") rb@166: content = get_content(filename) rb@170: return render_template("/content.html", act="coursematerial", content=content) peter@1: rb@100: @app.route("/imprint") peter@88: def imprint(): rb@163: filename = os.path.join("templates", get_locale(), "rst", "imprint.rst") rb@163: content = get_content(filename) rb@170: return render_template("/content.html", act="imprint", content=content) peter@88: rb@100: @app.route("/privacy") peter@88: def privacy(): rb@164: filename = os.path.join("templates", get_locale(), "rst", "privacy.rst") rb@164: content = get_content(filename) rb@170: return render_template("/content.html", act="privacy", content=content) rb@170: rb@170: @app.route("/dates") rb@170: def dates(): rb@170: filename = os.path.join("templates", get_locale(), "rst", "dates.rst") rb@170: content = get_content(filename) rb@170: return render_template("/content.html", rb@170: act="dates", content=content) peter@88: peter@123: @app.route("/competition/2013") peter@109: def competition_2013(): rb@180: filename = os.path.join("templates", get_locale(), "archive", "2013", "competitions", "rst", "2013.rst") rb@176: content = get_content(filename) rb@176: return render_template("/impressions_2013.html", rb@176: act="competition_2013", content=content) peter@109: peter@88: peter@1: @app.errorhandler(404) peter@1: def page_not_found(e): rb@157: msg = _(u"Url: %(url)s not found" , url=request.url) rb@157: info = _(u"This information is not available!") rb@157: return render_template("404.html", msg=msg, info=info) peter@1: peter@1: if __name__ == "__main__": peter@1: app.run(host='localhost', port=5014, debug=True)