eskp.py
changeset 275 6d0b1a0468af
parent 272 75000430e87b
child 277 128c9379cd29
equal deleted inserted replaced
274:b174d1f1716e 275:6d0b1a0468af
     1 # -*- coding: utf-8 -*-
     1 # -*- coding: utf-8 -*-
     2 
     2 
     3 import os
     3 import os
     4 import codecs
     4 import codecs
       
     5 import vobject
       
     6 import StringIO
       
     7 
       
     8 import qrcode
       
     9 from qrcode.image.pure import PymagingImage
     5 
    10 
     6 from docutils.core import publish_parts
    11 from docutils.core import publish_parts
     7 from flask import Flask, Response
    12 from flask import Flask
     8 from flask import render_template
    13 from flask import render_template
     9 from flask import request
    14 from flask import request
    10 from flask.ext.babel import gettext as _
    15 from flask.ext.babel import gettext as _
    11 from flask.ext.babel import Babel
    16 from flask.ext.babel import Babel
    12 from config import LANGUAGES
    17 from config import LANGUAGES
    13 
    18 
       
    19 import base64
       
    20 
    14 LANGUAGE_SELECTED = "de"
    21 LANGUAGE_SELECTED = "de"
    15 #ToDo after engelish is implemented set LANGUAGE_SELECTED = None
    22 #ToDo after engelish is implemented set LANGUAGE_SELECTED = None
    16 
    23 
    17 # 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
    18 ESKP_PATH = os.path.dirname(os.path.abspath(__file__))
    25 ESKP_PATH = os.path.dirname(os.path.abspath(__file__))
    19 
    26 
    20 app = Flask(__name__)
    27 app = Flask(__name__)
    21 babel = Babel(app)
    28 babel = Babel(app)
    22 
    29 
    23 app.config['BABEL_DEFAULT_LOCALE'] = 'de'
    30 app.config['BABEL_DEFAULT_LOCALE'] = 'de'
       
    31 
       
    32 
       
    33 
       
    34 def get_vcard(filename):
       
    35     with codecs.open(filename, 'r', 'utf-8') as f:
       
    36             vcard = f.read()
       
    37             f.close()
       
    38     return vobject.readOne(vcard)
       
    39 
    24 
    40 
    25 def get_content(filename, overrides=None):
    41 def get_content(filename, overrides=None):
    26     content = u""
    42     content = u""
    27     filename = os.path.join(ESKP_PATH, filename)
    43     filename = os.path.join(ESKP_PATH, filename)
    28     if os.path.isfile(filename):
    44     if os.path.isfile(filename):
    62 app.jinja_env.globals.update(get_vpsc_dates=get_vpsc_dates)
    78 app.jinja_env.globals.update(get_vpsc_dates=get_vpsc_dates)
    63 
    79 
    64 
    80 
    65 def modal_info(template, act, title, filename):
    81 def modal_info(template, act, title, filename):
    66     content = get_content(filename)
    82     content = get_content(filename)
    67     print template
       
    68     html = render_template(template, act=act, title=title, content=content, exit=_(u"Close"))
    83     html = render_template(template, act=act, title=title, content=content, exit=_(u"Close"))
    69     return html
    84     return html
    70 
    85 
    71 
    86 
    72 @babel.localeselector
    87 @babel.localeselector
   123     filename = os.path.join("templates", get_locale(), "rst", "eskp_title.rst")
   138     filename = os.path.join("templates", get_locale(), "rst", "eskp_title.rst")
   124     headline = get_content(filename)
   139     headline = get_content(filename)
   125     return render_template("/eskp.html", act="eskp", content=content, headline=headline)
   140     return render_template("/eskp.html", act="eskp", content=content, headline=headline)
   126 
   141 
   127 
   142 
       
   143 def qr_image_data(card):
       
   144     buf= StringIO.StringIO()
       
   145     qr = qrcode.QRCode(
       
   146         version=1,
       
   147         error_correction=qrcode.constants.ERROR_CORRECT_L,
       
   148         box_size=2,
       
   149         border=2,
       
   150         )
       
   151     qr.add_data(card.serialize())
       
   152     qr.make(fit=True)
       
   153     img = qr.make_image()
       
   154     img.save(buf)
       
   155     image = buf.getvalue()
       
   156     return base64.b64encode(image)
       
   157 
       
   158 
   128 @app.route("/ozoneloss")
   159 @app.route("/ozoneloss")
   129 def ozoneloss():
   160 def ozoneloss():
       
   161 
   130     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
   162     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
   131     content = get_content(filename)
   163     content = get_content(filename)
   132     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_author.rst")
   164 
   133     author = modal_info("/author_info.html", "author", _(u"Ozoneloss"), filename)
   165     vcard_file = os.path.join("vcards", "jug.vcf")
       
   166     author = u""
       
   167     try:
       
   168         card = get_vcard(vcard_file)
       
   169     except IOError:
       
   170         card = None
       
   171     if card is not None:
       
   172         qr_image = qr_image_data(card)
       
   173         author = render_template("/author_info.html", act="author", title=_(u"Ozoneloss"),
       
   174                                   card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
       
   175 
   134     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_publications.rst")
   176     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_publications.rst")
   135     publications = modal_info("/publications_info.html", "publications", _(u"Ozoneloss"), filename)
   177     publications = modal_info("/publications_info.html", "publications", _(u"Ozoneloss"), filename)
   136     return render_template("/ozoneloss.html", act="ozoneloss", content=content,
   178     return render_template("/ozoneloss.html", act="ozoneloss", content=content,
   137                            author=author, contact = u"Dr. Jens-Uwe Grooß", publications=publications )
   179                            author=author,card=card, publications=publications )
   138 
   180 
   139 @app.route("/ozoneloss/clams")
   181 @app.route("/ozoneloss/clams")
   140 def ozoneloss_clams():
   182 def ozoneloss_clams():
   141     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
   183     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
   142     content = get_content(filename)
   184     content = get_content(filename)
Impressum Datenschutzerklärung