Archive / currentwq votes are separated
[cavote.git] / main.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from flask import Flask, request, session, g, redirect, url_for, abort, \
5 render_template, flash
6 import sqlite3
7 from datetime import date, timedelta
8 import locale
9 locale.setlocale(locale.LC_ALL, '')
10
11 DATABASE = '/tmp/cavote.db'
12 SECRET_KEY = '{J@uRKO,xO-PK7B,jF?>iHbxLasF9s#zjOoy=+:'
13 DEBUG = True
14 USERNAME = 'admin'
15 PASSWORD = 'admin'
16
17 app = Flask(__name__)
18 app.config.from_object(__name__)
19
20 def connect_db():
21 return sqlite3.connect(app.config['DATABASE'])
22
23 @app.before_request
24 def before_request():
25 g.db = connect_db()
26
27 @app.teardown_request
28 def teardown_request(exception):
29 g.db.close()
30
31 @app.route('/')
32 def home():
33 return render_template('index.html')
34
35
36 #----------------
37 # Login / Logout
38
39 @app.route('/login', methods=['GET', 'POST'])
40 def login():
41 error = None
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'
47 else:
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)
55
56 @app.route('/logout')
57 def logout():
58 session.pop('logged_in', None)
59 flash('You were logged out')
60 return redirect(url_for('home'))
61
62 #---------------
63 # User settings
64
65 #------------
66 # User admin
67
68
69 #------------
70 # Votes list
71
72 @app.route('/votes/<votes>')
73 def show_votes(votes):
74 today = date.today().strftime('%d %B %Y')
75 if votes == 'all':
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})
81 else:
82 abort(404)
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)
86
87 #-------------
88 # Votes admin
89
90 @app.route('/votes/admin/new')
91 def new_vote():
92 if not session.get('logged_in'):
93 abort(401)
94 return render_template('new_vote.html')
95
96 @app.route('/votes/admin/add', methods=['POST'])
97 def add_vote():
98 if not session.get('logged_in'):
99 abort(401)
100 daten = date.today() + timedelta(days=int(request.form['days']))
101 ndate = daten.strftime('%d %B %Y')
102 transparent = 0
103 public = 0
104 multiplechoice = 0
105 if 'transparent' in request.form.keys():
106 transparent = 1
107 if 'public' in request.form.keys():
108 public = 1
109 if 'multiplechoice' in request.form.keys():
110 multiplechoice = 1
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])
113 g.db.commit()
114 flash('New entry was successfully posted')
115 return redirect(url_for('home'))
116
117 #------
118 # Main
119
120 if __name__ == '__main__':
121 app.run()
122