Skip to content

Instantly share code, notes, and snippets.

@ciscoinfo
ciscoinfo / views.py
Created September 8, 2021 14:28
article Flask (Python) : lire une requête PUT / POST - views form
@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")
@ciscoinfo
ciscoinfo / models.py
Created September 8, 2021 14:24
article Flask (Python) : lire une requête PUT / POST - models 2
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
@ciscoinfo
ciscoinfo / test_marshmallow_serialize.py
Created September 8, 2021 14:06
article Flask (Python) : lire une requête PUT / POST - marsh 1
# 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)
@ciscoinfo
ciscoinfo / models.py
Created September 8, 2021 14:01
article Flask (Python) : lire une requête PUT / POST - models 1
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)
@ciscoinfo
ciscoinfo / test_put_requests.py
Created September 8, 2021 12:20
article Flask (Python) : lire une requête PUT / POST - views.py - put3
product_data = {
"name": "pates",
"price": 1.5,
}
response = requests.put(f"{BASE_URL}add_with_parser", data=product_data)
@ciscoinfo
ciscoinfo / test_put_requests.py
Created September 8, 2021 12:10
article Flask (Python) : lire une requête PUT / POST - views.py - put1
product_data = {
"name": "pates",
"price": 1.5,
}
response = requests.put(f"{BASE_URL}add_data", data=json.dumps(product_data))
@ciscoinfo
ciscoinfo / test_put_requests.py
Last active September 8, 2021 12:17
article Flask (Python) : lire une requête PUT / POST - views.py - put1
product_data = {
"name": "pates",
"price": 1.5,
}
response = requests.put(f"{BASE_URL}add_json", json=product_data)
@ciscoinfo
ciscoinfo / run.py
Created September 5, 2021 16:06
attached files for my article : Flask (Python) : lire une requête GET
from my_app import app
if __name__ == "__main__":
app.run()
@ciscoinfo
ciscoinfo / views.py
Last active September 4, 2021 14:51
file views.py for my article "API REST avec Flask"
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(
@ciscoinfo
ciscoinfo / __init__.py
Last active September 4, 2021 15:04
files for my article "API REST avec Flask"
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