This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route("/products/form", methods=["GET", "POST"]) | |
def form(): | |
if request.method == "POST": | |
# request.form contains the product data dictionnary | |
new_product = product_schema.load(request.form) | |
add_product(new_product) | |
name = request.form.get("name") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Product: | |
id = 1 | |
def __init__(self, name, price, description="no description"): | |
self.id = Product.id | |
self.name = name | |
self.price = price | |
self.description = description | |
Product.id += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# serialize one instance with Marshmallow | |
product = Product(name="jambon", price=5, description="Le meilleur jambon") | |
serialized_product = product_schema.dump(product) | |
# serialize many instances with Marshmallow | |
serialized_products = products_schema.dump(products) | |
# deserialize a dict to an object (instance) with Marshmallow | |
product_dict_1 = {"name": "jambon", "price": 12.5, "description": "La meilleure pizza made in Italie"} | |
new_instance = product_schema.load(product_dict_1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProductSchema(Schema): | |
id = fields.Int() | |
name = fields.Str() | |
price = fields.Float() | |
description = fields.Str(allow_none=True) | |
@post_load | |
def make_product(self, data, **kwargs): | |
constructor_helper = {k: v for k, v in data.items() if v != None} | |
return Product(**constructor_helper) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
product_data = { | |
"name": "pates", | |
"price": 1.5, | |
} | |
response = requests.put(f"{BASE_URL}add_with_parser", data=product_data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
product_data = { | |
"name": "pates", | |
"price": 1.5, | |
} | |
response = requests.put(f"{BASE_URL}add_data", data=json.dumps(product_data)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
product_data = { | |
"name": "pates", | |
"price": 1.5, | |
} | |
response = requests.put(f"{BASE_URL}add_json", json=product_data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from my_app import app | |
if __name__ == "__main__": | |
app.run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from my_app import app | |
from flask import jsonify | |
from my_app.models import * | |
# only to test flask_restful | |
from flask_restful import fields, marshal_with, Resource, Api | |
api = Api(app) | |
product_1 = Product( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask_sqlalchemy import SQLAlchemy | |
from flask_marshmallow import Marshmallow | |
app = Flask(__name__) | |
# Load the configuration from the instance folder | |
app.config.from_pyfile('config.py', silent=True) | |
# Define the database object which is imported |