eskp.py
author Reimar Bauer <rb.proj AT googlemail DOT com>
Mon, 12 May 2014 13:21:18 +0200
changeset 258 ec6fbfdc129c
parent 257 e953d3dc48b7
child 264 bdf11bb4cb98
permissions -rwxr-xr-x
removed pycharm idea files
     1 # -*- coding: utf-8 -*-
     2 
     3 import os
     4 import codecs
     5 
     6 from docutils.core import publish_parts
     7 from flask import Flask
     8 from flask import render_template
     9 from flask import request
    10 from flask.ext.babel import gettext as _
    11 from flask.ext.babel import Babel
    12 from config import LANGUAGES
    13 
    14 LANGUAGE_SELECTED = "de"
    15 #ToDo after engelish is implemented set LANGUAGE_SELECTED = None
    16 
    17 # We need the path of this file to find templates to translate
    18 ESKP_PATH = os.path.dirname(os.path.abspath(__file__))
    19 
    20 app = Flask(__name__)
    21 babel = Babel(app)
    22 
    23 app.config['BABEL_DEFAULT_LOCALE'] = 'de'
    24 
    25 
    26 def get_content(filename, overrides=None):
    27     content = u""
    28     filename = os.path.join(ESKP_PATH, filename)
    29     if os.path.isfile(filename):
    30         with codecs.open(filename, 'r', 'utf-8') as f:
    31             rst_data = f.read()
    32         f.close()
    33         content = publish_parts(rst_data, writer_name='html', settings_overrides=overrides)['html_body']
    34     return content
    35 
    36 def get_topmenue():
    37     menue = [
    38              ('/ozoneloss', _(u'Ozoneloss')),
    39              ('/eskp', _(u'ESKP')),
    40              ('/iek-7', _(u'IEK-7')),
    41             ]
    42     return menue
    43 
    44 app.jinja_env.globals.update(get_topmenue=get_topmenue)
    45 
    46 def get_o3lossclams_dates():
    47     menue = [('/ozoneloss/clams/2012', _(u'2012')),
    48              ('/ozoneloss/clams/2011', _(u'2011')),
    49              ('/ozoneloss/clams/2010', _(u'2010')),
    50              ]
    51     return menue
    52 
    53 def get_vpsc_dates():
    54     menue = [('/ozoneloss/vpsc/2014', _(u'2014')),
    55              ('/ozoneloss/vpsc/2013', _(u'2013')),
    56              ('/ozoneloss/vpsc/2012', _(u'2012')),
    57              ('/ozoneloss/vpsc/2011', _(u'2011')),
    58              ('/ozoneloss/vpsc/2010', _(u'2010')),
    59              ]
    60     return menue
    61 
    62 app.jinja_env.globals.update(get_o3lossclams_dates=get_o3lossclams_dates)
    63 app.jinja_env.globals.update(get_vpsc_dates=get_vpsc_dates)
    64 
    65 @babel.localeselector
    66 def get_locale():
    67     """ToDo: if translation is completed, switch to en """
    68     return LANGUAGE_SELECTED or request.accept_languages.best_match(LANGUAGES.keys()) or 'de'
    69 
    70 
    71 @app.route("/")
    72 @app.route("/index")
    73 def index():
    74     return render_template("/index.html",
    75                            eskp_info=_(u'About ESKP'),
    76                            )
    77 
    78 
    79 @app.route('/ozoneloss/clams/<year>')
    80 def ozoneloss_clams_year(year):
    81     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
    82     content = get_content(filename)
    83     return render_template("/ozoneloss_clams.html", act="ozoneloss/clams/%s" % year, content=content, year=year)
    84 
    85 
    86 @app.route('/ozoneloss/vpsc/<year>')
    87 def ozoneloss_vspc_year(year):
    88     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
    89     content = get_content(filename)
    90     filename = os.path.join("templates", get_locale(), "rst", "explanation_vpsc.rst")
    91     explanation = get_content(filename)
    92 
    93     return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc/%s" % year, content=content,
    94                            content_explanation=explanation, year=year)
    95 
    96 @app.route('/de')
    97 def de():
    98     global LANGUAGE_SELECTED
    99     LANGUAGE_SELECTED = "de"
   100     saying, author = get_saying()
   101     return render_template("/index.html",
   102                            saying=saying,
   103                            author=author,
   104                            eskp_info=_(u'About ESKP'),
   105                            )
   106 
   107 @app.route('/en')
   108 def en():
   109     saying, author = get_saying()
   110     global LANGUAGE_SELECTED
   111     LANGUAGE_SELECTED = "en"
   112     return render_template("/index.html",
   113                            saying=saying,
   114                            author=author,
   115                            eskp_info=_(u'About ESKP'),
   116                            )
   117 
   118 @app.route("/eskp")
   119 def eskp():
   120     filename = os.path.join("templates", get_locale(), "rst", "eskp.rst")
   121     content = get_content(filename)
   122     return render_template("/eskp.html", act="eskp", content=content)
   123 
   124 
   125 @app.route("/ozoneloss")
   126 def ozoneloss():
   127     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
   128     content = get_content(filename)
   129     return render_template("/ozoneloss.html", act="ozoneloss", content=content)
   130 
   131 @app.route("/ozoneloss/clams")
   132 def ozoneloss_clams():
   133     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
   134     content = get_content(filename)
   135     return render_template("/ozoneloss_clams.html", act="ozoneloss/clams", content=content)
   136 
   137 @app.route("/ozoneloss/vpsc")
   138 def ozoneloss_vspc():
   139     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
   140     content = get_content(filename)
   141     return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc", content=content)
   142 
   143 @app.route("/iek-7")
   144 def institute():
   145     filename = os.path.join("templates", get_locale(), "rst", "iek-7.rst")
   146     content = get_content(filename)
   147     return render_template("/iek-7.html", act="iek-7", content=content)
   148 
   149 
   150 @app.route("/imprint")
   151 def imprint():
   152     filename = os.path.join("templates", get_locale(), "rst", "imprint.rst")
   153     content = get_content(filename)
   154     return render_template("/content.html", act="imprint", content=content)
   155 
   156 
   157 
   158 @app.errorhandler(404)
   159 def page_not_found(e):
   160     msg = _(u"Url: %(url)s not found", url=request.url)
   161     info = _(u"This information is not available!")
   162     return render_template("404.html", msg=msg, info=info)
   163 
   164 if __name__ == "__main__":
   165     app.run(host='localhost', port=5014, debug=True)
Impressum Datenschutzerklärung