eskp.py
author Reimar Bauer <rb.proj AT googlemail DOT com>
Mon, 05 May 2014 14:37:13 +0200
changeset 239 e1e0ddbc8c98
parent 238 c89b73e1a53a
child 241 9bfd98868419
permissions -rwxr-xr-x
imprint text added
     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 from sayings import get_saying
    14 
    15 
    16 LANGUAGE_SELECTED = "de"
    17 #ToDo after engelish is implemented set LANGUAGE_SELECTED = None
    18 
    19 app = Flask(__name__)
    20 babel = Babel(app)
    21 
    22 app.config['BABEL_DEFAULT_LOCALE'] = 'de'
    23 
    24 
    25 def get_content(filename, overrides=None):
    26     content = u""
    27     if os.path.isfile(filename):
    28         with codecs.open(filename, 'r', 'utf-8') as f:
    29             rst_data = f.read()
    30         f.close()
    31         content = publish_parts(rst_data, writer_name='html', settings_overrides=overrides)['html_body']
    32     return content
    33 
    34 def get_topmenue():
    35     menue = [
    36              ('/ozoneloss', _(u'Ozoneloss')),
    37              ('/eskp', _(u'ESKP')),
    38              ('/iek-7', _(u'IEK-7')),
    39             ]
    40     return menue
    41 
    42 app.jinja_env.globals.update(get_topmenue=get_topmenue)
    43 
    44 
    45 @babel.localeselector
    46 def get_locale():
    47     """ToDo: if translation is completed, switch to en """
    48     return LANGUAGE_SELECTED or request.accept_languages.best_match(LANGUAGES.keys()) or 'de'
    49 
    50 
    51 @app.route("/")
    52 @app.route("/index")
    53 def index():
    54     saying, author = get_saying()
    55     return render_template("/index.html",
    56                            saying=saying,
    57                            author=author,
    58                            eskp_info=_(u'About ESKP'),
    59                            )
    60 
    61 @app.route('/de')
    62 def de():
    63     global LANGUAGE_SELECTED
    64     LANGUAGE_SELECTED = "de"
    65     saying, author = get_saying()
    66     return render_template("/index.html",
    67                            saying=saying,
    68                            author=author,
    69                            eskp_info=_(u'About ESKP'),
    70                            )
    71 
    72 @app.route('/en')
    73 def en():
    74     saying, author = get_saying()
    75     global LANGUAGE_SELECTED
    76     LANGUAGE_SELECTED = "en"
    77     return render_template("/index.html",
    78                            saying=saying,
    79                            author=author,
    80                            eskp_info=_(u'About ESKP'),
    81                            )
    82 
    83 @app.route("/eskp")
    84 def eskp():
    85     filename = os.path.join("templates", get_locale(), "rst", "eskp.rst")
    86     content = get_content(filename)
    87     return render_template("/content.html", act="eskp", content=content)
    88 
    89 @app.route("/ozoneloss")
    90 def task():
    91     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
    92     content = get_content(filename)
    93     print content
    94     print filename
    95     return render_template("/content.html", act="ozoneloss", content=content)
    96 
    97 @app.route("/iek-7")
    98 def submission():
    99     filename = os.path.join("templates", get_locale(), "rst", "iek-7.rst")
   100     content = get_content(filename)
   101     return render_template("/content.html", act="submission", content=content)
   102 
   103 
   104 @app.route("/imprint")
   105 def imprint():
   106     filename = os.path.join("templates", get_locale(), "rst", "imprint.rst")
   107     content = get_content(filename)
   108     return render_template("/content.html", act="imprint", content=content)
   109 
   110 
   111 
   112 @app.errorhandler(404)
   113 def page_not_found(e):
   114     msg = _(u"Url: %(url)s not found", url=request.url)
   115     info = _(u"This information is not available!")
   116     return render_template("404.html", msg=msg, info=info)
   117 
   118 if __name__ == "__main__":
   119     app.run(host='localhost', port=5014, debug=True)
Impressum Datenschutzerklärung