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