pymove3d.py
author Reimar Bauer <rb.proj AT googlemail DOT com>
Sun, 19 Jan 2014 20:04:32 +0100
changeset 179 4ea9139ca9fb
parent 177 6010a584dd21
child 180 981d9968f011
permissions -rwxr-xr-x
archives of 2014 competition can be readded later again
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@169
    62
                           competition_info=_(u'About Competition'),
rb@173
    63
                           dates=_(u'Dates'),
rb@173
    64
                           impressions=_(u'Impressions'))
peter@1
    65
rb@118
    66
@app.route('/de')
rb@118
    67
def de():
rb@118
    68
    global LANGUAGE_SELECTED
rb@118
    69
    LANGUAGE_SELECTED = "de"
peter@146
    70
    saying, author = get_saying()
peter@160
    71
    return render_template("/index.html",
peter@146
    72
                           saying = saying,
rb@168
    73
                           author = author,
rb@169
    74
                           competition_info=_(u'About Competition'),
rb@173
    75
                           dates=_(u'Dates'),
rb@173
    76
                           impressions=_(u'Impressions'))
rb@118
    77
rb@118
    78
@app.route('/en')
rb@118
    79
def en():
peter@146
    80
    saying, author = get_saying()
rb@118
    81
    global LANGUAGE_SELECTED
rb@118
    82
    LANGUAGE_SELECTED = "en"
peter@160
    83
    return render_template("/index.html",
peter@146
    84
                           saying = saying,
rb@168
    85
                           author = author,
rb@169
    86
                           competition_info=_(u'About Competition'),
rb@173
    87
                           dates=_(u'Dates'),
rb@173
    88
                           impressions=_(u'Impressions'))
rb@118
    89
rb@100
    90
@app.route("/competition")
peter@1
    91
def competition():
rb@162
    92
    filename = os.path.join("templates", get_locale(), "rst", "competition.rst")
rb@162
    93
    content = get_content(filename)
rb@170
    94
    return render_template("/content.html", act="competition", content=content)
peter@1
    95
rb@100
    96
@app.route("/task")
peter@1
    97
def task():
rb@165
    98
    filename = os.path.join("templates", get_locale(), "rst", "task.rst")
rb@165
    99
    content = get_content(filename)
rb@170
   100
    return render_template("/content.html", act="task", content=content)
peter@1
   101
rb@100
   102
@app.route("/submission")
peter@1
   103
def submission():
rb@167
   104
    filename = os.path.join("templates", get_locale(), "rst", "submission.rst")
rb@167
   105
    content = get_content(filename)
rb@170
   106
    return render_template("/content.html", act="submission", content=content)
peter@1
   107
rb@100
   108
@app.route("/coursematerial")
peter@1
   109
def coursematerial():
rb@166
   110
    filename = os.path.join("templates", get_locale(), "rst", "coursematerial.rst")
rb@166
   111
    content = get_content(filename)
rb@170
   112
    return render_template("/content.html", act="coursematerial", content=content)
peter@1
   113
rb@100
   114
@app.route("/imprint")
peter@88
   115
def imprint():
rb@163
   116
    filename = os.path.join("templates", get_locale(), "rst", "imprint.rst")
rb@163
   117
    content = get_content(filename)
rb@170
   118
    return render_template("/content.html", act="imprint", content=content)
peter@88
   119
rb@100
   120
@app.route("/privacy")
peter@88
   121
def privacy():
rb@164
   122
    filename = os.path.join("templates", get_locale(), "rst", "privacy.rst")
rb@164
   123
    content = get_content(filename)
rb@170
   124
    return render_template("/content.html", act="privacy", content=content)
rb@170
   125
rb@170
   126
@app.route("/dates")
rb@170
   127
def dates():
rb@170
   128
    filename = os.path.join("templates", get_locale(), "rst", "dates.rst")
rb@170
   129
    content = get_content(filename)
rb@170
   130
    return render_template("/content.html",
rb@170
   131
                           act="dates", content=content)
peter@88
   132
peter@123
   133
@app.route("/competition/2013")
peter@109
   134
def competition_2013():
rb@176
   135
    filename = os.path.join("templates", get_locale(), "archive", "competitions", "2013", "rst", "2013.rst")
rb@176
   136
    content = get_content(filename)
rb@176
   137
    return render_template("/impressions_2013.html",
rb@176
   138
                           act="competition_2013", content=content)
peter@109
   139
rb@177
   140
#ToDo content from rst missing
peter@123
   141
@app.route("/competition/2014")
peter@109
   142
def competition_2014():
rb@177
   143
    return render_template(get_locale() + "/archive/competitions/2014/index.html",
peter@109
   144
                           act="coursematerial")
peter@88
   145
peter@1
   146
@app.errorhandler(404)
peter@1
   147
def page_not_found(e):
rb@157
   148
    msg = _(u"Url: %(url)s not found" , url=request.url)
rb@157
   149
    info = _(u"This information is not available!")
rb@157
   150
    return render_template("404.html", msg=msg, info=info)
peter@1
   151
peter@1
   152
if __name__ == "__main__":
peter@1
   153
    app.run(host='localhost', port=5014, debug=True)
Impressum Datenschutzerklärung