eskp.py
author Jens-Uwe Grooss <j.-u.grooss@fz-juelich.de>
Wed, 07 May 2014 09:52:18 +0200
changeset 247 6a8a901ba550
parent 246 591429b20748
child 249 3b60bc570d8a
permissions -rwxr-xr-x
Erweiterungen:
rst Texte ge?ndert
ozoneloss mit Kartenprojektion von Ozon und Ozonverlust
VPSC-Bilder der Jahre verschoben nach vpsc/years
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 = [
j@247
    38
             ('/', _(u'Home')),
rb@238
    39
             ('/ozoneloss', _(u'Ozoneloss')),
rb@238
    40
             ('/eskp', _(u'ESKP')),
rb@238
    41
             ('/iek-7', _(u'IEK-7')),
rb@186
    42
            ]
peter@145
    43
    return menue
peter@142
    44
peter@142
    45
app.jinja_env.globals.update(get_topmenue=get_topmenue)
peter@142
    46
rb@241
    47
def get_ozone_dates():
j@247
    48
    menue = [('/ozoneloss/years/2014', _(u'2014')),
j@247
    49
             ('/ozoneloss/years/2013', _(u'2013')),
j@247
    50
             ('/ozoneloss/years/2012', _(u'2012')),
j@247
    51
             ('/ozoneloss/years/2011', _(u'2011')),
j@247
    52
             ('/ozoneloss/years/2010', _(u'2010')),
rb@241
    53
             ]
rb@241
    54
    return menue
rb@241
    55
rb@241
    56
app.jinja_env.globals.update(get_ozone_dates=get_ozone_dates)
peter@142
    57
peter@1
    58
@babel.localeselector
peter@1
    59
def get_locale():
peter@109
    60
    """ToDo: if translation is completed, switch to en """
rb@118
    61
    return LANGUAGE_SELECTED or request.accept_languages.best_match(LANGUAGES.keys()) or 'de'
peter@1
    62
rb@100
    63
peter@1
    64
@app.route("/")
rb@100
    65
@app.route("/index")
peter@1
    66
def index():
rb@213
    67
    return render_template("/index.html",
rb@233
    68
                           eskp_info=_(u'About ESKP'),
rb@233
    69
                           )
peter@1
    70
rb@241
    71
j@247
    72
@app.route('/ozoneloss/years/2014')
rb@241
    73
def y2014():
j@247
    74
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_years.rst")
rb@241
    75
    content = get_content(filename)
j@247
    76
    return render_template("/ozoneloss_years.html", act="ozoneloss/years/2014", content=content, year=2014)
rb@241
    77
j@247
    78
@app.route('/ozoneloss/years/2013')
rb@241
    79
def y2013():
j@247
    80
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_years.rst")
rb@241
    81
    content = get_content(filename)
j@247
    82
    return render_template("/ozoneloss_years.html", act="ozoneloss/years/2013", content=content, year=2013)
rb@241
    83
j@247
    84
@app.route('/ozoneloss/years/2012')
rb@241
    85
def y2012():
j@247
    86
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_years.rst")
rb@241
    87
    content = get_content(filename)
j@247
    88
    return render_template("/ozoneloss_years.html", act="ozoneloss/years/2012", content=content, year=2012)
rb@241
    89
j@247
    90
@app.route('/ozoneloss/years/2011')
rb@241
    91
def y2011():
j@247
    92
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_years.rst")
rb@241
    93
    content = get_content(filename)
j@247
    94
    return render_template("/ozoneloss_years.html", act="ozoneloss/years/2011", content=content, year=2011)
rb@241
    95
j@247
    96
@app.route('/ozoneloss/years/2010')
rb@241
    97
def y2010():
j@247
    98
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_years.rst")
rb@241
    99
    content = get_content(filename)
j@247
   100
    return render_template("/ozoneloss_years.html", act="ozoneloss/years/2010", content=content, year=2010)
rb@241
   101
rb@118
   102
@app.route('/de')
rb@118
   103
def de():
rb@118
   104
    global LANGUAGE_SELECTED
rb@118
   105
    LANGUAGE_SELECTED = "de"
peter@146
   106
    saying, author = get_saying()
peter@160
   107
    return render_template("/index.html",
rb@186
   108
                           saying=saying,
rb@186
   109
                           author=author,
rb@233
   110
                           eskp_info=_(u'About ESKP'),
rb@233
   111
                           )
rb@118
   112
rb@118
   113
@app.route('/en')
rb@118
   114
def en():
peter@146
   115
    saying, author = get_saying()
rb@118
   116
    global LANGUAGE_SELECTED
rb@118
   117
    LANGUAGE_SELECTED = "en"
peter@160
   118
    return render_template("/index.html",
rb@186
   119
                           saying=saying,
rb@186
   120
                           author=author,
rb@233
   121
                           eskp_info=_(u'About ESKP'),
rb@233
   122
                           )
rb@118
   123
rb@233
   124
@app.route("/eskp")
rb@233
   125
def eskp():
rb@233
   126
    filename = os.path.join("templates", get_locale(), "rst", "eskp.rst")
rb@162
   127
    content = get_content(filename)
rb@233
   128
    return render_template("/content.html", act="eskp", content=content)
peter@1
   129
rb@233
   130
@app.route("/ozoneloss")
peter@1
   131
def task():
rb@233
   132
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
rb@165
   133
    content = get_content(filename)
j@247
   134
    return render_template("/ozoneloss.html", act="ozoneloss", content=content)
j@247
   135
j@247
   136
@app.route("/ozoneloss/years")
j@247
   137
def task1():
j@247
   138
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_years.rst")
j@247
   139
    content = get_content(filename)
j@247
   140
    return render_template("/ozoneloss_years.html", act="ozoneloss/years", content=content)
peter@1
   141
rb@233
   142
@app.route("/iek-7")
peter@1
   143
def submission():
rb@233
   144
    filename = os.path.join("templates", get_locale(), "rst", "iek-7.rst")
rb@167
   145
    content = get_content(filename)
rb@170
   146
    return render_template("/content.html", act="submission", content=content)
peter@1
   147
peter@1
   148
rb@100
   149
@app.route("/imprint")
peter@88
   150
def imprint():
rb@163
   151
    filename = os.path.join("templates", get_locale(), "rst", "imprint.rst")
rb@163
   152
    content = get_content(filename)
rb@170
   153
    return render_template("/content.html", act="imprint", content=content)
peter@88
   154
peter@109
   155
peter@88
   156
peter@1
   157
@app.errorhandler(404)
peter@1
   158
def page_not_found(e):
rb@213
   159
    msg = _(u"Url: %(url)s not found", url=request.url)
rb@157
   160
    info = _(u"This information is not available!")
rb@157
   161
    return render_template("404.html", msg=msg, info=info)
peter@1
   162
peter@1
   163
if __name__ == "__main__":
peter@1
   164
    app.run(host='localhost', port=5014, debug=True)
Impressum Datenschutzerklärung