Skip to content

Instantly share code, notes, and snippets.

@kelvinndmo
Created April 28, 2019 17:30
Show Gist options
  • Save kelvinndmo/b4aa57678d4e3f3dba2ea02aad9ecf3c to your computer and use it in GitHub Desktop.
Save kelvinndmo/b4aa57678d4e3f3dba2ea02aad9ecf3c to your computer and use it in GitHub Desktop.
import os
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
# init app
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)
# product class/model
class Product(db.Model):
_id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
description = db.Column(db.String(200))
price = db.Column(db.Float)
qty = db.Column(db.Integer)
def __init__(self, name, description, price, qty):
self.name = name
self.description = description
self.price = price
self.qty = qty
class ProductSchema(ma.Schema):
class Meta:
fields = ('_id', 'name', 'description', 'price', 'qty')
# init Schema
product_schema = ProductSchema(strict=True)
products_schema = ProductSchema(strict=True, many=True)
# create_product:
@app.route('/product', methods=['POST'])
def add_product():
name = request.json['name']
description = request.json['description']
price = request.json['price']
qty = request.json['qty']
new_product = Product(name, description, price, qty)
db.session.add(new_product)
db.session.commit()
return product_schema.jsonify(new_product)
@app.route('/', methods=['GET'])
def get():
return jsonify({'name': 'kelvin onkundi'})
# Run sever
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment