eskp.py
author Jens-Uwe Grooss <j.-u.grooss@fz-juelich.de>
Sun, 21 Dec 2014 19:27:26 +0100
changeset 335 a3ee1aa4cda0
parent 317 08fcdd86155a
child 380 1c6c49675d42
permissions -rwxr-xr-x
plot update from 141221
rb@217
     1
# -*- coding: utf-8 -*-
rb@280
     2
import logging
rb@217
     3
rb@186
     4
import os
rb@186
     5
import codecs
rb@275
     6
import vobject
rb@275
     7
import StringIO
rb@275
     8
rb@275
     9
import qrcode
rb@186
    10
rb@186
    11
from docutils.core import publish_parts
rb@275
    12
from flask import Flask
peter@1
    13
from flask import render_template
peter@1
    14
from flask import request
rb@95
    15
from flask.ext.babel import gettext as _
peter@1
    16
from flask.ext.babel import Babel
rb@95
    17
from config import LANGUAGES
rb@161
    18
rb@275
    19
import base64
rb@275
    20
rb@122
    21
LANGUAGE_SELECTED = "de"
rb@122
    22
#ToDo after engelish is implemented set LANGUAGE_SELECTED = None
rb@95
    23
rb@246
    24
# We need the path of this file to find templates to translate
rb@246
    25
ESKP_PATH = os.path.dirname(os.path.abspath(__file__))
rb@280
    26
logging.basicConfig(filename=os.path.join(ESKP_PATH, 'eskp-app.log'),level=logging.DEBUG)
rb@246
    27
peter@1
    28
app = Flask(__name__)
peter@1
    29
babel = Babel(app)
peter@1
    30
peter@145
    31
app.config['BABEL_DEFAULT_LOCALE'] = 'de'
peter@145
    32
rb@275
    33
rb@275
    34
rb@275
    35
def get_vcard(filename):
rb@281
    36
    filename = os.path.join(ESKP_PATH, filename)
rb@275
    37
    with codecs.open(filename, 'r', 'utf-8') as f:
rb@275
    38
            vcard = f.read()
rb@275
    39
            f.close()
rb@275
    40
    return vobject.readOne(vcard)
rb@275
    41
rb@275
    42
rb@214
    43
def get_content(filename, overrides=None):
rb@162
    44
    content = u""
rb@246
    45
    filename = os.path.join(ESKP_PATH, filename)
rb@162
    46
    if os.path.isfile(filename):
rb@162
    47
        with codecs.open(filename, 'r', 'utf-8') as f:
rb@162
    48
            rst_data = f.read()
rb@162
    49
        f.close()
rb@214
    50
        content = publish_parts(rst_data, writer_name='html', settings_overrides=overrides)['html_body']
rb@162
    51
    return content
rb@162
    52
peter@142
    53
def get_topmenue():
rb@238
    54
    menue = [
rb@306
    55
             ('/ozoneloss', _(u'Ozoneloss'),(
j@309
    56
             ('/ozoneloss', _(u'overview')),
rb@304
    57
             ('/ozoneloss/clams/2015', _(u'calculations')),
j@317
    58
             ('/ozoneloss/vpsc/2015', _(u'estimations')),
j@317
    59
             ('/ozoneloss/uvi', _(u'uv increase')))),
rb@304
    60
rb@304
    61
             ('/eskp', _(u'ESKP'),(None, None)),
rb@304
    62
             ('/iek-7', _(u'IEK-7'),(None, None))
rb@186
    63
            ]
peter@145
    64
    return menue
peter@142
    65
peter@142
    66
app.jinja_env.globals.update(get_topmenue=get_topmenue)
peter@142
    67
j@250
    68
def get_o3lossclams_dates():
rb@302
    69
    menue = [
rb@302
    70
             ('/ozoneloss/clams/2015', _(u'2015')),
rb@302
    71
             ('/ozoneloss/clams/2012', _(u'2012')),
j@249
    72
             ('/ozoneloss/clams/2011', _(u'2011')),
j@249
    73
             ('/ozoneloss/clams/2010', _(u'2010')),
j@249
    74
             ]
j@249
    75
    return menue
j@249
    76
j@249
    77
