rb@217: # -*- coding: utf-8 -*- rb@217: 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 rb@161: rb@122: LANGUAGE_SELECTED = "de" rb@122: #ToDo after engelish is implemented set LANGUAGE_SELECTED = None rb@95: rb@246: # We need the path of this file to find templates to translate rb@246: ESKP_PATH = os.path.dirname(os.path.abspath(__file__)) rb@246: 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@214: def get_content(filename, overrides=None): rb@162: content = u"" rb@246: filename = os.path.join(ESKP_PATH, filename) 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@214: content = publish_parts(rst_data, writer_name='html', settings_overrides=overrides)['html_body'] rb@162: return content rb@162: peter@142: def get_topmenue(): rb@238: menue = [ rb@238: ('/ozoneloss', _(u'Ozoneloss')), rb@238: ('/eskp', _(u'ESKP')), rb@238: ('/iek-7', _(u'IEK-7')), rb@186: ] peter@145: return menue peter@142: peter@142: app.jinja_env.globals.update(get_topmenue=get_topmenue) peter@142: j@250: def get_o3lossclams_dates(): j@250: menue = [('/ozoneloss/clams/2012', _(u'2012')), j@249: ('/ozoneloss/clams/2011', _(u'2011')), j@249: ('/ozoneloss/clams/2010', _(u'2010')), j@249: ] j@249: return menue j@249: j@249: def get_vpsc_dates(): j@249: menue = [('/ozoneloss/vpsc/2014', _(u'2014')), j@249: ('/ozoneloss/vpsc/2013', _(u'2013')), j@249: ('/ozoneloss/vpsc/2012', _(u'2012')), j@249: ('/ozoneloss/vpsc/2011', _(u'2011')), j@249: ('/ozoneloss/vpsc/2010', _(u'2010')), rb@241: ] rb@241: return menue rb@241: j@250: app.jinja_env.globals.update(get_o3lossclams_dates=get_o3lossclams_dates) j@249: app.jinja_env.globals.update(get_vpsc_dates=get_vpsc_dates) 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(): rb@213: return render_template("/index.html", rb@233: eskp_info=_(u'About ESKP'), rb@233: ) peter@1: rb@241: rb@257: @app.route('/ozoneloss/clams/') rb@257: def ozoneloss_clams_year(year): j@249: filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst") rb@241: content = get_content(filename) rb@257: return render_template("/ozoneloss_clams.html", act="ozoneloss/clams/%s" % year, content=content, year=year) rb@241: rb@241: rb@257: @app.route('/ozoneloss/vpsc/') rb@257: def ozoneloss_vspc_year(year): j@249: filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst") j@249: content = get_content(filename) j@250: filename = os.path.join("templates", get_locale(), "rst", "explanation_vpsc.rst") j@250: explanation = get_content(filename) j@250: rb@257: return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc/%s" % year, content=content, rb@257: content_explanation=explanation, year=year) rb@241: 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@233: eskp_info=_(u'About ESKP'), rb@233: ) 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@233: eskp_info=_(u'About ESKP'), rb@233: ) rb@118: rb@233: @app.route("/eskp") rb@233: def eskp(): rb@233: filename = os.path.join("templates", get_locale(), "rst", "eskp.rst") rb@162: content = get_content(filename) j@252: return render_template("/eskp.html", act="eskp", content=content) j@252: peter@1: rb@233: @app.route("/ozoneloss") rb@257: def ozoneloss(): rb@233: filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst") rb@165: content = get_content(filename) j@247: return render_template("/ozoneloss.html", act="ozoneloss", content=content) j@247: j@249: @app.route("/ozoneloss/clams") rb@257: def ozoneloss_clams(): j@249: filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst") j@247: content = get_content(filename) j@249: return render_template("/ozoneloss_clams.html", act="ozoneloss/clams", content=content) j@249: j@249: @app.route("/ozoneloss/vpsc") rb@257: def ozoneloss_vspc(): j@249: filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst") j@249: content = get_content(filename) j@249: return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc", content=content) peter@1: rb@233: @app.route("/iek-7") rb@257: def institute(): rb@233: filename = os.path.join("templates", get_locale(), "rst", "iek-7.rst") rb@167: content = get_content(filename) rb@257: return render_template("/iek-7.html", act="iek-7", content=content) peter@1: 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: peter@109: peter@88: peter@1: @app.errorhandler(404) peter@1: def page_not_found(e): rb@213: 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)