-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
105 lines (81 loc) · 2.84 KB
/
main.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
from flask import Flask, render_template, Response, request, jsonify, redirect
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
#Database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'db.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
#Init db
db = SQLAlchemy(app)
#Init marshmallow
ma = Marshmallow(app)
# Produc Class/Model
class Product(db.Model):
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String, unique = True)
stock = db.Column(db.String)
price = db.Column(db.Integer)
link = db.Column(db.String)
image = db.Column(db.String)
def __init__(self, name, stock, price, link, image):
self.name = name
self.stock = stock
self.price = price
self.link = link
self.image = image
#Product Schema
class ProductSchema(ma.Schema):
class Meta:
fields = ('id', 'name', 'stock', 'price', 'link', 'image')
#Init Schema
product_schema = ProductSchema()
proudcts_schemas = ProductSchema(many=True)
@app.route("/", methods=["POST", "GET"])
def home(product = None):
all_products = Product.query.all()
result = proudcts_schemas.dump(all_products)
return render_template('index.html', products=result)
@app.route('/product', methods=["Post"])
def add_product():
name = request.json['name']
stock = request.json['stock']
price = request.json['price']
link = request.json['link']
image = request.json['image']
if bool(db.session.query(Product).filter_by(name=name).first()):
product = db.session.query(Product).filter(Product.name==name).first()
return redirect(f'/product/{product.id}', code=307)
new_product = Product(name, stock, price, link, image)
db.session.add(new_product)
db.session.commit()
return product_schema.jsonify(new_product)
#Get All Items
@app.route('/product', methods=["GET"])
def get_products():
all_products = Product.query.all()
result = proudcts_schemas.dump(all_products)
return jsonify(result)
#Get An Items
@app.route('/product/<id>', methods=["GET"])
def get_product(id):
product = Product.query.get(id)
return product_schema.jsonify(product)
@app.route('/product/<id>', methods=["PUT", "POST"])
def update_product(id):
product = Product.query.get(id)
name = request.json['name']
stock = request.json['stock']
price = request.json['price']
link = request.json['link']
image = request.json['image']
product.name = name
product.stock = stock
product.price = price
product.link = link
product.image = image
db.session.commit()
return product_schema.jsonify(product)
if __name__ == "__main__":
app.run()