1 # -*- coding: utf-8 -*-
 
    11 from docutils.core import publish_parts
 
    12 from flask import Flask
 
    13 from flask import render_template
 
    14 from flask import request
 
    15 from flask.ext.babel import gettext as _
 
    16 from flask.ext.babel import Babel
 
    17 from config import LANGUAGES
 
    21 LANGUAGE_SELECTED = "de"
 
    22 #ToDo after engelish is implemented set LANGUAGE_SELECTED = None
 
    24 # We need the path of this file to find templates to translate
 
    25 ESKP_PATH = os.path.dirname(os.path.abspath(__file__))
 
    26 logging.basicConfig(filename=os.path.join(ESKP_PATH, 'eskp-app.log'),level=logging.DEBUG)
 
    31 app.config['BABEL_DEFAULT_LOCALE'] = 'de'
 
    35 def get_vcard(filename):
 
    36     filename = os.path.join(ESKP_PATH, filename)
 
    37     with codecs.open(filename, 'r', 'utf-8') as f:
 
    40     return vobject.readOne(vcard)
 
    43 def get_content(filename, overrides=None):
 
    45     filename = os.path.join(ESKP_PATH, filename)
 
    46     if os.path.isfile(filename):
 
    47         with codecs.open(filename, 'r', 'utf-8') as f:
 
    50         content = publish_parts(rst_data, writer_name='html', settings_overrides=overrides)['html_body']
 
    55              ('/ozoneloss', _(u'quantification'),(
 
    56              ('/ozoneloss', _(u'Ozoneloss')),
 
    57              ('/ozoneloss/clams/2015', _(u'calculations')),
 
    58              ('/ozoneloss/vpsc/2015', _(u'estimations')))),
 
    60              ('/eskp', _(u'ESKP'),(None, None)),
 
    61              ('/iek-7', _(u'IEK-7'),(None, None))
 
    65 app.jinja_env.globals.update(get_topmenue=get_topmenue)
 
    67 def get_o3lossclams_dates():
 
    69              ('/ozoneloss/clams/2015', _(u'2015')),
 
    70              ('/ozoneloss/clams/2012', _(u'2012')),
 
    71              ('/ozoneloss/clams/2011', _(u'2011')),
 
    72              ('/ozoneloss/clams/2010', _(u'2010')),
 
    78              ('/ozoneloss/vpsc/2015', _(u'2015')),
 
    79              ('/ozoneloss/vpsc/2014', _(u'2014')),
 
    80              ('/ozoneloss/vpsc/2013', _(u'2013')),
 
    81              ('/ozoneloss/vpsc/2012', _(u'2012')),
 
    82              ('/ozoneloss/vpsc/2011', _(u'2011')),
 
    83              ('/ozoneloss/vpsc/2010', _(u'2010')),
 
    87 app.jinja_env.globals.update(get_o3lossclams_dates=get_o3lossclams_dates)
 
    88 app.jinja_env.globals.update(get_vpsc_dates=get_vpsc_dates)
 
    91 def modal_info(template, act, title, filename):
 
    92     content = get_content(filename)
 
    93     html = render_template(template, act=act, title=title, content=content, exit=_(u"Close"))
 
    99     """ToDo: if translation is completed, switch to en """
 
   100     return LANGUAGE_SELECTED or request.accept_languages.best_match(LANGUAGES.keys()) or 'de'
 
   106     return render_template("/index.html",
 
   107                            eskp_info=_(u'About ESKP'),
 
   111 @app.route('/ozoneloss/clams/<year>')
 
   112 def ozoneloss_clams_year(year):
 
   113     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
 
   114     content = get_content(filename)
 
   115     return render_template("/ozoneloss_clams.html", act="ozoneloss/clams/%s" % year, content=content, year=year)
 
   118 @app.route('/ozoneloss/vpsc/<year>')
 
   119 def ozoneloss_vspc_year(year):
 
   120     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
 
   121     content = get_content(filename)
 
   122     filename = os.path.join("templates", get_locale(), "rst", "explanation_vpsc.rst")
 
   123     explanation = get_content(filename)
 
   125     return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc/%s" % year, content=content,
 
   126                            content_explanation=explanation, year=year)
 
   130     global LANGUAGE_SELECTED
 
   131     LANGUAGE_SELECTED = "de"
 
   132     return render_template("/index.html",
 
   133                            eskp_info=_(u'About ESKP'),
 
   138     global LANGUAGE_SELECTED
 
   139     LANGUAGE_SELECTED = "en"
 
   140     return render_template("/index.html",
 
   141                            eskp_info=_(u'About ESKP'),
 
   146     filename = os.path.join("templates", get_locale(), "rst", "eskp.rst")
 
   147     content = get_content(filename)
 
   148     filename = os.path.join("templates", get_locale(), "rst", "eskp_title.rst")
 
   149     headline = get_content(filename)
 
   150     return render_template("/eskp.html", act="eskp", content=content, headline=headline)
 
   153 def qr_image_data(card):
 
   154     buf= StringIO.StringIO()
 
   157         error_correction=qrcode.constants.ERROR_CORRECT_L,
 
   161     qr.add_data(card.serialize())
 
   163     img = qr.make_image()
 
   165     image = buf.getvalue()
 
   166     return base64.b64encode(image)
 
   169 @app.route("/ozoneloss")
 
   172     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
 
   173     content = get_content(filename)
 
   175     vcard_file = os.path.join("vcards", "jug.vcf")
 
   178         card = get_vcard(vcard_file)
 
   182         qr_image = qr_image_data(card)
 
   183         author = render_template("/author_info.html", act="author", title=_(u"Ozoneloss"),
 
   184                                   card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
 
   186     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_publications.rst")
 
   187     publications = modal_info("/publications_info.html", "publications", _(u"Ozoneloss"), filename)
 
   188     return render_template("/ozoneloss.html", act="ozoneloss", content=content,
 
   189                            author=author,card=card, publications=publications )
 
   191 @app.route("/ozoneloss/clams")
 
   192 def ozoneloss_clams():
 
   193     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
 
   194     content = get_content(filename)
 
   195     return render_template("/ozoneloss_clams.html", act="ozoneloss/clams", content=content)
 
   197 @app.route("/ozoneloss/vpsc")
 
   198 def ozoneloss_vspc():
 
   199     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
 
   200     content = get_content(filename)
 
   201     return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc", content=content)
 
   205     filename = os.path.join("templates", get_locale(), "rst", "iek-7.rst")
 
   206     content = get_content(filename)
 
   207     vcard_file = os.path.join("vcards", "sas.vcf")
 
   210         card = get_vcard(vcard_file)
 
   214         qr_image = qr_image_data(card)
 
   215         author = render_template("/author_info.html", act="author", title=_(u"IEK-7"),
 
   216                                   card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
 
   218     return render_template("/iek-7.html", act="iek-7", content=content,
 
   219                            author=author, card=card, contact = u"IEK-7")
 
   222 @app.route("/imprint")
 
   224     filename = os.path.join("templates", get_locale(), "rst", "imprint.rst")
 
   225     content = get_content(filename)
 
   226     return render_template("/content.html", act="imprint", content=content)
 
   232 @app.errorhandler(404)
 
   233 def page_not_found(e):
 
   234     msg = _(u"Url: %(url)s not found", url=request.url)
 
   235     info = _(u"This information is not available!")
 
   236     return render_template("404.html", msg=msg, info=info)
 
   238 if __name__ == "__main__":
 
   239     app.run(host='localhost', port=5014, debug=True)