eskp.py
author Reimar Bauer <rb.proj AT googlemail DOT com>
Tue, 20 May 2014 15:45:14 +0200
changeset 275 6d0b1a0468af
parent 272 75000430e87b
child 277 128c9379cd29
permissions -rwxr-xr-x
vcards for buiseness card added
rb@217
     1
# -*- coding: utf-8 -*-
rb@217
     2
rb@186
     3
import os
rb@186
     4
import codecs
rb@275
     5
import vobject
rb@275
     6
import StringIO
rb@275
     7
rb@275
     8
import qrcode
rb@275
     9
from qrcode.image.pure import PymagingImage
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@246
    26
peter@1
    27
app = Flask(__name__)
peter@1
    28
babel = Babel(app)
peter@1
    29
peter@145
    30
app.config['BABEL_DEFAULT_LOCALE'] = 'de'
peter@145
    31
rb@275
    32
rb@275
    33
rb@275
    34
def get_vcard(filename):
rb@275
    35
    with codecs.open(filename, 'r', 'utf-8') as f:
rb@275
    36
            vcard = f.read()
rb@275
    37
            f.close()
rb@275
    38
    return vobject.readOne(vcard)
rb@275
    39
rb@275
    40
rb@214
    41
def get_content(filename, overrides=None):
rb@162
    42
    content = u""
rb@246
    43
    filename = os.path.join(ESKP_PATH, filename)
rb@162
    44
    if os.path.isfile(filename):
rb@162
    45
        with codecs.open(filename, 'r', 'utf-8') as f:
rb@162
    46
            rst_data = f.read()
rb@162
    47
        f.close()
rb@214
    48
        content = publish_parts(rst_data, writer_name='html', settings_overrides=overrides)['html_body']
rb@162
    49
    return content
rb@162
    50
peter@142
    51
def get_topmenue():
rb@238
    52
    menue = [
rb@238
    53
             ('/ozoneloss', _(u'Ozoneloss')),
rb@238
    54
             ('/eskp', _(u'ESKP')),
rb@238
    55
             ('/iek-7', _(u'IEK-7')),
rb@186
    56
            ]
peter@145
    57
    return menue
peter@142
    58
peter@142
    59
app.jinja_env.globals.update(get_topmenue=get_topmenue)
peter@142
    60
j@250
    61
def get_o3lossclams_dates():
j@250
    62
    menue = [('/ozoneloss/clams/2012', _(u'2012')),
j@249
    63
             ('/ozoneloss/clams/2011', _(u'2011')),
j@249
    64
             ('/ozoneloss/clams/2010', _(u'2010')),
j@249
    65
             ]
j@249
    66
    return menue
j@249
    67
j@249
    68
def get_vpsc_dates():
j@249
    69
    menue = [('/ozoneloss/vpsc/2014', _(u'2014')),
j@249
    70
             ('/ozoneloss/vpsc/2013', _(u'2013')),
j@249
    71
             ('/ozoneloss/vpsc/2012', _(u'2012')),
j@249
    72
             ('/ozoneloss/vpsc/2011', _(u'2011')),
j@249
    73
             ('/ozoneloss/vpsc/2010', _(u'2010')),
rb@241
    74
             ]
rb@241
    75
    return menue
rb@241
    76
j@250
    77
app.jinja_env.globals.update(get_o3lossclams_dates=get_o3lossclams_dates)
j@249
    78
app.jinja_env.globals.update(get_vpsc_dates=get_vpsc_dates)
peter@142
    79
rb@267
    80
rb@267
    81
def modal_info(template, act, title, filename):
rb@264
    82
    content = get_content(filename)
rb@267
    83
    html = render_template(template, act=act, title=title, content=content, exit=_(u"Close"))
rb@264
    84
    return html
rb@264
    85
rb@264
    86
peter@1
    87
@babel.localeselector
peter@1
    88
def get_locale():
peter@109
    89
    """ToDo: if translation is completed, switch to en """
rb@118
    90
    return LANGUAGE_SELECTED or request.accept_languages.best_match(LANGUAGES.keys()) or 'de'
peter@1
    91
rb@100
    92
peter@1
    93
@app.route("/")
rb@100
    94
@app.route("/index")
peter@1
    95
def index():
rb@213
    96
    return render_template("/index.html",
rb@233
    97
                           eskp_info=_(u'About ESKP'),
rb@233
    98
                           )
peter@1
    99
rb@241
   100
rb@257
   101
@app.route('/ozoneloss/clams/<year>')
rb@257
   102
def ozoneloss_clams_year(year):
j@249
   103
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
rb@241
   104
    content = get_content(filename)
rb@257
   105
    return render_template("/ozoneloss_clams.html", act="ozoneloss/clams/%s" % year, content=content, year=year)
rb@241
   106
rb@241
   107
rb@257
   108
@app.route('/ozoneloss/vpsc/<year>')
rb@257
   109
def ozoneloss_vspc_year(year):
j@249
   110
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
j@249
   111
    content = get_content(filename)
j@250
   112
    filename = os.path.join("templates", get_locale(), "rst", "explanation_vpsc.rst")
j@250
   113
    explanation = get_content(filename)
j@250
   114
rb@257
   115
    return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc/%s" % year, content=content,
