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