-
Notifications
You must be signed in to change notification settings - Fork 0
/
adoption_site.py
132 lines (89 loc) · 3.15 KB
/
adoption_site.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os
from forms import AddForm, DelForm, AddOwnerForm
from flask import Flask, render_template, url_for, redirect
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
# Key for Forms
app.config['SECRET_KEY'] = 'mysecretkey'
############################################
# SQL DATABASE
##########################################
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \
os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
Migrate(app, db)
############################################
# MODELS
##########################################
class Puppy(db.Model):
__tablename__ = 'puppies'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text)
owner = db.relationship('Owner', backref='puppy', uselist=False)
def __init__(self, name):
self.name = name
def __repr__(self):
if self.owner:
return f"Puppy name is {self.name} and owner is {self.owner.name}"
else:
return f"Puppy name is {self.name} and has no owner assigned yet."
class Owner(db.Model):
__tablename__ = 'owners'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text)
# We use puppies.id because __tablename__='puppies'
puppy_id = db.Column(db.Integer, db.ForeignKey('puppies.id'))
def __init__(self, name, puppy_id):
self.name = name
self.puppy_id = puppy_id
def __repr__(self):
return f"Owner Name: {self.name}"
############################################
# CONTROLER (TO GO TO VIEWS WITH FORMS)
##########################################
@app.route('/')
def index():
return render_template('index.html')
@app.route('/add', methods=['GET', 'POST'])
def add_pup():
form = AddForm()
if form.validate_on_submit():
name = form.name.data
# Add new Puppy to database
new_pup = Puppy(name)
db.session.add(new_pup)
db.session.commit()
return redirect(url_for('list_pup'))
return render_template('add.html', form=form)
@app.route('/add_owner', methods=['GET', 'POST'])
def add_owner():
form = AddOwnerForm()
if form.validate_on_submit():
name = form.name.data
pup_id = form.pup_id.data
# Add new owner to database
new_owner = Owner(name, pup_id)
db.session.add(new_owner)
db.session.commit()
return redirect(url_for('list_pup'))
return render_template('add_owner.html', form=form)
@app.route('/list')
def list_pup():
# Grab a list of puppies from database. Puppy is a table.
puppies = Puppy.query.all()
return render_template('list.html', puppies=puppies)
@app.route('/delete', methods=['GET', 'POST'])
def del_pup():
form = DelForm()
if form.validate_on_submit():
id = form.id.data
pup = Puppy.query.get(id)
db.session.delete(pup)
db.session.commit()
return redirect(url_for('list_pup'))
return render_template('delete.html', form=form)
if __name__ == '__main__':
app.run(debug=True)