2 # -*- coding: utf-8 -*-
4 from flask
import Flask
, request
, session
, g
, redirect
, url_for
, abort
, \
7 from datetime
import date
, timedelta
9 DATABASE
= '/tmp/cavote.db'
10 SECRET_KEY
= '{J@uRKO,xO-PK7B,jF?>iHbxLasF9s#zjOoy=+:'
16 app
.config
.from_object(__name__
)
19 return sqlite3
.connect(app
.config
['DATABASE'])
26 def teardown_request(exception
):
29 @app.route('/admin/votes')
31 cur
= g
.db
.execute('select title, description, date from votes order by id desc')
32 votes
= [dict(title
=row
[0], description
=row
[1], date
=row
[2]) for row
in cur
.fetchall()]
33 return render_template('show_votes.html', votes
=votes
)
35 @app.route('/admin/vote/add', methods
=['POST'])
37 if not session
.get('logged_in'):
39 g
.db
.execute('insert into votes (title, description, date) values (?, ?, ?)',
40 [request
.form
['title'], request
.form
['description'], date
.today() + timedelta(days
=60)])
42 flash('New entry was successfully posted')
43 return redirect(url_for('show_votes'))
45 @app.route('/login', methods
=['GET', 'POST'])
48 if request
.method
== 'POST':
49 if request
.form
['username'] != app
.config
['USERNAME']:
50 error
= 'Invalid username'
51 elif request
.form
['password'] != app
.config
['PASSWORD']:
52 error
= 'Invalid password'
54 session
['logged_in'] = True
55 flash('You were logged in')
56 return redirect(url_for('show_votes'))
57 return render_template('login.html', error
=error
)
61 session
.pop('logged_in', None)
62 flash('You were logged out')
63 return redirect(url_for('show_votes'))
67 if __name__
== '__main__':