-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
120 lines (99 loc) · 3.53 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
from flask import Flask, render_template, request, flash
from models import db, Category, Product, ProductInfo
from config import BaseConfig
# Application definition
app = Flask(__name__)
# Application configuration
app.config.from_object(BaseConfig)
db.init_app(app)
# Creating database tables and columns
with app.app_context():
db.create_all()
# Application endpoints
@app.route("/", methods=["GET", "POST"])
def index():
categories = Category.query.all()
products = []
if request.method == "POST":
product_shops = (
ProductInfo.query.join(Product, ProductInfo.product == Product.id)
.join(Category, Product.category == Category.id)
.filter(Product.name.like("%{}%".format(request.form.get("name"))))
.filter(Category.id == int(request.form.get("category")))
.add_columns(
Product.id,
Category.name.label("category"),
Product.name.label("product"),
ProductInfo.source,
ProductInfo.source_type,
ProductInfo.price,
ProductInfo.url,
)
.order_by(ProductInfo.price)
.all()
)
products = product_shops
return render_template("index.html", categories=categories, products=products)
@app.route("/products")
def product_list():
products = (
Product.query.join(Category, Product.category == Category.id)
.add_columns(
Category.name.label("category"),
Product.name.label("product_name"),
Product.id,
)
.all()
)
return render_template("products.html", products=products)
@app.route("/add-product", methods=["GET", "POST"])
def add_product():
categories = Category.query.all()
if request.method == "POST":
product = Product(
name=request.form.get("name"),
category=int(request.form.get("category")),
description=request.form.get("description"),
)
db.session.add(product)
db.session.commit()
flash("Successfully saved product " + str(request.form.get("name")))
return render_template("add-product.html", categories=categories)
@app.route("/add-category", methods=["GET", "POST"])
def add_category():
if request.method == "POST":
name = request.form.get("name")
category = Category(name=name)
db.session.add(category)
db.session.commit()
flash("Successfully saved category")
return render_template("add-category.html")
@app.route("/products/<id>")
def get_product_info(id):
product = (
Product.query.join(Category, Product.category == Category.id)
.filter(Product.id == id)
.first()
)
return render_template("view-product.html", product=product)
@app.route("/product/<id>/add-info-source", methods=["GET", "POST"])
def add_source_info(id):
product = (
Product.query.join(Category, Product.category == Category.id)
.filter(Product.id == id)
.first()
)
if request.method == "POST":
product_info = ProductInfo(
source=request.form.get("name"),
product=id,
source_type=request.form.get("type"),
price=request.form.get("price"),
url=request.form.get("url"),
)
db.session.add(product_info)
db.session.commit()
flash("Successfully saved information")
return render_template("add-shop.html", product=product)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)