forked from pcewebpython/flask-mailroom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
109 lines (91 loc) · 4.23 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import base64
from flask import Flask, render_template, request, redirect, url_for, session
from passlib.hash import pbkdf2_sha256
from peewee import CharField
from model import Donation, Donor, User
app = Flask(__name__)
#app.secret_key = b'\x9d\xb1u\x08%\xe0\xd0p\x9bEL\xf8JC\xa3\xf4J(hAh\xa4\xcdw\x12S*,u\xec\xb8\xb8'
app.secret_key = os.environ.get('SECRET_KEY').encode()
@app.route('/')
def home():
return redirect(url_for('all'))
@app.route('/donations/')
def all():
# displays a list of donors and thier donations
donations = Donation.select()
return render_template('donations.jinja2', donations=donations)
@app.route('/create/', methods=['GET', 'POST'])
def create():
# Creates a donation record
if 'username' not in session:
# requires user to be signed in to create a donation record
return redirect(url_for('login'))
if request.method == 'GET':
# render the html page
return render_template('create.jinja2')
if request.method == 'POST':
donorName = str.capitalize(request.form['name'])
if donorName == '':
# no donor name entered, returns to create with an input error message
return render_template('create.jinja2', error="No name was entered, please enter the donor's name.")
elif (Donor.select().where(Donor.name == donorName)):
# set donor to donor from list
donor = Donor.select().where(Donor.name==donorName).get()
else:
# creates a new donor
donor = Donor(name=donorName)
donor.save()
try:
# creates the donation record and return user to all
donation = Donation(donor=donor, value=int(request.form['amount']))
donation.save()
return redirect(url_for('all'))
except (AttributeError, ValueError):
# no or incorrect donation type entered, returns to create with an input error message
return render_template('create.jinja2', error="Donation amount requires a whole number, please try again.")
else:
return render_template('create.jinja2')
@app.route('/login/', methods=['GET', 'POST'])
def login():
# provides a login screen for users
if request.method == 'POST':
try:
# checks the user to the list of authorized users
user = User.select().where(User.name == request.form['name']).get()
except User.DoesNotExist:
# if user is not on the list, set it to none
user = None
if user and pbkdf2_sha256.verify(request.form['password'], user.password):
# if user exists, verify the users password
session['username'] = request.form['name']
# redirect to creat doantion if credentials are valid
return redirect(url_for('create'))
# incorect username or password, user is redirected back to login page with error message
return render_template('login.jinja2', error="Incorrect username or password.")
else:
return render_template('login.jinja2')
@app.route('/single/', methods=['GET', 'POST']) # Option 3: create all donations for a single donor
def single(): # may want to rename
if request.method == 'GET':
# render the page
return render_template('single.jinja2')
if request.method == 'POST':
try:
# checks if the donor is in the list of donors
donor = Donor.select().where(Donor.name == str.capitalize(request.form['name'])).get()
except Donor.DoesNotExist:
donor = None
# If incorrect donor name is entered, user is redirected back to page with error message
return render_template('single.jinja2', error="That name was not found, please enter a donor's name.")
if donor:
# copiles an list of donations by the selected donor and displays them
donations = Donation.select().where(Donation.donor == donor)
return render_template('single.jinja2', donations=donations)
else:
return redirect(url_for('all'))
else:
return redirect(url_for('all'))
if __name__ == "__main__":
port = int(os.environ.get("PORT", 6738))
app.run(host='0.0.0.0', port=port)