Skip to content

Commit b30b2da

Browse files
committed
Completed task pcewebpython#1. Added a form for creating new donations, and created a route and handler function inside of 'main.py'.
1 parent fd02f85 commit b30b2da

File tree

6 files changed

+40
-16
lines changed

6 files changed

+40
-16
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
__pycache__/
22
.idea/
33
my_database.db
4+
.vscode

main.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,33 @@
33

44
from flask import Flask, render_template, request, redirect, url_for, session
55

6-
from model import Donation
6+
from model import Donation, Donor
77

88
app = Flask(__name__)
99

10+
1011
@app.route('/')
1112
def home():
1213
return redirect(url_for('all'))
1314

14-
@app.route('/donations/')
15+
16+
@app.route('/donations')
1517
def all():
1618
donations = Donation.select()
1719
return render_template('donations.jinja2', donations=donations)
18-
20+
21+
22+
@app.route('/create', methods=['GET', 'POST'])
23+
def create():
24+
if request.method == 'POST':
25+
donor = Donor.select().where(
26+
Donor.name == request.form['donor-name']).get()
27+
donation = Donation(value=request.form['donation-amount'], donor=donor)
28+
donation.save()
29+
return redirect(url_for('all'))
30+
return render_template('create.jinja2')
31+
1932

2033
if __name__ == "__main__":
2134
port = int(os.environ.get("PORT", 6738))
2235
app.run(host='0.0.0.0', port=port)
23-

model.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
db = connect(os.environ.get('DATABASE_URL', 'sqlite:///my_database.db'))
77

8-
class Donor(Model):
9-
name = CharField(max_length=255, unique=True)
108

9+
class BaseModel(Model):
1110
class Meta:
1211
database = db
1312

14-
class Donation(Model):
15-
value = IntegerField()
16-
donor = ForeignKeyField(Donor, backref='donations')
1713

18-
class Meta:
19-
database = db
14+
class Donor(BaseModel):
15+
name = CharField(max_length=255, unique=True)
16+
2017

18+
class Donation(BaseModel):
19+
value = IntegerField()
20+
donor = ForeignKeyField(Donor, backref='donations')

requirements.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ click==6.7
22
Flask==0.12.3
33
itsdangerous==0.24
44
Jinja2==2.10
5-
MarkupSafe==1.0
5+
MarkupSafe==1.1.1
66
peewee==3.2.2
7-
psycopg2-binary==2.7.4
7+
psycopg2-binary==2.8.5
88
Werkzeug==0.14.1

setup.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import random
22

3-
from model import db, Donor, Donation
3+
from model import db, Donor, Donation
44

55
db.connect()
66

@@ -22,5 +22,5 @@
2222
donors = [alice, bob, charlie]
2323

2424
for x in range(30):
25-
Donation(donor=random.choice(donors), value=random.randint(100, 10000)).save()
26-
25+
Donation(donor=random.choice(donors),
26+
value=random.randint(100, 10000)).save()

templates/create.jinja2

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% extends 'base.jinja2' %}
2+
3+
{% block subtitle %}Add a donation{% endblock subtitle %}
4+
5+
{% block content %}
6+
<form method="POST">
7+
<label for="donor-name-input">Donor name:</label><input id="donor-name-input" type="text" name="donor-name"><br>
8+
<label for="donation-amount-input">Donation amount:</label><input id="donation-amount-input" type="number" name="donation-amount" min="1"><br>
9+
<input type="submit" value="Add Donation">
10+
</form>
11+
{% endblock content %}

0 commit comments

Comments
 (0)