pymove3d.py
author Peter Koppatz <peter@koppatz.com>
Sun, 19 Jan 2014 21:34:36 +0100
changeset 188 104211ae0ffa
parent 186 ddcf9027531e
child 202 c8d8bbf51f90
permissions -rwxr-xr-x
W3C errrors
rb@186
     1
import os
rb@186
     2
import codecs
rb@186
     3
rb@186
     4
from docutils.core import publish_parts
peter@1
     5
from flask import Flask
peter@1
     6
from flask import render_template
peter@1
     7
from flask import request
rb@95
     8
from flask.ext.babel import gettext as _
peter@1
     9
from flask.ext.babel import Babel
rb@95
    10
from config import LANGUAGES
peter@141
    11
from sayings import get_saying
rb@161
    12
rb@161
    13
rb@122
    14
LANGUAGE_SELECTED = "de"
rb@122
    15
#ToDo after engelish is implemented set LANGUAGE_SELECTED = None
rb@95
    16
peter@1
    17
app = Flask(__name__)
peter@1
    18
babel = Babel(app)
peter@1
    19
peter@145
    20
app.config['BABEL_DEFAULT_LOCALE'] = 'de'
peter@145
    21
rb@186
    22
rb@162
    23
def get_content(filename):
rb@162
    24
    content = u""
rb@162
    25
    if os.path.isfile(filename):
rb@162
    26
        with codecs.open(filename, 'r', 'utf-8') as f:
rb@162
    27
            rst_data = f.read()
rb@162
    28
        f.close()
rb@162
    29
        content = publish_parts(rst_data, writer_name='html')['html_body']
rb@162
    30
    return content
rb@162
    31
peter@142
    32
def get_topmenue():
rb@186
    33
    menue = [('/competition', _(u'Competition')),
rb@154
    34
              ('/task', _(u'Task')),
rb@154
    35
              ('/submission', _(u'Submission')),
rb@154
    36
              ('/coursematerial', _(u'Coursematerial')),
rb@186
    37
            ]
peter@145
    38
    return menue
peter@142
    39
peter@142
    40
app.jinja_env.globals.update(get_topmenue=get_topmenue)
peter@142
    41
peter@142
    42
peter@1
    43
@babel.localeselector
peter@1
    44
def get_locale():
peter@109
    45
    """ToDo: if translation is completed, switch to en """
rb@118
    46
    return LANGUAGE_SELECTED or request.accept_languages.best_match(LANGUAGES.keys()) or 'de'
peter@1
    47
rb@100
    48
peter@1
    49
@app.route("/")
rb@100
    50
@app.route("/index")
peter@1
    51
def index():
peter@141
    52
    saying, author = get_saying()
peter@160
    53
    return render_template("/index.html", 
rb@186
    54
                           saying=saying,
rb@186
    55
                           author=author,
rb@169
    56
                           competition_info=_(u'About Competition'),
rb@173
    57
                           dates=_(u'Dates'),
rb@173
    58
                           impressions=_(u'Impressions'))
peter@1
    59
rb@118
    60
@app.route('/de')
rb@118
    61
def de():
rb@118
    62
    global LANGUAGE_SELECTED
rb@118
    63
    LANGUAGE_SELECTED = "de"
peter@146
    64
    saying, author = get_saying()
peter@160
    65
    return render_template("/index.html",
rb@186
    66
                           saying=saying,
rb@186
    67
                           author=author,
rb@169
    68
                           competition_info=_(u'About Competition'),
rb@173
    69
                           dates=_(u'Dates'),
rb@173
    70
                           impressions=_(u'Impressions'))
rb@118
    71
rb@118
    72
@app.route('/en')
rb@118
    73
def en():
peter@146
    74
    saying, author = get_saying()
rb@118
    75
    global LANGUAGE_SELECTED
rb@118
    76
    LANGUAGE_SELECTED = "en"
peter@160
    77
    return render_template("/index.html",
rb@186
    78
                           saying=saying,
rb@186
    79
                           author=author,
rb@169
    80
                           competition_info=_(u'About Competition'),
rb@173
    81
                           dates=_(u'Dates'),
rb@173
    82
                           impressions=_(u'Impressions'))
rb@118
    83
rb@100
    84
@app.route("/competition")
peter@1
    85
def competition():
rb@162
    86
    filename = os.path.join("templates", get_locale(), "rst", "competition.rst")
rb@162
    87
    content = get_content(filename)
rb@170
    88
    return render_template("/content.html", act="competition", content=content)
peter@1
    89
rb@100
    90
@app.route("/task")
peter@1
    91
def task():
rb@165
    92
    filename = os.path.join("templates", get_locale(), "rst", "task.rst")
rb@165
    93
    content = get_content(filename)
rb@170
    94
    return render_template("/content.html", act="task", content=content)
peter@1
    95
rb@100
    96
@app.route("/submission")
peter@1
    97
def submission():
rb@167
    98
    filename = os.path.join("templates", get_locale(), "rst", "submission.rst")
rb@167
    99
    content = get_content(filename)
rb@170
   100
    return render_template("/content.html", act="submission", content=content)
peter@1
   101
rb@100
   102
@app.route("/coursematerial")
peter@1
   103
def coursematerial():
rb@166
   104
    filename = os.path.join("templates", get_locale(), "rst", "coursematerial.rst")
rb@166
   105
    content = get_content(filename)
rb@170
   106
    return render_template("/content.html", act="coursematerial", content=content)
peter@1
   107
rb@100
   108
@app.route("/imprint")
peter@88
   109
def imprint():
rb@163
   110
    filename = os.path.join("templates", get_locale(), "rst", "imprint.rst")
rb@163
   111
    content = get_content(filename)
rb@170
   112
    return render_template("/content.html", act="imprint", content=content)
peter@88
   113
rb@100
   114
@app.route("/privacy")
peter@88
   115
def privacy():
rb@164
   116
    filename = os.path.join("templates", get_locale(), "rst", "privacy.rst")
rb@164
   117
    content = get_content(filename)
rb@170
   118
    return render_template("/content.html", act="privacy", content=content)
rb@170
   119
rb@170
   120
@app.route("/dates")
rb@170
   121
def dates():
rb@170
   122
    filename = os.path.join("templates", get_locale(), "rst", "dates.rst")
rb@170
   123
    content = get_content(filename)
rb@170
   124
    return render_template("/content.html",
rb@170
   125
                           act="dates", content=content)
peter@88
   126
peter@123
   127
@app.route("/competition/2013")
peter@109
   128
def competition_2013():
rb@180
   129
    filename = os.path.join("templates", get_locale(), "archive", "2013", "competitions", "rst", "2013.rst")
rb@176
   130
    content = get_content(filename)
rb@176
   131
    return render_template("/impressions_2013.html",
rb@176
   132
                           act="competition_2013", content=content)
peter@109
   133
peter@88
   134
peter@1
   135
@app.errorhandler(404)
peter@1
   136
def page_not_found(e):
rb@157
   137
    msg = _(u"Url: %(url)s not found" , url=request.url)
rb@157
   138
    info = _(u"This information is not available!")
rb@157
   139
    return render_template("404.html", msg=msg, info=info)
peter@1
   140
peter@1
   141
if __name__ == "__main__":
peter@1
   142
    app.run(host='localhost', port=5014, debug=True)
Impressum Datenschutzerklärung