ea1179ecd368a5763122d941382ff56519dc9fd5
2 # -*- coding: utf-8 -*-
4 from flask
import Flask
, request
, session
, g
, redirect
, url_for
, abort
, \
7 from datetime
import date
, timedelta
9 locale
.setlocale(locale
.LC_ALL
, '')
11 DATABASE
= '/tmp/cavote.db'
12 SECRET_KEY
= '{J@uRKO,xO-PK7B,jF?>iHbxLasF9s#zjOoy=+:'
18 app
.config
.from_object(__name__
)
21 return sqlite3
.connect(app
.config
['DATABASE'])
28 def teardown_request(exception
):
33 return render_template('index.html')
39 @app.route('/login', methods
=['GET', 'POST'])
42 if request
.method
== 'POST':
43 if request
.form
['username'] != app
.config
['USERNAME']:
44 error
= 'Invalid username'
45 elif request
.form
['password'] != app
.config
['PASSWORD']:
46 error
= 'Invalid password'
48 session
['logged_in'] = True
49 session
['nickname'] = request
.form
['username']
50 if session
['nickname'] == 'admin':
51 session
['is_admin'] = True
52 flash('You were logged in')
53 return redirect(url_for('home'))
54 return render_template('login.html', error
=error
)
58 session
.pop('logged_in', None)
59 flash('You were logged out')
60 return redirect(url_for('home'))
72 @app.route('/votes/<votes>')
73 def show_votes(votes
):
74 today
= date
.today().strftime('%d %B %Y')
76 cur
= g
.db
.execute('select title, description, date_begin, date_end from votes order by id desc')
77 elif votes
== 'archives':
78 cur
= g
.db
.execute('select title, description, date_begin, date_end from votes where date_end < :today order by id desc', {"today" : today
})
79 elif votes
== 'currently':
80 cur
= g
.db
.execute('select title, description, date_begin, date_end from votes where date_end > :today order by id desc', {"today" : today
})
83 votes
= [dict(title
=row
[0], description
=row
[1], date_begin
=row
[2], date_end
=row
[3],
84 pourcent
=60) for row
in cur
.fetchall()]
85 return render_template('show_votes.html', votes
=votes
)
90 @app.route('/votes/admin/new')
92 if not session
.get('logged_in'):
94 return render_template('new_vote.html')
96 @app.route('/votes/admin/add', methods
=['POST'])
98 if not session
.get('logged_in'):
100 daten
= date
.today() + timedelta(days
=int(request
.form
['days']))
101 ndate
= daten
.strftime('%d %B %Y')
105 if 'transparent' in request
.form
.keys():
107 if 'public' in request
.form
.keys():
109 if 'multiplechoice' in request
.form
.keys():
111 g
.db
.execute('insert into votes (title, description, date_end, is_transparent, is_public, is_multiplechoice) values (?, ?, ?, ?, ?, ?)',
112 [request
.form
['title'], request
.form
['description'], ndate
, transparent
, public
, multiplechoice
])
114 flash('New entry was successfully posted')
115 return redirect(url_for('home'))
120 if __name__
== '__main__':