-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
executable file
·72 lines (62 loc) · 2.04 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import config
import logging
from flask import Flask, redirect, url_for, \
g, request, render_template
from dae.api.users import get_current_user, create_login_url
from models import init_db
from query import get_subjects, get_votes, \
update_votes, get_groups, get_group, \
create_subject
from utils import outdate, votetype
app = Flask(__name__)
app.debug = config.DEBUG
app.secret_key = config.SECRET_KEY
app.config.update(
SQLALCHEMY_DATABASE_URI = config.DATABASE_URI,
SQLALCHEMY_POOL_SIZE = 100,
SQLALCHEMY_POOL_TIMEOUT = 10,
SQLALCHEMY_POOL_RECYCLE = 3600,
)
app.jinja_env.globals['outdate'] = outdate
app.jinja_env.globals['votetype'] = votetype
logger = logging.getLogger(__name__)
init_db(app)
@app.route('/')
def index():
q = request.args.get('q', '')
return render_template('index.html', user=g.user, \
subjects=get_subjects(q), \
groups=get_groups(), \
group=get_group(q))
@app.route('/view/<int:sid>/')
def view(sid):
votes = get_votes(sid)
if not votes:
return redirect(url_for('index'))
subject = votes[0].subject
return render_template('view.html', subject=subject, \
votes=votes)
@app.route('/vote/<int:sid>/', methods=['POST'])
def vote(sid):
votes = request.form
if votes:
update_votes(votes.getlist('selected'))
return redirect(url_for('view', sid=sid))
@app.route('/write/', methods=['GET', 'POST'])
def write():
if request.method == 'GET':
return render_template('write.html', user=g.user, \
groups=get_groups())
topic = request.form.get('topic')
group = request.form.get('group')
deadline = request.form.get('deadline')
votetype = request.form.get('votetype')
options = request.form.getlist('options')
create_subject(topic, group, deadline, votetype, options, g.user.username)
return redirect(url_for('index', q=group))
@app.before_request
def before():
user = get_current_user()
if not user:
return redirect(create_login_url())
g.user = user