def get_vpsc_dates():
rb@302
    78
    menue = [
rb@302
    79
             ('/ozoneloss/vpsc/2015', _(u'2015')),
rb@302
    80
             ('/ozoneloss/vpsc/2014', _(u'2014')),
j@249
    81
             ('/ozoneloss/vpsc/2013', _(u'2013')),
j@249
    82
             ('/ozoneloss/vpsc/2012', _(u'2012')),
j@249
    83
             ('/ozoneloss/vpsc/2011', _(u'2011')),
j@249
    84
             ('/ozoneloss/vpsc/2010', _(u'2010')),
rb@241
    85
             ]
rb@241
    86
    return menue
rb@241
    87
j@250
    88
app.jinja_env.globals.update(get_o3lossclams_dates=get_o3lossclams_dates)
j@249
    89
app.jinja_env.globals.update(get_vpsc_dates=get_vpsc_dates)
peter@142
    90
rb@267
    91
rb@267
    92
def modal_info(template, act, title, filename):
rb@264
    93
    content = get_content(filename)
rb@267
    94
    html = render_template(template, act=act, title=title, content=content, exit=_(u"Close"))
rb@264
    95
    return html
rb@264
    96
rb@264
    97
peter@1
    98
@babel.localeselector
peter@1
    99
def get_locale():
peter@109
   100
    """ToDo: if translation is completed, switch to en """
rb@118
   101
    return LANGUAGE_SELECTED or request.accept_languages.best_match(LANGUAGES.keys()) or 'de'
peter@1
   102
rb@100
   103
peter@1
   104
@app.route("/")
rb@100
   105
@app.route("/index")
peter@1
   106
def index():
rb@213
   107
    return render_template("/index.html",
rb@233
   108
                           eskp_info=_(u'About ESKP'),
rb@233
   109
                           )
peter@1
   110
rb@241
   111
rb@257
   112
@app.route('/ozoneloss/clams/<year>')
rb@257
   113
def ozoneloss_clams_year(year):
j@249
   114
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
rb@241
   115
    content = get_content(filename)
rb@257
   116
    return render_template("/ozoneloss_clams.html", act="ozoneloss/clams/%s" % year, content=content, year=year)
rb@241
   117
rb@241
   118
rb@257
   119
@app.route('/ozoneloss/vpsc/<year>')
rb@257
   120
def ozoneloss_vspc_year(year):
j@249
   121
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
j@249
   122
    content = get_content(filename)
j@250
   123
    filename = os.path.join("templates", get_locale(), "rst", "explanation_vpsc.rst")
j@250
   124
    explanation = get_content(filename)
j@250
   125
rb@257
   126
    return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc/%s" % year, content=content,
rb@257
   127
                           content_explanation=explanation, year=year)
rb@241
   128
j@317
   129
rb@118
   130
@app.route('/de')
rb@118
   131
def de():
rb@118
   132
    global LANGUAGE_SELECTED
rb@118
   133
    LANGUAGE_SELECTED = "de"
peter@160
   134
    return render_template("/index.html",
rb@233
   135
                           eskp_info=_(u'About ESKP'),
rb@233
   136
                           )
rb@118
   137
rb@118
   138
@app.route('/en')
rb@118
   139
def en():
rb@118
   140
    global LANGUAGE_SELECTED
rb@118
   141
    LANGUAGE_SELECTED = "en"
peter@160
   142
    return render_template("/index.html",
rb@233
   143
                           eskp_info=_(u'About ESKP'),
rb@233
   144
                           )
rb@118
   145
rb@233
   146
@app.route("/eskp")
rb@233
   147
def eskp():
rb@233
   148
    filename = os.path.join("templates", get_locale(), "rst", "eskp.rst")
rb@162
   149
    content = get_content(filename)
j@272
   150
    filename = os.path.join("templates", get_locale(), "rst", "eskp_title.rst")
j@272
   151
    headline = get_content(filename)
j@272
   152
    return render_template("/eskp.html", act="eskp", content=content, headline=headline)
j@252
   153
peter@1
   154
rb@275
   155
def qr_image_data(card):
rb@275
   156
    buf= StringIO.StringIO()
rb@275
   157
    qr = qrcode.QRCode(
rb@275
   158
        version=1,
rb@275
   159
        error_correction=qrcode.constants.ERROR_CORRECT_L,
rb@275
   160
        box_size=2,
rb@275
   161
        border=2,
rb@275
   162
        )
