77310af83a72ef8ea984d351567672c88d33c15f
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 def valid_login(username
, password
):
40 return username
== app
.config
['USERNAME'] and password
== app
.config
['PASSWORD']
42 @app.route('/login', methods
=['GET', 'POST'])
45 if request
.method
== 'POST':
46 if valid_login(request
.form
['username'], request
.form
['password']):
47 session
['username'] = request
.form
['username']
48 if session
['username'] == 'admin':
49 session
['is_admin'] = True
50 flash('You were logged in')
51 return redirect(url_for('home'))
53 error
= "Invalid username/password"
54 return render_template('login.html', error
=error
)
58 session
.pop('username', None)
59 session
.pop('is_admin', None)
60 flash('You were logged out')
61 return redirect(url_for('home'))
65 @app.route('/user/settings/<username>')
66 def show_settings(username
):
67 if username
!= session
['username']:
78 @app.route('/votes/<votes>')
79 def show_votes(votes
):
82 cur
= g
.db
.execute('select title, description, date_begin, date_end from votes order by id desc')
83 elif votes
== 'archives':
84 cur
= g
.db
.execute('select title, description, date_begin, date_end from votes where date_end < (?) order by id desc', [today
])
85 elif votes
== 'currently':
86 cur
= g
.db
.execute('select title, description, date_begin, date_end from votes where date_end >= (?) order by id desc', [today
])
89 votes
= [dict(title
=row
[0], description
=row
[1], date_begin
=row
[2], date_end
=row
[3],
90 pourcent
=60) for row
in cur
.fetchall()]
91 return render_template('show_votes.html', votes
=votes
)
96 @app.route('/votes/admin/new')
98 if not session
.get('is_admin'):
100 return render_template('new_vote.html')
102 @app.route('/votes/admin/add', methods
=['POST'])
104 if not session
.get('is_admin'):
106 date_begin
= date
.today()
107 date_end
= date
.today() + timedelta(days
=int(request
.form
['days']))
111 if 'transparent' in request
.form
.keys():
113 if 'public' in request
.form
.keys():
115 if 'multiplechoice' in request
.form
.keys():
117 g
.db
.execute('insert into votes (title, description, date_begin, date_end, is_transparent, is_public, is_multiplechoice) values (?, ?, ?, ?, ?, ?, ?)',
118 [request
.form
['title'], request
.form
['description'], date_begin
, date_end
, transparent
, public
, multiplechoice
])
120 flash('New entry was successfully posted')
121 return redirect(url_for('home'))
126 if __name__
== '__main__':