forked from rice-apps/petition-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
petition.py
61 lines (48 loc) · 1.78 KB
/
petition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from flask import Flask, render_template, flash, redirect, url_for, session
from flask_login import LoginManager, login_required, request, login_user
import MySQLdb
login_manager = LoginManager()
app = Flask(__name__)
app.debug = True
login_manager.init(app)
db = MySQLdb.connect(host = 'localhost',
user = 'Xiaoyu Chen',
passwd = "petitions",
db = "petitions")
cur = db.cursor()
@app.route('/')
def show_main_page():
#substitute the following file name with your template file name
return render_template('PETITION_TEMPLATE.html')
@app.route('/petitions')
@login_required
def show_petitions():
petitions_name = []
petitions_description = []
petitions_start_date = []
petitions_end_date = []
cur.execute('select * from petitions')
for p_name in cur[2]:
petitions_name.extend(p_name)
for p_descript in cur[3]:
petitions_description.extend(p_descript)
for p_start in cur[4]:
petitions_start_date.extend(p_start)
for p_end in cur(5):
petitions_end_date.extend(p_end)
posts_info = [petitions_name, petitions_description, petitions_start_date, petitions_end_date]
return render_template('PETITION_TEMPLATE.html', posts_info = posts_info)
@app.route('/login', methods = ['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != app.config['USERNAME']:
error = 'Invalid username'
elif request.form['password'] != app.config['PASSWORD']:
error = 'Invalid password'
else:
session['logged_in'] = True
flash('You were logged in')
return redirect(url_for('show_petitions'))
return render_template('login.html', error=error)
app.run()