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'Ozoneloss'),
 
    56              (('/ozoneloss', _(u'overview')),
 
    57              ('/ozoneloss/clams/2016', _(u'calculations')),
 
    58              ('/ozoneloss/vpsc/2016', _(u'estimations')),
 
    59              ('/ozoneloss/uvi', _(u'uv increase')))),
 
    61              ('/eskp', _(u'ESKP'),(None, None)),
 
    62              ('/iek-7', _(u'IEK-7'),(None, None))
 
    66 app.jinja_env.globals.update(get_topmenue=get_topmenue)
 
    68 def get_o3lossclams_dates():
 
    70         ('/ozoneloss/clams/2016', _(u'2016')),
 
    71         ('/ozoneloss/clams/2015', _(u'2015')),
 
    72         ('/ozoneloss/clams/2012', _(u'2012')),
 
    73         ('/ozoneloss/clams/2011', _(u'2011')),
 
    74         ('/ozoneloss/clams/2010', _(u'2010')),
 
    80         ('/ozoneloss/vpsc/2016', _(u'2016')),
 
    81         ('/ozoneloss/vpsc/2015', _(u'2015')),
 
    82         ('/ozoneloss/vpsc/2014', _(u'2014')),
 
    83         ('/ozoneloss/vpsc/2013', _(u'2013')),
 
    84         ('/ozoneloss/vpsc/2012', _(u'2012')),
 
    85         ('/ozoneloss/vpsc/2011', _(u'2011')),
 
    86         ('/ozoneloss/vpsc/2010', _(u'2010')),
 
    90 app.jinja_env.globals.update(get_o3lossclams_dates=get_o3lossclams_dates)
 
    91 app.jinja_env.globals.update(get_vpsc_dates=get_vpsc_dates)
 
    94 def modal_info(template, act, title, filename):
 
    95     content = get_content(filename)
 
    96     html = render_template(template, act=act, title=title, content=content, exit=_(u"Close"))
 
   100 @babel.localeselector
 
   102     """ToDo: if translation is completed, switch to en """
 
   103     return LANGUAGE_SELECTED or request.accept_languages.best_match(LANGUAGES.keys()) or 'de'
 
   109     return render_template("/index.html",
 
   110                            eskp_info=_(u'About ESKP'),
 
   114 @app.route('/ozoneloss/clams/<year>')
 
   115 def ozoneloss_clams_year(year):
 
   116     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
 
   117     content = get_content(filename)
 
   118     return render_template("/ozoneloss_clams.html", act="ozoneloss/clams/%s" % year, content=content, year=year)
 
   121 @app.route('/ozoneloss/vpsc/<year>')
 
   122 def ozoneloss_vspc_year(year):
 
   123     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
 
   124     content = get_content(filename)
 
   125     filename = os.path.join("templates", get_locale(), "rst", "explanation_vpsc.rst")
 
   126     explanation = get_content(filename)
 
   128     return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc/%s" % year, content=content,
 
   129                            content_explanation=explanation, year=year)
 
   134     global LANGUAGE_SELECTED
 
   135     LANGUAGE_SELECTED = "de"
 
   136     return render_template("/index.html",
 
   137                            eskp_info=_(u'About ESKP'),
 
   142     global LANGUAGE_SELECTED
 
   143     LANGUAGE_SELECTED = "en"
 
   144     return render_template("/index.html",
 
   145                            eskp_info=_(u'About ESKP'),
 
   150     filename = os.path.join("templates", get_locale(), "rst", "eskp.rst")
 
   151     content = get_content(filename)
 
   152     filename = os.path.join("templates", get_locale(), "rst", "eskp_title.rst")
 
   153     headline = get_content(filename)
 
   154     return render_template("/eskp.html", act="eskp", content=content, headline=headline)
 
   157 def qr_image_data(card):
 
   158     buf= StringIO.StringIO()
 
   161         error_correction=qrcode.constants.ERROR_CORRECT_L,
 
   165     qr.add_data(card.serialize())
 
   167     img = qr.make_image()
 
   169     image = buf.getvalue()
 
   170     return base64.b64encode(image)
 
   173 @app.route("/ozoneloss")
 
   176     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
 
   177     content = get_content(filename)
 
   179     vcard_file = os.path.join("vcards", "jug.vcf")
 
   182         card = get_vcard(vcard_file)
 
   186         qr_image = qr_image_data(card)
 
   187         author = render_template("/author_info.html", act="author", title=_(u"Ozoneloss"),
 
   188                                   card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
 
   190     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_publications.rst")
 
   191     publications = modal_info("/publications_info.html", "publications", _(u"Ozoneloss"), filename)
 
   192     return render_template("/ozoneloss.html", act="ozoneloss", content=content,
 
   193                            author=author,card=card, publications=publications )
 
   195 @app.route("/ozoneloss/clams")
 
   196 def ozoneloss_clams():
 
   197     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
 
   198     content = get_content(filename)
 
   199     return render_template("/ozoneloss_clams.html", act="ozoneloss/clams", content=content)
 
   201 @app.route("/ozoneloss/vpsc")
 
   202 def ozoneloss_vspc():
 
   203     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
 
   204     content = get_content(filename)
 
   205     return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc", content=content)
 
   208 @app.route("/ozoneloss/uvi", methods=['GET'])
 
   210     # XXX check 'POST' does not work
 
   211     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvi.rst")
 
   212     content = get_content(filename)
 
   216     figname = "uvincr_lat%0.3i_do3%0.3i.svg" % (lat, o3offset)
 
   219     return render_template('ozoneloss_uvi.html', act="ozoneloss/uvi", content=content, figname=figname,
 
   220                            alt=_(u"UV increase at {{lat}} degrees N for {{o3offset}} DU ozone depletion"))
 
   222 @app.route("/ozoneloss/uvi_graph", methods=['POST'])
 
   223 def ozoneloss_uvi_graph():
 
   224     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvi.rst")
 
   225     content = get_content(filename)
 
   227     latstr = request.form['Gradzahl']
 
   228     o3offsetstr = request.form['Dobson-Unit']
 
   230     latstr2 = latstr.replace(u'\xb0', '')
 
   232     if latstr2.endswith(u'N'):
 
   233         latstr2 = latstr2.replace(u'N', '')
 
   235     if latstr2.endswith(u'S'):
 
   236         latstr2 = latstr2.replace(u'S', '')
 
   237         lat = -float(latstr2)
 
   240     figname = "uvincr_lat%0.3i_do3%0.3i.svg" % (lat, float(o3offsetstr))
 
   242     return render_template('graph.html', act="ozoneloss_uvi_graph", content=content, figname=figname,
 
   243                            o3offsetstr=o3offsetstr, latstr=latstr)
 
   252     filename = os.path.join("templates", get_locale(), "rst", "iek-7.rst")
 
   253     content = get_content(filename)
 
   254     vcard_file = os.path.join("vcards", "sas.vcf")
 
   257         card = get_vcard(vcard_file)
 
   261         qr_image = qr_image_data(card)
 
   262         author = render_template("/author_info.html", act="author", title=_(u"IEK-7"),
 
   263                                   card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
 
   265     return render_template("/iek-7.html", act="iek-7", content=content,
 
   266                            author=author, card=card, contact=u"IEK-7")
 
   269 @app.route("/imprint")
 
   271     filename = os.path.join("templates", get_locale(), "rst", "imprint.rst")
 
   272     content = get_content(filename)
 
   273     return render_template("/content.html", act="imprint", content=content)
 
   279 @app.errorhandler(404)
 
   280 def page_not_found(e):
 
   281     msg = _(u"Url: %(url)s not found", url=request.url)
 
   282     info = _(u"This information is not available!")
 
   283     return render_template("404.html", msg=msg, info=info)
 
   285 if __name__ == "__main__":
 
   286     app.run(host='localhost', port=5014)