eskp.py
changeset 499 04b941597cc4
parent 498 a4f39dc4db6a
child 501 bc348dd68c65
child 503 f44e4855e8e5
equal deleted inserted replaced
492:ef878249fb51 499:04b941597cc4
    17 from config import LANGUAGES
    17 from config import LANGUAGES
    18 
    18 
    19 import base64
    19 import base64
    20 
    20 
    21 LANGUAGE_SELECTED = "de"
    21 LANGUAGE_SELECTED = "de"
    22 #ToDo after engelish is implemented set LANGUAGE_SELECTED = None
    22 # ToDo after engelish is implemented set LANGUAGE_SELECTED = None
    23 
    23 
    24 # We need the path of this file to find templates to translate
    24 # We need the path of this file to find templates to translate
    25 ESKP_PATH = os.path.dirname(os.path.abspath(__file__))
    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)
    26 logging.basicConfig(filename=os.path.join(ESKP_PATH, 'eskp-app.log'), level=logging.DEBUG)
       
    27 FILES= os.listdir(os.path.join(ESKP_PATH, 'static/images/uvmap'))
    27 
    28 
    28 app = Flask(__name__)
    29 app = Flask(__name__)
    29 babel = Babel(app)
    30 babel = Babel(app)
    30 
    31 
    31 app.config['BABEL_DEFAULT_LOCALE'] = 'de'
    32 app.config['BABEL_DEFAULT_LOCALE'] = 'de'
    32 
       
    33 
    33 
    34 
    34 
    35 def get_vcard(filename):
    35 def get_vcard(filename):
    36     filename = os.path.join(ESKP_PATH, filename)
    36     filename = os.path.join(ESKP_PATH, filename)
    37     with codecs.open(filename, 'r', 'utf-8') as f:
    37     with codecs.open(filename, 'r', 'utf-8') as f:
    38             vcard = f.read()
    38         vcard = f.read()
    39             f.close()
       
    40     return vobject.readOne(vcard)
    39     return vobject.readOne(vcard)
    41 
    40 
    42 
    41 
    43 def get_content(filename, overrides=None):
    42 def get_content(filename, overrides=None):
    44     content = u""
    43     content = u""
    45     filename = os.path.join(ESKP_PATH, filename)
    44     filename = os.path.join(ESKP_PATH, filename)
    46     if os.path.isfile(filename):
    45     if os.path.isfile(filename):
    47         with codecs.open(filename, 'r', 'utf-8') as f:
    46         with codecs.open(filename, 'r', 'utf-8') as f:
    48             rst_data = f.read()
    47             rst_data = f.read()
    49         f.close()
       
    50         content = publish_parts(rst_data, writer_name='html', settings_overrides=overrides)['html_body']
    48         content = publish_parts(rst_data, writer_name='html', settings_overrides=overrides)['html_body']
    51     return content
    49     return content
    52 
    50 
       
    51 
       
    52 def get_newest_date():
       
    53     getdates = get_valid_dates(FILES)
       
    54     newest_date = getdates[-1]
       
    55 
       
    56     return newest_date
       
    57 
       
    58 
    53 def get_topmenue():
    59 def get_topmenue():
       
    60     newest_date = get_newest_date()
    54     menue = [
    61     menue = [
    55              ('/ozoneloss', _(u'Ozoneloss'),
    62         ('/ozoneloss', _(u'Ozoneloss'),
    56              (('/ozoneloss', _(u'overview')),
    63          (('/ozoneloss', _(u'overview')),
    57              ('/ozoneloss/clams/2016', _(u'calculations')),
    64           ('/ozoneloss/clams/2016', _(u'calculations')),
    58              ('/ozoneloss/vpsc/2016', _(u'estimations')),
    65           ('/ozoneloss/vpsc/2016', _(u'estimations')),
    59              ('/ozoneloss/uvi', _(u'uv increase')))),
    66           ('/ozoneloss/uvi', _(u'uv increase')),
    60 
    67           ('/ozoneloss/uvmap/' + newest_date, _(u'uv index map')))),
    61              ('/eskp', _(u'ESKP'),(None, None)),
    68         ('/eskp', _(u'ESKP'), (None, None)),
    62              ('/iek-7', _(u'IEK-7'),(None, None))
    69         ('/iek-7', _(u'IEK-7'), (None, None))
    63             ]
    70     ]
    64     return menue
    71     return menue
    65 
    72 
       
    73 
    66 app.jinja_env.globals.update(get_topmenue=get_topmenue)
    74 app.jinja_env.globals.update(get_topmenue=get_topmenue)
       
    75 
    67 
    76 
    68 def get_o3lossclams_dates():
    77 def get_o3lossclams_dates():
    69     menue = [
    78     menue = [
    70         ('/ozoneloss/clams/2016', _(u'2016')),
    79         ('/ozoneloss/clams/2016', _(u'2016')),
    71         ('/ozoneloss/clams/2015', _(u'2015')),
    80         ('/ozoneloss/clams/2015', _(u'2015')),
    72         ('/ozoneloss/clams/2012', _(u'2012')),
    81         ('/ozoneloss/clams/2012', _(u'2012')),
    73         ('/ozoneloss/clams/2011', _(u'2011')),
    82         ('/ozoneloss/clams/2011', _(u'2011')),
    74         ('/ozoneloss/clams/2010', _(u'2010')),
    83         ('/ozoneloss/clams/2010', _(u'2010')),
    75         ]
    84     ]
    76     return menue
    85     return menue
       
    86 
       
    87 
       
    88 def get_valid_dates(files):
       
    89     dates = []
       
    90 
       
    91     for file in files:
       
    92         if file.endswith('.png') and file.find('uvi') >= 0:
       
    93             date = file[-12:-6]
       
    94             dates.append(date)
       
    95     dates.sort()
       
    96     for date in dates:
       
    97         i = 0
       
    98         for param in ['uvi', 'o3col', 'do3col']:
       
    99             testfile = 'clams_' + param + '_' + date + '12.png'
       
   100             if files.count(testfile) > 0:
       
   101                 i = i + 1
       
   102         if i <> 3:
       
   103             dates.remove(date)
       
   104     return dates
       
   105 
       
   106 
       
   107 def get_o3lossuvmap_dates(date_show):
       
   108     dates = get_valid_dates(FILES)
       
   109     ndates = len(dates)
       
   110     ind = dates.index(date_show)
       
   111     if ind ==0 :
       
   112         chosendates = [dates[ind+1]]
       
   113     elif ind >= ndates - 1:
       
   114         chosendates= [dates[ind-1]]
       
   115     else:
       
   116         chosendates = [dates[ind-1], dates[ind+1]]
       
   117     menue = []
       
   118     for date in chosendates:
       
   119         text_date = date[-2:] + '.' + date[-4:-2] + '.'
       
   120         menue.append(('/ozoneloss/uvmap/' + date, _(text_date)))
       
   121     return menue
       
   122 
    77 
   123 
    78 def get_vpsc_dates():
   124 def get_vpsc_dates():
    79     menue = [
   125     menue = [
    80         ('/ozoneloss/vpsc/2016', _(u'2016')),
   126         ('/ozoneloss/vpsc/2016', _(u'2016')),
    81         ('/ozoneloss/vpsc/2015', _(u'2015')),
   127         ('/ozoneloss/vpsc/2015', _(u'2015')),
    82         ('/ozoneloss/vpsc/2014', _(u'2014')),
   128         ('/ozoneloss/vpsc/2014', _(u'2014')),
    83         ('/ozoneloss/vpsc/2013', _(u'2013')),
   129         ('/ozoneloss/vpsc/2013', _(u'2013')),
    84         ('/ozoneloss/vpsc/2012', _(u'2012')),
   130         ('/ozoneloss/vpsc/2012', _(u'2012')),
    85         ('/ozoneloss/vpsc/2011', _(u'2011')),
   131         ('/ozoneloss/vpsc/2011', _(u'2011')),
    86         ('/ozoneloss/vpsc/2010', _(u'2010')),
   132         ('/ozoneloss/vpsc/2010', _(u'2010')),
    87         ]
   133     ]
    88     return menue
   134     return menue
       
   135 
    89 
   136 
    90 app.jinja_env.globals.update(get_o3lossclams_dates=get_o3lossclams_dates)
   137 app.jinja_env.globals.update(get_o3lossclams_dates=get_o3lossclams_dates)
    91 app.jinja_env.globals.update(get_vpsc_dates=get_vpsc_dates)
   138 app.jinja_env.globals.update(get_vpsc_dates=get_vpsc_dates)
       
   139 app.jinja_env.globals.update(get_o3lossuvmap_dates=get_o3lossuvmap_dates)
    92 
   140 
    93 
   141 
    94 def modal_info(template, act, title, filename):
   142 def modal_info(template, act, title, filename):
    95     content = get_content(filename)
   143     content = get_content(filename)
    96     html = render_template(template, act=act, title=title, content=content, exit=_(u"Close"))
   144     html = render_template(template, act=act, title=title, content=content, exit=_(u"Close"))
   114 @app.route('/ozoneloss/clams/<year>')
   162 @app.route('/ozoneloss/clams/<year>')
   115 def ozoneloss_clams_year(year):
   163 def ozoneloss_clams_year(year):
   116     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
   164     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
   117     content = get_content(filename)
   165     content = get_content(filename)
   118     return render_template("/ozoneloss_clams.html", act="ozoneloss/clams/%s" % year, content=content, year=year)
   166     return render_template("/ozoneloss_clams.html", act="ozoneloss/clams/%s" % year, content=content, year=year)
       
   167 
       
   168 
       
   169 @app.route('/ozoneloss/uvmap/<date>')
       
   170 def ozoneloss_uvmap_date(date):
       
   171     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvmap.rst")
       
   172     content = get_content(filename)
       
   173     return render_template("/ozoneloss_uvmap.html", act="ozoneloss/uvmap/%s" % date, content=content, date=date)
   119 
   174 
   120 
   175 
   121 @app.route('/ozoneloss/vpsc/<year>')
   176 @app.route('/ozoneloss/vpsc/<year>')
   122 def ozoneloss_vspc_year(year):
   177 def ozoneloss_vspc_year(year):
   123     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
   178     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
   135     LANGUAGE_SELECTED = "de"
   190     LANGUAGE_SELECTED = "de"
   136     return render_template("/index.html",
   191     return render_template("/index.html",
   137                            eskp_info=_(u'About ESKP'),
   192                            eskp_info=_(u'About ESKP'),
   138                            )
   193                            )
   139 
   194 
       
   195 
   140 @app.route('/en')
   196 @app.route('/en')
   141 def en():
   197 def en():
   142     global LANGUAGE_SELECTED
   198     global LANGUAGE_SELECTED
   143     LANGUAGE_SELECTED = "en"
   199     LANGUAGE_SELECTED = "en"
   144     return render_template("/index.html",
   200     return render_template("/index.html",
   145                            eskp_info=_(u'About ESKP'),
   201                            eskp_info=_(u'About ESKP'),
   146                            )
   202                            )
   147 
   203 
       
   204 
   148 @app.route("/eskp")
   205 @app.route("/eskp")
   149 def eskp():
   206 def eskp():
   150     filename = os.path.join("templates", get_locale(), "rst", "eskp.rst")
   207     filename = os.path.join("templates", get_locale(), "rst", "eskp.rst")
   151     content = get_content(filename)
   208     content = get_content(filename)
   152     filename = os.path.join("templates", get_locale(), "rst", "eskp_title.rst")
   209     filename = os.path.join("templates", get_locale(), "rst", "eskp_title.rst")
   153     headline = get_content(filename)
   210     headline = get_content(filename)
   154     return render_template("/eskp.html", act="eskp", content=content, headline=headline)
   211     return render_template("/eskp.html", act="eskp", content=content, headline=headline)
   155 
   212 
   156 
   213 
   157 def qr_image_data(card):
   214 def qr_image_data(card):
   158     buf= StringIO.StringIO()
   215     buf = StringIO.StringIO()
   159     qr = qrcode.QRCode(
   216     qr = qrcode.QRCode(
   160         version=1,
   217         version=1,
   161         error_correction=qrcode.constants.ERROR_CORRECT_L,
   218         error_correction=qrcode.constants.ERROR_CORRECT_L,
   162         box_size=2,
   219         box_size=2,
   163         border=2,
   220         border=2,
   164         )
   221     )
   165     qr.add_data(card.serialize())
   222     qr.add_data(card.serialize())
   166     qr.make(fit=True)
   223     qr.make(fit=True)
   167     img = qr.make_image()
   224     img = qr.make_image()
   168     img.save(buf)
   225     img.save(buf)
   169     image = buf.getvalue()
   226     image = buf.getvalue()
   170     return base64.b64encode(image)
   227     return base64.b64encode(image)
   171 
   228 
   172 
   229 
   173 @app.route("/ozoneloss")
   230 @app.route("/ozoneloss")
   174 def ozoneloss():
   231 def ozoneloss():
   175 
       
   176     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
   232     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
   177     content = get_content(filename)
   233     content = get_content(filename)
   178 
   234 
   179     vcard_file = os.path.join("vcards", "jug.vcf")
   235     vcard_file = os.path.join("vcards", "jug.vcf")
   180     author = u""
   236     author = u""
       
   237 
   181     try:
   238     try:
   182         card = get_vcard(vcard_file)
   239         card = get_vcard(vcard_file)
   183     except IOError:
   240     except IOError:
   184         card = None
   241         card = None
   185     if card is not None:
   242     if card is not None:
   186         qr_image = qr_image_data(card)
   243         qr_image = qr_image_data(card)
   187         author = render_template("/author_info.html", act="author", title=_(u"Ozoneloss"),
   244         author = render_template("/author_info.html", act="author", title=_(u"Ozoneloss"),
   188                                   card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
   245                                  card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
   189 
   246 
   190     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_publications.rst")
   247     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_publications.rst")
   191     publications = modal_info("/publications_info.html", "publications", _(u"Ozoneloss"), filename)
   248     publications = modal_info("/publications_info.html", "publications", _(u"Ozoneloss"), filename)
   192     return render_template("/ozoneloss.html", act="ozoneloss", content=content,
   249     return render_template("/ozoneloss.html", act="ozoneloss", content=content,
   193                            author=author,card=card, publications=publications )
   250                            author=author, card=card, publications=publications)
       
   251 
   194 
   252 
   195 @app.route("/ozoneloss/clams")
   253 @app.route("/ozoneloss/clams")
   196 def ozoneloss_clams():
   254 def ozoneloss_clams():
   197     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
   255     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
   198     content = get_content(filename)
   256     content = get_content(filename)
   199     return render_template("/ozoneloss_clams.html", act="ozoneloss/clams", content=content)
   257     return render_template("/ozoneloss_clams.html", act="ozoneloss/clams", content=content)
       
   258 
       
   259 
       
   260 @app.route("/ozoneloss/uvmap")
       
   261 def ozoneloss_uvmap():
       
   262     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvmap.rst")
       
   263     content = get_content(filename)
       
   264     return render_template("/ozoneloss_uvmap.html", act="ozoneloss/uvmap", content=content)
       
   265 
   200 
   266 
   201 @app.route("/ozoneloss/vpsc")
   267 @app.route("/ozoneloss/vpsc")
   202 def ozoneloss_vspc():
   268 def ozoneloss_vspc():
   203     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
   269     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
   204     content = get_content(filename)
   270     content = get_content(filename)
   213 
   279 
   214     lat = 50.
   280     lat = 50.
   215     o3offset = 50.
   281     o3offset = 50.
   216     figname = "uvincr_lat%0.3i_do3%0.3i.svg" % (lat, o3offset)
   282     figname = "uvincr_lat%0.3i_do3%0.3i.svg" % (lat, o3offset)
   217 
   283 
   218 
       
   219     return render_template('ozoneloss_uvi.html', act="ozoneloss/uvi", content=content, figname=figname,
   284     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"))
   285                            alt=_(u"UV increase at {{lat}} degrees N for {{o3offset}} DU ozone depletion"))
       
   286 
   221 
   287 
   222 @app.route("/ozoneloss/uvi_graph", methods=['POST'])
   288 @app.route("/ozoneloss/uvi_graph", methods=['POST'])
   223 def ozoneloss_uvi_graph():
   289 def ozoneloss_uvi_graph():
   224     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvi.rst")
   290     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvi.rst")
   225     content = get_content(filename)
   291     content = get_content(filename)
   234         lat = float(latstr2)
   300         lat = float(latstr2)
   235     if latstr2.endswith(u'S'):
   301     if latstr2.endswith(u'S'):
   236         latstr2 = latstr2.replace(u'S', '')
   302         latstr2 = latstr2.replace(u'S', '')
   237         lat = -float(latstr2)
   303         lat = -float(latstr2)
   238 
   304 
   239 
       
   240     figname = "uvincr_lat%0.3i_do3%0.3i.svg" % (lat, float(o3offsetstr))
   305     figname = "uvincr_lat%0.3i_do3%0.3i.svg" % (lat, float(o3offsetstr))
   241 
   306 
   242     return render_template('graph.html', act="ozoneloss_uvi_graph", content=content, figname=figname,
   307     return render_template('graph.html', act="ozoneloss_uvi_graph", content=content, figname=figname,
   243                            o3offsetstr=o3offsetstr, latstr=latstr)
   308                            o3offsetstr=o3offsetstr, latstr=latstr)
   244 
       
   245 
       
   246 
       
   247 
       
   248 
   309 
   249 
   310 
   250 @app.route("/iek-7")
   311 @app.route("/iek-7")
   251 def institute():
   312 def institute():
   252     filename = os.path.join("templates", get_locale(), "rst", "iek-7.rst")
   313     filename = os.path.join("templates", get_locale(), "rst", "iek-7.rst")
   258     except IOError:
   319     except IOError:
   259         card = None
   320         card = None
   260     if card is not None:
   321     if card is not None:
   261         qr_image = qr_image_data(card)
   322         qr_image = qr_image_data(card)
   262         author = render_template("/author_info.html", act="author", title=_(u"IEK-7"),
   323         author = render_template("/author_info.html", act="author", title=_(u"IEK-7"),
   263                                   card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
   324                                  card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
   264 
   325 
   265     return render_template("/iek-7.html", act="iek-7", content=content,
   326     return render_template("/iek-7.html", act="iek-7", content=content,
   266                            author=author, card=card, contact=u"IEK-7")
   327                            author=author, card=card, contact=u"IEK-7")
   267 
   328 
   268 
   329 
   269 @app.route("/imprint")
   330 @app.route("/imprint")
   270 def imprint():
   331 def imprint():
   271     filename = os.path.join("templates", get_locale(), "rst", "imprint.rst")
   332     filename = os.path.join("templates", get_locale(), "rst", "imprint.rst")
   272     content = get_content(filename)
   333     content = get_content(filename)
   273     return render_template("/content.html", act="imprint", content=content)
   334     return render_template("/content.html", act="imprint", content=content)
   274 
       
   275 
       
   276 
       
   277 
   335 
   278 
   336 
   279 @app.errorhandler(404)
   337 @app.errorhandler(404)
   280 def page_not_found(e):
   338 def page_not_found(e):
   281     msg = _(u"Url: %(url)s not found", url=request.url)
   339     msg = _(u"Url: %(url)s not found", url=request.url)
   282     info = _(u"This information is not available!")
   340     info = _(u"This information is not available!")
   283     return render_template("404.html", msg=msg, info=info)
   341     return render_template("404.html", msg=msg, info=info)
   284 
   342 
       
   343 
   285 if __name__ == "__main__":
   344 if __name__ == "__main__":
   286     app.run(host='localhost', port=5014)
   345     app.run(host='localhost', port=5014, debug=True)
Impressum Datenschutzerklärung