eskp.py
author Reimar Bauer <rb.proj AT googlemail DOT com>
Fri, 16 May 2014 11:10:42 +0200
changeset 267 64c9f7ecbac4
parent 265 69babac65a72
child 271 6ef8ac6c3481
permissions -rwxr-xr-x
refactored modal infos
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
rb@264
     7
from flask import Flask, Response
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@214
    25
def get_content(filename, overrides=None):
rb@162
    26
    content = u""
rb@246
    27
    filename = os.path.join(ESKP_PATH, filename)
rb@162
    28
    if os.path.isfile(filename):
rb@162
    29
        with codecs.open(filename, 'r', 'utf-8') as f:
rb@162
    30
            rst_data = f.read()
rb@162
    31
        f.close()
rb@214
    32
        content = publish_parts(rst_data, writer_name='html', settings_overrides=overrides)['html_body']
rb@162
    33
    return content
rb@162
    34
peter@142
    35
def get_topmenue():
rb@238
    36
    menue = [
rb@238
    37
             ('/ozoneloss', _(u'Ozoneloss')),
rb@238
    38
             ('/eskp', _(u'ESKP')),
rb@238
    39
             ('/iek-7', _(u'IEK-7')),
rb@186
    40
            ]
peter@145
    41
    return menue
peter@142
    42
peter@142
    43
app.jinja_env.globals.update(get_topmenue=get_topmenue)
peter@142
    44
j@250
    45
def get_o3lossclams_dates():
j@250
    46
    menue = [('/ozoneloss/clams/2012', _(u'2012')),
j@249
    47
             ('/ozoneloss/clams/2011', _(u'2011')),
j@249
    48
             ('/ozoneloss/clams/2010', _(u'2010')),
j@249
    49
             ]
j@249
    50
    return menue
j@249
    51
j@249
    52
def get_vpsc_dates():
j@249
    53
    menue = [('/ozoneloss/vpsc/2014', _(u'2014')),
j@249
    54
             ('/ozoneloss/vpsc/2013', _(u'2013')),
j@249
    55
             ('/ozoneloss/vpsc/2012', _(u'2012')),
j@249
    56
             ('/ozoneloss/vpsc/2011', _(u'2011')),
j@249
    57
             ('/ozoneloss/vpsc/2010', _(u'2010')),
rb@241
    58
             ]
rb@241
    59
    return menue
rb@241
    60
j@250
    61
app.jinja_env.globals.update(get_o3lossclams_dates=get_o3lossclams_dates)
j@249
    62
app.jinja_env.globals.update(get_vpsc_dates=get_vpsc_dates)
peter@142
    63
rb@267
    64
rb@267
    65
def modal_info(template, act, title, filename):
rb@264
    66
    content = get_content(filename)
rb@267
    67
    print template
rb@267
    68
    html = render_template(template, act=act, title=title, content=content, exit=_(u"Close"))
rb@264
    69
    return html
rb@264
    70
rb@264
    71
peter@1
    72
@babel.localeselector
peter@1
    73
def get_locale():
peter@109
    74
    """ToDo: if translation is completed, switch to en """
rb@118
    75
    return LANGUAGE_SELECTED or request.accept_languages.best_match(LANGUAGES.keys()) or 'de'
peter@1
    76
rb@100
    77
peter@1
    78
@app.route("/")
rb@100
    79
@app.route("/index")
peter@1
    80
def index():
rb@213
    81
    return render_template("/index.html",
rb@233
    82
                           eskp_info=_(u'About ESKP'),
rb@233
    83
                           )
peter@1
    84
rb@241
    85
rb@257
    86
@app.route('/ozoneloss/clams/<year>')
rb@257
    87
def ozoneloss_clams_year(year):
j@249
    88
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
rb@241
    89
    content = get_content(filename)
rb@257
    90
    return render_template("/ozoneloss_clams.html", act="ozoneloss/clams/%s" % year, content=content, year=year)
rb@241
    91
rb@241
    92
rb@257
    93
@app.route('/ozoneloss/vpsc/<year>')
rb@257
    94
def ozoneloss_vspc_year(year):
j@249
    95
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
j@249
    96
    content = get_content(filename)
j@250
    97
    filename = os.path.join("templates", get_locale(), "rst", "explanation_vpsc.rst")
j@250
    98
    explanation = get_content(filename)
j@250
    99
rb@257
   100
    return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc/%s" % year, content=content,
rb@257
   101
                           content_explanation=explanation, year=year)
rb@241
   102
rb@118
   103
@app.route('/de')
rb@118
   104
def de():
rb@118
   105
    global LANGUAGE_SELECTED
rb@118
   106
    LANGUAGE_SELECTED = "de"
peter@160
   107
    return render_template("/index.html",
rb@233
   108
                           eskp_info=_(u'About ESKP'),
rb@233
   109
                           )
rb@118
   110
rb@118
   111
@app.route('/en')
rb@118
   112
def en():
rb@118
   113
    global LANGUAGE_SELECTED
rb@118
   114
    LANGUAGE_SELECTED = "en"
peter@160
   115
    return render_template("/index.html",
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)
j@252
   123
    return render_template("/eskp.html", act="eskp", content=content)
j@252
   124
peter@1
   125
rb@233
   126
@app.route("/ozoneloss")
rb@257
   127
def ozoneloss():
rb@233
   128
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
rb@165
   129
    content = get_content(filename)
rb@264
   130
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_author.rst")
rb@267
   131
    author = modal_info("/author_info.html", "author", _(u"Ozoneloss"), filename)
rb@267
   132
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_publications.rst")
rb@267
   133
    publications = modal_info("/publications_info.html", "publications", _(u"Ozoneloss"), filename)
rb@264
   134
    return render_template("/ozoneloss.html", act="ozoneloss", content=content,
rb@267
   135
                           author=author, contact = u"Dr. Jens-Uwe Grooß", publications=publications )
j@247
   136
j@249
   137
@app.route("/ozoneloss/clams")
rb@257
   138
def ozoneloss_clams():
j@249
   139
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
j@247
   140
    content = get_content(filename)
j@249
   141
    return render_template("/ozoneloss_clams.html", act="ozoneloss/clams", content=content)
j@249
   142
j@249
   143
@app.route("/ozoneloss/vpsc")
rb@257
   144
def ozoneloss_vspc():
j@249
   145
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
j@249
   146
    content = get_content(filename)
j@249
   147
    return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc", content=content)
peter@1
   148
rb@233
   149
@app.route("/iek-7")
rb@257
   150
def institute():
rb@233
   151
    filename = os.path.join("templates", get_locale(), "rst", "iek-7.rst")
rb@167
   152
    content = get_content(filename)
rb@264
   153
    filename = os.path.join("templates", get_locale(), "rst", "iek-7_author.rst")
rb@267
   154
    author = modal_info("/author_info.html", "author", _(u"IEK-7"), filename)
rb@264
   155
    return render_template("/iek-7.html", act="iek-7", content=content,
rb@264
   156
                           author=author, contact = u"Sandra Stein")
peter@1
   157
peter@1
   158
rb@100
   159
@app.route("/imprint")
peter@88
   160
def imprint():
rb@163
   161
    filename = os.path.join("templates", get_locale(), "rst", "imprint.rst")
rb@163
   162
    content = get_content(filename)
rb@170
   163
    return render_template("/content.html", act="imprint", content=content)
peter@88
   164
peter@109
   165
peter@88
   166
rb@264
   167
rb@264
   168
peter@1
   169
@app.errorhandler(404)
peter@1
   170
def page_not_found(e):
rb@213
   171
    msg = _(u"Url: %(url)s not found", url=request.url)
rb@157
   172
    info = _(u"This information is not available!")
rb@157
   173
    return render_template("404.html", msg=msg, info=info)
peter@1
   174
peter@1
   175
if __name__ == "__main__":
peter@1
   176
    app.run(host='localhost', port=5014, debug=True)
Impressum Datenschutzerklärung