pymove3d.py
author Reimar Bauer <rb.proj AT googlemail DOT com>
Thu, 23 Jan 2014 17:52:52 +0100
changeset 213 1871ef3bee34
parent 202 c8d8bbf51f90
child 214 7dda7406a677
permissions -rwxr-xr-x
typo and PEP8 fixes
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@213
    23
def get_content(filename, settings=None):
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@213
    29
        content = publish_parts(rst_data, writer_name='html', settings_overwrite=settings)['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()
rb@213
    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@213
   117
    settings_overrides = {
rb@213
   118
                           'initial_header_level': 2,
rb@213
   119
                         }
rb@213
   120
    content = get_content(filename, settings=settings_overrides)
rb@170
   121
    return render_template("/content.html", act="privacy", content=content)
rb@170
   122
rb@170
   123
@app.route("/dates")
rb@170
   124
def dates():
rb@170
   125
    filename = os.path.join("templates", get_locale(), "rst", "dates.rst")
rb@170
   126
    content = get_content(filename)
rb@170
   127
    return render_template("/content.html",
rb@170
   128
                           act="dates", content=content)
peter@88
   129
peter@123
   130
@app.route("/competition/2013")
peter@109
   131
def competition_2013():
rb@176
   132
    return render_template("/impressions_2013.html",
rb@202
   133
                           act="competition_2013")
peter@109
   134
peter@88
   135
peter@1
   136
@app.errorhandler(404)
peter@1
   137
def page_not_found(e):
rb@213
   138
    msg = _(u"Url: %(url)s not found", url=request.url)
rb@157
   139
    info = _(u"This information is not available!")
rb@157
   140
    return render_template("404.html", msg=msg, info=info)
peter@1
   141
peter@1
   142
if __name__ == "__main__":
peter@1
   143
    app.run(host='localhost', port=5014, debug=True)
Impressum Datenschutzerklärung