-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
137 lines (101 loc) · 4.38 KB
/
app.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
133
134
135
136
137
"""Main application file.
"""
from flask import Flask, render_template, request, redirect, url_for, flash
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.engine import Engine
from sqlalchemy import event, exc
app = Flask(__name__)
app.secret_key = "secret_key"
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///C:\\sqlite\\Databases\\python_crud_app.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"]= False
db = SQLAlchemy(app)
class InventoryItem(db.Model):
"""ORM for Inventory Items.
"""
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True, nullable=False)
quantity = db.Column(db.Integer, nullable=False)
warehouse_id = db.Column(db.Integer, db.ForeignKey('warehouse.id'), nullable=True)
class Warehouse(db.Model):
"""ORM for Warehouses. One-to-many with Inventory Items.
"""
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True, nullable=False)
inventory_items = db.relationship('InventoryItem', backref='warehouse')
@app.route('/')
def Index():
inventory_data = InventoryItem.query.all()
warehouse_data = Warehouse.query.all()
return render_template("index.html", inventory_data=inventory_data, warehouse_data=warehouse_data)
@app.route('/insert_item', methods=['POST'])
def insert_item():
if request.method == 'POST':
name = request.form['name']
quantity = request.form['quantity']
warehouse_id = request.form['warehouse_id'] or None
data = InventoryItem(name=name, quantity=quantity, warehouse_id=warehouse_id)
try:
db.session.add(data)
db.session.commit()
flash("Inserted Inventory Item Successfully", 'success')
except exc.IntegrityError:
flash("Failed to Insert Inventory Item", 'danger') # generic error for DB API errors (e.g. foreign key constraint)
finally:
return redirect(url_for('Index'))
@app.route('/update_item', methods=['GET', 'POST'])
def update_item():
if request.method == 'POST':
data = InventoryItem.query.get(request.form.get('id')) # the hidden ID from the form
data.name = request.form['name']
data.quantity = request.form['quantity']
data.warehouse_id = request.form['warehouse_id']
try:
db.session.commit()
flash("Updated Inventory Item Successfully", 'success')
except exc.IntegrityError as e:
flash("Failed to Update Inventory Item", 'danger') # generic error for DB API errors (e.g. foreign key constraint)
finally:
return redirect(url_for('Index'))
@app.route('/delete_item/<id>/', methods=['GET', 'POST'])
def delete_item(id):
data = InventoryItem.query.get(id)
db.session.delete(data)
db.session.commit()
flash("Deleted Inventory Item Successfully", 'success')
return redirect(url_for('Index'))
@app.route('/insert_warehouse', methods=['POST'])
def insert_warehouse():
if request.method == 'POST':
name = request.form['name']
data = Warehouse(name=name)
db.session.add(data)
db.session.commit()
flash("Inserted Warehouse Successfully", 'success')
return redirect(url_for('Index'))
@app.route('/update_warehouse', methods=['GET', 'POST'])
def update_warehouse():
if request.method == 'POST':
data = Warehouse.query.get(request.form.get('id')) # the hidden ID from the form
data.name = request.form['name']
db.session.commit()
flash("Updated Warehouse Successfully", 'success')
return redirect(url_for('Index'))
@app.route('/delete_warehouse/<id>/', methods=['GET', 'POST'])
def delete_warehouse(id):
data = Warehouse.query.get(id)
db.session.delete(data)
db.session.commit()
flash("Deleted Warehouse Successfully", 'success')
return redirect(url_for('Index'))
@event.listens_for(Engine, "connect")
def set_sqlite_pragma(dbapi_connection, connection_record):
"""Change the foreign keys setting for SQLite to enforce foreign key constraints.
By default, foreign keys SQLite do not affect the operation of the tables.
"""
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA foreign_keys=ON")
cursor.close()
if __name__ == "__main__":
db.drop_all() # clear db tables for development purposes
db.create_all() # create database tables
app.run(host="0.0.0.0")