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