rb@275
   163
    qr.add_data(card.serialize())
rb@275
   164
    qr.make(fit=True)
rb@275
   165
    img = qr.make_image()
rb@275
   166
    img.save(buf)
rb@275
   167
    image = buf.getvalue()
rb@275
   168
    return base64.b64encode(image)
rb@275
   169
rb@275
   170
rb@233
   171
@app.route("/ozoneloss")
rb@257
   172
def ozoneloss():
rb@275
   173
rb@233
   174
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
rb@165
   175
    content = get_content(filename)
rb@275
   176
rb@275
   177
    vcard_file = os.path.join("vcards", "jug.vcf")
rb@275
   178
    author = u""
rb@275
   179
    try:
rb@275
   180
        card = get_vcard(vcard_file)
rb@275
   181
    except IOError:
rb@275
   182
        card = None
rb@275
   183
    if card is not None:
rb@275
   184
        qr_image = qr_image_data(card)
rb@275
   185
        author = render_template("/author_info.html", act="author", title=_(u"Ozoneloss"),
rb@275
   186
                                  card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
rb@275
   187
rb@267
   188
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_publications.rst")
rb@267
   189
    publications = modal_info("/publications_info.html", "publications", _(u"Ozoneloss"), filename)
rb@264
   190
    return render_template("/ozoneloss.html", act="ozoneloss", content=content,
rb@275
   191
                           author=author,card=card, publications=publications )
j@247
   192
j@249
   193
@app.route("/ozoneloss/clams")
rb@257
   194
def ozoneloss_clams():
j@249
   195
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
j@247
   196
    content = get_content(filename)
j@249
   197
    return render_template("/ozoneloss_clams.html", act="ozoneloss/clams", content=content)
j@249
   198
j@249
   199
@app.route("/ozoneloss/vpsc")
rb@257
   200
def ozoneloss_vspc():
j@249
   201
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
j@249
   202
    content = get_content(filename)
j@249
   203
    return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc", content=content)
peter@1
   204
j@317
   205
@app.route("/ozoneloss/uvi")
j@317
   206
def ozoneloss_uvi():
j@317
   207
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvi.rst")
j@317
   208
    content = get_content(filename)
j@317
   209
    return render_template("/ozoneloss_uvi.html", act="ozoneloss/uvi", content=content,
j@317
   210
                           alt=_(u"UV increase at 52 degrees N for 50 DU ozone depletion"))
j@317
   211
rb@233
   212
@app.route("/iek-7")
rb@257
   213
def institute():
rb@233
   214
    filename = os.path.join("templates", get_locale(), "rst", "iek-7.rst")
rb@167
   215
    content = get_content(filename)
rb@277
   216
    vcard_file = os.path.join("vcards", "sas.vcf")
rb@277
   217
    author = u""
rb@277
   218
    try:
rb@277
   219
        card = get_vcard(vcard_file)
rb@277
   220
    except IOError:
rb@277
   221
        card = None
rb@277
   222
    if card is not None:
rb@277
   223
        qr_image = qr_image_data(card)
rb@277
   224
        author = render_template("/author_info.html", act="author", title=_(u"IEK-7"),
rb@277
   225
                                  card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
rb@277
   226
rb@264
   227
    return render_template("/iek-7.html", act="iek-7", content=content,
rb@277
   228
                           author=author, card=card, contact = u"IEK-7")
peter@1
   229
peter@1
   230
rb@100
   231
@app.route("/imprint")
peter@88
   232
def imprint():
rb@163
   233
    filename = os.path.join("templates", get_locale(), "rst", "imprint.rst")
rb@163
   234
    content = get_content(filename)
rb@170
   235
    return render_template("/content.html", act="imprint", content=content)
peter@88
   236
peter@109
   237
peter@88
   238
rb@264
   239
rb@264
   240
peter@1
   241
@app.errorhandler(404)
peter@1
   242
def page_not_found(e):
rb@213
   243
    msg = _(u"Url: %(url)s not found", url=request.url)
rb@157
   244
    info = _(u"This information is not available!")
rb@157
   245
    return render_template("404.html", msg=msg, info=info)
peter@1
   246
peter@1
   247
if __name__ == "__main__":
peter@1
   248
    app.run(host='localhost', port=5014, debug=True)
Impressum Datenschutzerklärung