-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
149 lines (127 loc) · 5.42 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
138
139
140
141
142
143
144
145
146
147
148
149
from os import name
from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.db" # relative path
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
# Create model for Products and Warehouses with the following attributes.
# Warehouse and Product are in one-to-many relationship
class Warehouse(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
def __repr__(self):
return self.name
class Products(db.Model):
id = db.Column(db.Integer, primary_key=True)
product = db.Column(db.String(100), nullable=False) # must have a name
quantity = db.Column(db.Integer, default=1)
comments = db.Column(db.String(200))
ware_id = db.Column(db.Integer, db.ForeignKey("warehouse.id"))
warehouse = db.relationship('Warehouse', backref=db.backref('products', lazy = 'dynamic'))
def __init__(self, product, warehouse ):
self.product = product
self.warehouse = warehouse
def __repr__(self):
return '<Products %r>' % self.product
# Creates the SQLite database (db).
@app.before_first_request
def create_table():
db.create_all()
@app.route("/", methods=["POST", "GET"])
def index():
columns = [column.name for column in Products.__mapper__.columns if column.name != "id" and column.name != "ware_id"]
columns.append('warehouse')
# Add new product to the db
if request.method == "POST":
new_warehouse_name = request.form['warehouse']
new_warehouse = Warehouse.query.filter_by(name = str(new_warehouse_name)).first()
# if not specified or the warehouse user input does not exist, warehouse will be "None", we can update this attr later
new_task = Products(product = request.form['product'], warehouse= new_warehouse)
for column in columns:
if column == 'warehouse' or column == 'product':
continue
else:
setattr(new_task, column, request.form[column])
try:
db.session.add(new_task)
db.session.commit()
return redirect("/")
except:
return "There was an issue creating this product"
# List all.
else:
products = Products.query.all()
warehouse = Warehouse.query.all()
return render_template("index.html", products=products, columns=columns, warehouse = warehouse)
@app.route("/manageWare", methods=["POST", "GET"])
def manageWare():
if request.method == "POST":
# This is to add new warehouses
new_task = Warehouse(name = request.form['Warehouse Name'])
try:
db.session.add(new_task)
db.session.commit()
return redirect("/manageWare")
except:
return "There was an issue creating this warehouse"
# List all.
else:
warehouse = Warehouse.query.all()
return render_template("manageWare.html", warehouse = warehouse)
@app.route("/manageWare/delete/<int:id>")
def deleteWare(id):
# Delete warehouse with given id value from the db.
warehouse_to_delete = Warehouse.query.get_or_404(id)
try:
db.session.delete(warehouse_to_delete)
db.session.commit()
return redirect("/manageWare")
except:
return "There was an issue deleting this warehouse"
@app.route("/manageWare/rename/<int:id>", methods=["POST", "GET"])
def rename(id):
warehouse_to_rename = Warehouse.query.get_or_404(id)
# Rename this warehouse with given id
if request.method == "POST":
setattr(warehouse_to_rename, "name", request.form['Warehouse Name'])
print(warehouse_to_rename, warehouse_to_rename.name)
try:
db.session.commit()
return redirect("/manageWare")
except:
return "There was an issue rename this warehouse"
else:
return render_template("rename.html", house=warehouse_to_rename)
@app.route("/delete/<int:id>")
def delete(id):
# Delete product with given id value from the db.
product_to_delete = Products.query.get_or_404(id)
try:
db.session.delete(product_to_delete)
db.session.commit()
return redirect("/")
except:
return "There was an issue deleting that product"
@app.route("/update/<int:id>", methods=["POST", "GET"])
def update(id):
columns = [column.name for column in Products.__mapper__.columns if column.name != "id" and column.name != "ware_id"]
columns.append('warehouse')
product_to_update = Products.query.get_or_404(id)
# Update the info of a product.
if request.method == "POST":
for column in columns:
if column == 'warehouse':
new_warehouse_name = request.form[column]
new_warehouse = Warehouse.query.filter_by(name = str(new_warehouse_name)).first()
# if not specified or the warehouse user input does not exist, warehouse will be "None", we can update this attr later
product_to_update.warehouse = new_warehouse
else:
setattr(product_to_update, column, request.form[column])
try:
db.session.commit()
return redirect("/")
except:
return "There was an issue updating that product"
else:
return render_template("update.html", product=product_to_update, columns=columns)