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