sak1b

flask v2

Apr 8th, 2021
516
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from flask import Flask, request, jsonify, send_from_directory
  2. from flask_sqlalchemy import SQLAlchemy
  3. from flask_marshmallow import Marshmallow
  4. import uuid
  5. from werkzeug.utils import secure_filename
  6. import os
  7.  
  8.  
  9. app = Flask(__name__)
  10. basedir = os.path.abspath(os.path.dirname(__file__))
  11.  
  12. UPLOAD_FOLDER = os.path.join(basedir,'uploads')
  13. ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'csv'}
  14.  
  15. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'db.sqlite')
  16. app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  17. app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  18.  
  19.  
  20.  
  21. db = SQLAlchemy(app)
  22.  
  23. ma= Marshmallow(app)
  24.  
  25. # @app.route('/', methods=['GET'])
  26. # def get():
  27. #   return jsonify({'msg': 'hello world!'})
  28.  
  29.  
  30. class Product(db.Model):
  31.     id = db.Column(db.Integer, primary_key=True)
  32.     name = db.Column(db.String(100), unique=True)
  33.     description = db.Column(db.String(200))
  34.     price = db.Column(db.Float)
  35.     qty = db.Column(db.Integer)
  36.  
  37.     def __init__(self, name, description, price, qty):
  38.         self.name = name
  39.         self.description = description
  40.         self.price = price
  41.         self.qty = qty
  42.  
  43.     def __repr__(self):
  44.         return "Product, Name: %s and desc: %s" % (self.name, self.description)
  45.  
  46.  
  47. class ProductSchema(ma.Schema):
  48.     class Meta:
  49.           fields = ('id','name','description','price','qty')
  50.  
  51.  
  52. product_schema = ProductSchema()
  53. products_schema = ProductSchema(many=True)
  54.  
  55. @app.route('/product', methods=['POST'])
  56. def add_product():
  57.  
  58.     name=request.json.get('name')
  59.     description = request.json.get('description')
  60.     price=request.json.get('price')
  61.     qty=request.json.get('qty')
  62.  
  63.     new_product = Product(name=name, description=description, price=price, qty=qty)
  64.  
  65.     db.session.add(new_product)
  66.     db.session.commit()
  67.  
  68.     return product_schema.jsonify(new_product)
  69.  
  70. @app.route('/product', methods=['GET'])
  71. def get_products():
  72.     all_products = Product.query.all()
  73.     result = products_schema.dump(all_products)
  74.     print(result)
  75.     return jsonify(result)
  76.  
  77.  
  78. @app.route('/product/<string:id>', methods=['GET'])
  79. def get_product(id):
  80.     print(UPLOAD_FOLDER)
  81.     product = Product.query.get(id)
  82.     return product_schema.jsonify(product)
  83.  
  84.  
  85. @app.route('/product/<string:id>', methods=['PUT'])
  86. def update_product(id):
  87.  
  88.     product = Product.query.get(id)
  89.    
  90.     name=request.json.get('name')
  91.     description = request.json.get('description')
  92.     price=request.json.get('price')
  93.     qty=request.json.get('qty')
  94.  
  95.     product.name = name
  96.     product.description = description
  97.     product.price = price
  98.     product.qty = qty
  99.  
  100.     db.session.commit()
  101.  
  102.     return product_schema.jsonify(product)
  103.  
  104. @app.route('/product/<string:id>', methods=['DELETE'])
  105. def delete_product(id):
  106.     product = Product.query.get(id)
  107.     db.session.delete(product)
  108.     db.session.commit()
  109.  
  110.     product = Product.query.get(id)
  111.     return product_schema.jsonify(product)
  112.  
  113. def allowed_file(filename):
  114.     return '.' in filename and \
  115.            filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  116.  
  117. @app.route('/upload', methods=['POST'])
  118. def upload_file():
  119.     status = "ok"
  120.     random_id = uuid.uuid1()
  121.     if request.method == 'POST':
  122.         if 'file' not in request.files:
  123.             return jsonify({"status": "no file attached!"})
  124.        
  125.         file = request.files['file']
  126.  
  127.         if file.filename == '':
  128.             return jsonify({"status": "no file name found!"})
  129.  
  130.         if not allowed_file(file.filename):
  131.             return jsonify({"status": "file format not supported!"})
  132.  
  133.         if file and allowed_file(file.filename):
  134.             filename = secure_filename(file.filename)
  135.             file.save(os.path.join(app.config['UPLOAD_FOLDER'], str(random_id)+filename))
  136.             return jsonify({"status": "file uploaded!"})
  137.    
  138.     return jsonify({"status": status})
  139.  
  140.  
  141. @app.route('/download',  methods=['GET'])
  142. def download_file():
  143.     filename = 'target_data_bulk.csv'
  144.     return send_from_directory(directory=app.config['UPLOAD_FOLDER'], filename=filename)
  145.  
  146.  
  147. if __name__ == '__main__':
  148.     app.run(debug=True)
  149.  
  150.  
  151.  
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.

×