eskp.py
author Reimar Bauer <rb.proj AT googlemail DOT com>
Mon, 12 May 2014 13:15:42 +0200
changeset 257 e953d3dc48b7
parent 256 c9d4aa9ec58f
child 264 bdf11bb4cb98
permissions -rwxr-xr-x
eskp: simplified API
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
j@250
    46
def get_o3lossclams_dates():
j@250
    47
    menue = [('/ozoneloss/clams/2012', _(u'2012')),
j@249
    48
             ('/ozoneloss/clams/2011', _(u'2011')),
j@249
    49
             ('/ozoneloss/clams/2010', _(u'2010')),
j@249
    50
             ]
j@249
    51
    return menue
j@249
    52
j@249
    53
def get_vpsc_dates():
j@249
    54
    menue = [('/ozoneloss/vpsc/2014', _(u'2014')),
j@249
    55
             ('/ozoneloss/vpsc/2013', _(u'2013')),
j@249
    56
             ('/ozoneloss/vpsc/2012', _(u'2012')),
j@249
    57
             ('/ozoneloss/vpsc/2011', _(u'2011')),
j@249
    58
             ('/ozoneloss/vpsc/2010', _(u'2010')),
rb@241
    59
             ]
rb@241
    60
    return menue
rb@241
    61
j@250
    62
app.jinja_env.globals.update(get_o3lossclams_dates=get_o3lossclams_dates)
j@249
    63
app.jinja_env.globals.update(get_vpsc_dates=get_vpsc_dates)
peter@142
    64
peter@1
    65
@babel.localeselector
peter@1
    66
def get_locale():
peter@109
    67
    """ToDo: if translation is completed, switch to en """
rb@118
    68
    return LANGUAGE_SELECTED or request.accept_languages.best_match(LANGUAGES.keys()) or 'de'
peter@1
    69
rb@100
    70
peter@1
    71
@app.route("/")
rb@100
    72
@app.route("/index")
peter@1
    73
def index():
rb@213
    74
    return render_template("/index.html",
rb@233
    75
                           eskp_info=_(u'About ESKP'),
rb@233
    76
                           )
peter@1
    77
rb@241
    78
rb@257
    79
@app.route('/ozoneloss/clams/<year>')
rb@257
    80
def ozoneloss_clams_year(year):
j@249
    81
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
rb@241
    82
    content = get_content(filename)
rb@257
    83
    return render_template("/ozoneloss_clams.html", act="ozoneloss/clams/%s" % year, content=content, year=year)
rb@241
    84
rb@241
    85
rb@257
    86
@app.route('/ozoneloss/vpsc/<year>')
rb@257
    87
def ozoneloss_vspc_year(year):
j@249
    88
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
j@249
    89
    content = get_content(filename)
j@250
    90
    filename = os.path.join("templates", get_locale(), "rst", "explanation_vpsc.rst")
j@250
    91
    explanation = get_content(filename)
j@250
    92
rb@257
    93
    return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc/%s" % year, content=content,
rb@257
    94
                           content_explanation=explanation, year=year)
rb@241
    95
rb@118
    96
@app.route('/de')
rb@118
    97
def de():
rb@118
    98
    global LANGUAGE_SELECTED
rb@118
    99
    LANGUAGE_SELECTED = "de"
peter@146
   100
    saying, author = get_saying()
peter@160
   101
    return render_template("/index.html",
rb@186
   102
                           saying=saying,
rb@186
   103
                           author=author,
rb@233
   104
                           eskp_info=_(u'About ESKP'),
rb@233
   105
                           )
rb@118
   106
rb@118
   107
@app.route('/en')
rb@118
   108
def en():
peter@146
   109
    saying, author = get_saying()
rb@118
   110
    global LANGUAGE_SELECTED
rb@118
   111
    LANGUAGE_SELECTED = "en"
peter@160
   112
    return render_template("/index.html",
rb@186
   113
                           saying=saying,
rb@186
   114
                           author=author,
rb@233
   115
                           eskp_info=_(u'About ESKP'),
rb@233
   116
                           )
rb@118
   117
rb@233
   118
@app.route("/eskp")
rb@233
   119
def eskp():
rb@233
   120
    filename = os.path.join("templates", get_locale(), "rst", "eskp.rst")
rb@162
   121
    content = get_content(filename)
j@252
   122
    return render_template("/eskp.html", act="eskp", content=content)
j@252
   123
peter@1
   124
rb@233
   125
@app.route("/ozoneloss")
rb@257
   126
def ozoneloss():
rb@233
   127
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
rb@165
   128
    content = get_content(filename)
j@247
   129
    return render_template("/ozoneloss.html", act="ozoneloss", content=content)
j@247
   130
j@249
   131
@app.route("/ozoneloss/clams")
rb@257
   132
def ozoneloss_clams():
j@249
   133
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
j@247
   134
    content = get_content(filename)
j@249
   135
    return render_template("/ozoneloss_clams.html", act="ozoneloss/clams", content=content)
j@249
   136
j@249
   137
@app.route("/ozoneloss/vpsc")
rb@257
   138
def ozoneloss_vspc():
j@249
   139
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
j@249
   140
    content = get_content(filename)
j@249
   141
    return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc", content=content)
peter@1
   142
rb@233
   143
@app.route("/iek-7")
rb@257
   144
def institute():
rb@233
   145
    filename = os.path.join("templates", get_locale(), "rst", "iek-7.rst")
rb@167
   146
    content = get_content(filename)
rb@257
   147
    return render_template("/iek-7.html", act="iek-7", content=content)
peter@1
   148
peter@1
   149
rb@100
   150
@app.route("/imprint")
peter@88
   151
def imprint():
rb@163
   152
    filename = os.path.join("templates", get_locale(), "rst", "imprint.rst")
rb@163
   153
    content = get_content(filename)
rb@170
   154
    return render_template("/content.html", act="imprint", content=content)
peter@88
   155
peter@109
   156
peter@88
   157
peter@1
   158
@app.errorhandler(404)
peter@1
   159
def page_not_found(e):
rb@213
   160
    msg = _(u"Url: %(url)s not found", url=request.url)
rb@157
   161
    info = _(u"This information is not available!")
rb@157
   162
    return render_template("404.html", msg=msg, info=info)
peter@1
   163
peter@1
   164
if __name__ == "__main__":
peter@1
   165
    app.run(host='localhost', port=5014, debug=True)
Impressum Datenschutzerklärung