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,
|
peter@141
|
61 |
author = author)
|
peter@1
|
62 |
|
rb@118
|
63 |
@app.route('/de')
|
rb@118
|
64 |
def de():
|
rb@118
|
65 |
global LANGUAGE_SELECTED
|
rb@118
|
66 |
LANGUAGE_SELECTED = "de"
|
peter@146
|
67 |
saying, author = get_saying()
|
peter@160
|
68 |
return render_template("/index.html",
|
peter@146
|
69 |
saying = saying,
|
peter@146
|
70 |
author = author)
|
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",
|
peter@146
|
78 |
saying = saying,
|
peter@146
|
79 |
author = author)
|
rb@118
|
80 |
|
rb@100
|
81 |
@app.route("/competition")
|
peter@1
|
82 |
def competition():
|
rb@162
|
83 |
filename = os.path.join("templates", get_locale(), "rst", "competition.rst")
|
rb@162
|
84 |
content = get_content(filename)
|
rb@162
|
85 |
return render_template("/competition.html", act="competition", content=content)
|
peter@1
|
86 |
|
rb@100
|
87 |
@app.route("/task")
|
peter@1
|
88 |
def task():
|
rb@165
|
89 |
filename = os.path.join("templates", get_locale(), "rst", "task.rst")
|
rb@165
|
90 |
content = get_content(filename)
|
rb@165
|
91 |
return render_template("/task.html", act="task", content=content)
|
peter@1
|
92 |
|
rb@100
|
93 |
@app.route("/submission")
|
peter@1
|
94 |
def submission():
|
rb@167
|
95 |
filename = os.path.join("templates", get_locale(), "rst", "submission.rst")
|
rb@167
|
96 |
content = get_content(filename)
|
rb@167
|
97 |
return render_template("/submission.html", act="submission", content=content)
|
peter@1
|
98 |
|
rb@100
|
99 |
@app.route("/coursematerial")
|
peter@1
|
100 |
def coursematerial():
|
rb@166
|
101 |
filename = os.path.join("templates", get_locale(), "rst", "coursematerial.rst")
|
rb@166
|
102 |
content = get_content(filename)
|
rb@166
|
103 |
return render_template("/coursematerial.html", act="coursematerial", content=content)
|
peter@1
|
104 |
|
rb@100
|
105 |
@app.route("/imprint")
|
peter@88
|
106 |
def imprint():
|
rb@163
|
107 |
filename = os.path.join("templates", get_locale(), "rst", "imprint.rst")
|
rb@163
|
108 |
content = get_content(filename)
|
rb@163
|
109 |
return render_template("/imprint.html", act="imprint", content=content)
|
peter@88
|
110 |
|
rb@100
|
111 |
@app.route("/privacy")
|
peter@88
|
112 |
def privacy():
|
rb@164
|
113 |
filename = os.path.join("templates", get_locale(), "rst", "privacy.rst")
|
rb@164
|
114 |
content = get_content(filename)
|
rb@164
|
115 |
return render_template("/privacy.html", act="privacy", content=content)
|
peter@88
|
116 |
|
peter@123
|
117 |
@app.route("/competition/2013")
|
peter@109
|
118 |
def competition_2013():
|
peter@123
|
119 |
print get_locale() + "/archive/competitions/2013/index.html"
|
peter@123
|
120 |
return render_template(get_locale() + "/archive/competitions/2013/index.html",
|
peter@109
|
121 |
act="coursematerial")
|
peter@109
|
122 |
|
peter@123
|
123 |
@app.route("/competition/2014")
|
peter@109
|
124 |
def competition_2014():
|
peter@123
|
125 |
print get_locale() + "/archive/competitions/2014/index.html"
|
peter@123
|
126 |
return render_template(get_locale() + "/archive/competitions/2014/index.html",
|
peter@109
|
127 |
act="coursematerial")
|
peter@88
|
128 |
|
rb@162
|
129 |
|
rb@152
|
130 |
@app.route("/dates")
|
hr@112
|
131 |
def dates():
|
rb@161
|
132 |
filename = os.path.join("templates", get_locale(), "rst", "dates.rst")
|
rb@162
|
133 |
content = get_content(filename)
|
rb@161
|
134 |
return render_template("/dates.html",
|
rb@161
|
135 |
act="dates", content=content)
|
hr@112
|
136 |
|
hr@112
|
137 |
|
peter@1
|
138 |
@app.errorhandler(404)
|
peter@1
|
139 |
def page_not_found(e):
|
rb@157
|
140 |
msg = _(u"Url: %(url)s not found" , url=request.url)
|
rb@157
|
141 |
info = _(u"This information is not available!")
|
rb@157
|
142 |
return render_template("404.html", msg=msg, info=info)
|
peter@1
|
143 |
|
peter@1
|
144 |
if __name__ == "__main__":
|
peter@1
|
145 |
app.run(host='localhost', port=5014, debug=True)
|