sak1b

flask-rest-api-product-demo-app-1

Apr 7th, 2021
361
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from flask import Flask, request, jsonify
  2. from flask_sqlalchemy import SQLAlchemy
  3. from flask_marshmallow import Marshmallow
  4. import os
  5.  
  6.  
  7. app = Flask(__name__)
  8. basedir = os.path.abspath(os.path.dirname(__file__))
  9.  
  10. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'db.sqlite')
  11. app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  12.  
  13. db = SQLAlchemy(app)
  14.  
  15. ma= Marshmallow(app)
  16.  
  17. # @app.route('/', methods=['GET'])
  18. # def get():
  19. #   return jsonify({'msg': 'hello world!'})
  20.  
  21.  
  22. class Product(db.Model):
  23.     id = db.Column(db.Integer, primary_key=True)
  24.     name = db.Column(db.String(100), unique=True)
  25.     description = db.Column(db.String(200))
  26.     price = db.Column(db.Float)
  27.     qty = db.Column(db.Integer)
  28.  
  29.     def __init__(self, name, description, price, qty):
  30.         self.name = name
  31.         self.description = description
  32.         self.price = price
  33.         self.qty = qty
  34.  
  35.     def __repr__(self):
  36.         return "Product, Name: %s and desc: %s" % (self.name, self.description)
  37.  
  38.  
  39. class ProductSchema(ma.Schema):
  40.     class Meta:
  41.           fields = ('id','name','description','price','qty')
  42.  
  43.  
  44. product_schema = ProductSchema()
  45. products_schema = ProductSchema(many=True)
  46.  
  47. @app.route('/product', methods=['POST'])
  48. def add_product():
  49.  
  50.     name=request.json.get('name')
  51.     description = request.json.get('description')
  52.     price=request.json.get('price')
  53.     qty=request.json.get('qty')
  54.  
  55.     new_product = Product(name=name, description=description, price=price, qty=qty)
  56.  
  57.     db.session.add(new_product)
  58.     db.session.commit()
  59.  
  60.     return product_schema.jsonify(new_product)
  61.  
  62. @app.route('/product', methods=['GET'])
  63. def get_products():
  64.     all_products = Product.query.all()
  65.     result = products_schema.dump(all_products)
  66.     print(result)
  67.     return jsonify(result)
  68.  
  69.  
  70. @app.route('/product/<string:id>', methods=['GET'])
  71. def get_product(id):
  72.     product = Product.query.get(id)
  73.     return product_schema.jsonify(product)
  74.  
  75.  
  76. @app.route('/product/<string:id>', methods=['PUT'])
  77. def update_product(id):
  78.  
  79.     product = Product.query.get(id)
  80.    
  81.     name=request.json.get('name')
  82.     description = request.json.get('description')
  83.     price=request.json.get('price')
  84.     qty=request.json.get('qty')
  85.  
  86.     product.name = name
  87.     product.description = description
  88.     product.price = price
  89.     product.qty = qty
  90.  
  91.     db.session.commit()
  92.  
  93.     return product_schema.jsonify(product)
  94.  
  95. @app.route('/product/<string:id>', methods=['DELETE'])
  96. def delete_product(id):
  97.     product = Product.query.get(id)
  98.     db.session.delete(product)
  99.     db.session.commit()
  100.  
  101.     product = Product.query.get(id)
  102.     return product_schema.jsonify(product)
  103.  
  104. if __name__ == '__main__':
  105.     app.run(debug=True)
  106.  
  107.  
  108.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×