eskp.py
author Reimar Bauer <rb.proj AT googlemail DOT com>
Fri, 16 May 2014 10:18:42 +0200
changeset 266 1562f93c5c67
parent 265 69babac65a72
child 267 64c9f7ecbac4
permissions -rwxr-xr-x
image and link to the ESKP site changed
     1 # -*- coding: utf-8 -*-
     2 
     3 import os
     4 import codecs
     5 
     6 from docutils.core import publish_parts
     7 from flask import Flask, Response
     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 def get_content(filename, overrides=None):
    26     content = u""
    27     filename = os.path.join(ESKP_PATH, filename)
    28     if os.path.isfile(filename):
    29         with codecs.open(filename, 'r', 'utf-8') as f:
    30             rst_data = f.read()
    31         f.close()
    32         content = publish_parts(rst_data, writer_name='html', settings_overrides=overrides)['html_body']
    33     return content
    34 
    35 def get_topmenue():
    36     menue = [
    37              ('/ozoneloss', _(u'Ozoneloss')),
    38              ('/eskp', _(u'ESKP')),
    39              ('/iek-7', _(u'IEK-7')),
    40             ]
    41     return menue
    42 
    43 app.jinja_env.globals.update(get_topmenue=get_topmenue)
    44 
    45 def get_o3lossclams_dates():
    46     menue = [('/ozoneloss/clams/2012', _(u'2012')),
    47              ('/ozoneloss/clams/2011', _(u'2011')),
    48              ('/ozoneloss/clams/2010', _(u'2010')),
    49              ]
    50     return menue
    51 
    52 def get_vpsc_dates():
    53     menue = [('/ozoneloss/vpsc/2014', _(u'2014')),
    54              ('/ozoneloss/vpsc/2013', _(u'2013')),
    55              ('/ozoneloss/vpsc/2012', _(u'2012')),
    56              ('/ozoneloss/vpsc/2011', _(u'2011')),
    57              ('/ozoneloss/vpsc/2010', _(u'2010')),
    58              ]
    59     return menue
    60 
    61 app.jinja_env.globals.update(get_o3lossclams_dates=get_o3lossclams_dates)
    62 app.jinja_env.globals.update(get_vpsc_dates=get_vpsc_dates)
    63 
    64 def author_info(name, filename):
    65     content = get_content(filename)
    66     html = render_template("/author_info.html", act="author", title=name, content=content, exit=_(u"Close"))
    67     return html
    68 
    69 
    70 @babel.localeselector
    71 def get_locale():
    72     """ToDo: if translation is completed, switch to en """
    73     return LANGUAGE_SELECTED or request.accept_languages.best_match(LANGUAGES.keys()) or 'de'
    74 
    75 
    76 @app.route("/")
    77 @app.route("/index")
    78 def index():
    79     return render_template("/index.html",
    80                            eskp_info=_(u'About ESKP'),
    81                            )
    82 
    83 
    84 @app.route('/ozoneloss/clams/<year>')
    85 def ozoneloss_clams_year(year):
    86     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
    87     content = get_content(filename)
    88     return render_template("/ozoneloss_clams.html", act="ozoneloss/clams/%s" % year, content=content, year=year)
    89 
    90 
    91 @app.route('/ozoneloss/vpsc/<year>')
    92 def ozoneloss_vspc_year(year):
    93     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
    94     content = get_content(filename)
    95     filename = os.path.join("templates", get_locale(), "rst", "explanation_vpsc.rst")
    96     explanation = get_content(filename)
    97 
    98     return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc/%s" % year, content=content,
    99                            content_explanation=explanation, year=year)
   100 
   101 @app.route('/de')
   102 def de():
   103     global LANGUAGE_SELECTED
   104     LANGUAGE_SELECTED = "de"
   105     return render_template("/index.html",
   106                            eskp_info=_(u'About ESKP'),
   107                            )
   108 
   109 @app.route('/en')
   110 def en():
   111     global LANGUAGE_SELECTED
   112     LANGUAGE_SELECTED = "en"
   113     return render_template("/index.html",
   114                            eskp_info=_(u'About ESKP'),
   115                            )
   116 
   117 @app.route("/eskp")
   118 def eskp():
   119     filename = os.path.join("templates", get_locale(), "rst", "eskp.rst")
   120     content = get_content(filename)
   121     return render_template("/eskp.html", act="eskp", content=content)
   122 
   123 
   124 @app.route("/ozoneloss")
   125 def ozoneloss():
   126     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
   127     content = get_content(filename)
   128     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_author.rst")
   129     author = author_info(_(u"Ozoneloss"), filename)
   130     return render_template("/ozoneloss.html", act="ozoneloss", content=content,
   131                            author=author, contact = u"Dr. Jens-Uwe Grooß", publications=u"Publications" )
   132 
   133 @app.route("/ozoneloss/clams")
   134 def ozoneloss_clams():
   135     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
   136     content = get_content(filename)
   137     return render_template("/ozoneloss_clams.html", act="ozoneloss/clams", content=content)
   138 
   139 @app.route("/ozoneloss/vpsc")
   140 def ozoneloss_vspc():
   141     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
   142     content = get_content(filename)
   143     return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc", content=content)
   144 
   145 @app.route("/iek-7")
   146 def institute():
   147     filename = os.path.join("templates", get_locale(), "rst", "iek-7.rst")
   148     content = get_content(filename)
   149     filename = os.path.join("templates", get_locale(), "rst", "iek-7_author.rst")
   150     author = author_info(_(u"IEK-7"), filename)
   151     return render_template("/iek-7.html", act="iek-7", content=content,
   152                            author=author, contact = u"Sandra Stein")
   153 
   154 
   155 @app.route("/imprint")
   156 def imprint():
   157     filename = os.path.join("templates", get_locale(), "rst", "imprint.rst")
   158     content = get_content(filename)
   159     return render_template("/content.html", act="imprint", content=content)
   160 
   161 
   162 
   163 
   164 
   165 @app.errorhandler(404)
   166 def page_not_found(e):
   167     msg = _(u"Url: %(url)s not found", url=request.url)
   168     info = _(u"This information is not available!")
   169     return render_template("404.html", msg=msg, info=info)
   170 
   171 if __name__ == "__main__":
   172     app.run(host='localhost', port=5014, debug=True)
Impressum Datenschutzerklärung