f749cc43107229300be7b02bbff5350b7ea3db0f
2 # -*- coding: utf-8 -*-
4 from flask
import Flask
, request
, session
, g
, redirect
, url_for
, abort
, \
7 from datetime
import date
, timedelta
8 from contextlib
import closing
10 locale
.setlocale(locale
.LC_ALL
, '')
12 DATABASE
= '/tmp/cavote.db'
13 SECRET_KEY
= '{J@uRKO,xO-PK7B,jF?>iHbxLasF9s#zjOoy=+:'
19 app
.config
.from_object(__name__
)
22 return sqlite3
.connect(app
.config
['DATABASE'])
29 def teardown_request(exception
):
34 return render_template('index.html')
36 def query_db(query
, args
=(), one
=False):
37 cur
= g
.db
.execute(query
, args
)
38 rv
= [dict((cur
.description
[idx
][0], value
)
39 for idx
, value
in enumerate(row
)) for row
in cur
.fetchall()]
40 return (rv
[0] if rv
else None) if one
else rv
43 with
closing(connect_db()) as db
:
44 with app
.open_resource('schema.sql') as f
:
45 db
.cursor().executescript(f
.read())
51 def valid_login(username
, password
):
52 return username
== app
.config
['USERNAME'] and password
== app
.config
['PASSWORD']
54 @app.route('/login', methods
=['GET', 'POST'])
57 if request
.method
== 'POST':
58 if valid_login(request
.form
['username'], request
.form
['password']):
59 session
['username'] = request
.form
['username']
60 if session
['username'] == 'admin':
61 session
['is_admin'] = True
62 flash('You were logged in')
63 return redirect(url_for('home'))
65 error
= "Invalid username/password"
66 return render_template('login.html', error
=error
)
70 session
.pop('username', None)
71 session
.pop('is_admin', None)
72 flash('You were logged out')
73 return redirect(url_for('home'))
77 @app.route('/user/settings/<username>')
78 def show_settings(username
):
79 if username
!= session
['username']:
90 @app.route('/votes/<votes>')
91 def show_votes(votes
):
94 votes
= query_db('select title, description, date_begin, date_end from votes order by id desc')
95 elif votes
== 'archive':
96 votes
= query_db('select title, description, date_begin, date_end from votes where date_end < (?) order by id desc', [today
])
97 elif votes
== 'current':
98 votes
= query_db('select title, description, date_begin, date_end from votes where date_end >= (?) order by id desc', [today
])
101 return render_template('show_votes.html', votes
=votes
)
106 @app.route('/votes/admin/new')
108 if not session
.get('is_admin'):
110 return render_template('new_vote.html')
112 @app.route('/votes/admin/add', methods
=['POST'])
114 if not session
.get('is_admin'):
116 date_begin
= date
.today()
117 date_end
= date
.today() + timedelta(days
=int(request
.form
['days']))
121 if 'transparent' in request
.form
.keys():
123 if 'public' in request
.form
.keys():
125 if 'multiplechoice' in request
.form
.keys():
127 g
.db
.execute('insert into votes (title, description, date_begin, date_end, is_transparent, is_public, is_multiplechoice) values (?, ?, ?, ?, ?, ?, ?)',
128 [request
.form
['title'], request
.form
['description'], date_begin
, date_end
, transparent
, public
, multiplechoice
])
130 flash('New entry was successfully posted')
131 return redirect(url_for('home'))
136 if __name__
== '__main__':