rb@257
   116
                           content_explanation=explanation, year=year)
rb@241
   117
rb@118
   118
@app.route('/de')
rb@118
   119
def de():
rb@118
   120
    global LANGUAGE_SELECTED
rb@118
   121
    LANGUAGE_SELECTED = "de"
peter@160
   122
    return render_template("/index.html",
rb@233
   123
                           eskp_info=_(u'About ESKP'),
rb@233
   124
                           )
rb@118
   125
rb@118
   126
@app.route('/en')
rb@118
   127
def en():
rb@118
   128
    global LANGUAGE_SELECTED
rb@118
   129
    LANGUAGE_SELECTED = "en"
peter@160
   130
    return render_template("/index.html",
rb@233
   131
                           eskp_info=_(u'About ESKP'),
rb@233
   132
                           )
rb@118
   133
rb@233
   134
@app.route("/eskp")
rb@233
   135
def eskp():
rb@233
   136
    filename = os.path.join("templates", get_locale(), "rst", "eskp.rst")
rb@162
   137
    content = get_content(filename)
j@272
   138
    filename = os.path.join("templates", get_locale(), "rst", "eskp_title.rst")
j@272
   139
    headline = get_content(filename)
j@272
   140
    return render_template("/eskp.html", act="eskp", content=content, headline=headline)
j@252
   141
peter@1
   142
rb@275
   143
def qr_image_data(card):
rb@275
   144
    buf= StringIO.StringIO()
rb@275
   145
    qr = qrcode.QRCode(
rb@275
   146
        version=1,
rb@275
   147
        error_correction=qrcode.constants.ERROR_CORRECT_L,
rb@275
   148
        box_size=2,
rb@275
   149
        border=2,
rb@275
   150
        )
rb@275
   151
    qr.add_data(card.serialize())
rb@275
   152
    qr.make(fit=True)
rb@275
   153
    img = qr.make_image()
rb@275
   154
    img.save(buf)
rb@275
   155
    image = buf.getvalue()
rb@275
   156
    return base64.b64encode(image)
rb@275
   157
rb@275
   158
rb@233
   159
@app.route("/ozoneloss")
rb@257
   160
def ozoneloss():
rb@275
   161
rb@233
   162
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
rb@165
   163
    content = get_content(filename)
rb@275
   164
rb@275
   165
    vcard_file = os.path.join("vcards", "jug.vcf")
rb@275
   166
    author = u""
rb@275
   167
    try:
rb@275
   168
        card = get_vcard(vcard_file)
rb@275
   169
    except IOError:
rb@275
   170
        card = None
rb@275
   171
    if card is not None:
rb@275
   172
        qr_image = qr_image_data(card)
rb@275
   173
        author = render_template("/author_info.html", act="author", title=_(u"Ozoneloss"),
rb@275
   174
                                  card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
rb@275
   175
rb@267
   176
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_publications.rst")
rb@267
   177
    publications = modal_info("/publications_info.html", "publications", _(u"Ozoneloss"), filename)
rb@264
   178
    return render_template("/ozoneloss.html", act="ozoneloss", content=content,
rb@275
   179
                           author=author,card=card, publications=publications )
j@247
   180
j@249
   181
@app.route("/ozoneloss/clams")
rb@257
   182
def ozoneloss_clams():
j@249
   183
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
j@247
   184
    content = get_content(filename)
j@249
   185
    return render_template("/ozoneloss_clams.html", act="ozoneloss/clams", content=content)
j@249
   186
j@249
   187
@app.route("/ozoneloss/vpsc")
rb@257
   188
def ozoneloss_vspc():
j@249
   189
    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
j@249
   190
    content = get_content(filename)
j@249
   191
    return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc", content=content)
peter@1
   192
rb@233
   193
@app.route("/iek-7")
rb@257
   194
def institute():
rb@233
   195
    filename = os.path.join("templates", get_locale(), "rst", "iek-7.rst")
rb@167
   196
    content = get_content(filename)
rb@264
   197
    filename = os.path.join("templates", get_locale(), "rst", "iek-7_author.rst")
rb@267
   198
    author = modal_info("/author_info.html", "author", _(u"IEK-7"), filename)
rb@264
   199
    return render_template("/iek-7.html", act="iek-7", content=content,
j@271
   200
                           author=author, contact = u"IEK-7")
peter@1
   201
peter@1
   202
rb@100
   203
@app.route("/imprint")
peter@88
   204
def imprint():
rb@163
   205
    filename = os.path.join("templates", get_locale(), "rst", "imprint.rst")
rb@163
   206
    content = get_content(filename)
rb@170
   207
    return render_template("/content.html", act="imprint", content=content)
peter@88
   208
peter@109
   209
peter@88
   210
rb@264
   211
rb@264
   212
peter@1
   213
@app.errorhandler(404)
peter@1
   214
def page_not_found(e):
rb@213
   215
    msg = _(u"Url: %(url)s not found", url=request.url)
rb@157
   216
    info = _(u"This information is not available!")
rb@157
   217
    return render_template("404.html", msg=msg, info=info)
peter@1
   218
peter@1
   219
if __name__ == "__main__":
peter@1
   220
    app.run(host='localhost', port=5014, debug=True)
Impressum Datenschutzerklärung