Exemple #1
0
    def post(self):
        product_data = ProductSchema().load(request.json)
        quantity = product_data.pop("quantity", None)
        shop_id = product_data.pop("shop_id", None)
        product = Product(**product_data)

        try:
            product.save()
        except:
            return {"message": ERROR_INSERTING_PRODUCT}, 500

        inventory_data = {
            "product_id": product.id,
            "quantity": quantity,
            "shop_id": shop_id
        }

        inventory = Inventory(**inventory_data)

        try:
            inventory.save()
        except:
            return {"message": ERROR_INSERTING_INVENTORY}, 500

        return {"id": str(product.id)}, 201
Exemple #2
0
def create_product():
    data = request.get_json()
    product_schema = ProductSchema()
    product = product_schema.load(data)
    db.session.add(product)
    db.session.commit()
    return make_response(jsonify({"success": True}), 200)
Exemple #3
0
def update_product(product_id):
    data = request.get_json()
    get_product = Product.query.get(product_id)
    if data.get('name'):
        get_product.name = data['name']
    if data.get('description'):
        get_product.description = data['description']
    if data.get('price'):
        get_product.price = data['price']
    if data.get('stock'):
        get_product.price = data['stock']
    db.session.add(get_product)
    db.session.commit()
    product_schema = ProductSchema()
    product = product_schema.dump(get_product)
    return make_response(jsonify({"product": product}))
Exemple #4
0
    def get(product_id):
        try:
            product = Product.objects(product_id=product_id)[0]
            product.views += 1
            product.save()
            return ProductSchema().dump(product)

        except IndexError:
            return []
Exemple #5
0
    def get(category_id):
        try:
            category = Category.objects(category_id=category_id)[0]
            products = Product.objects(category=category.id)
            for product in products:
                product.views += 1
                product.save()
            return ProductSchema(many=True).dump(products)

        except IndexError:
            return []
Exemple #6
0
def get_all_products():
    get_products = Product.query.all()
    product_schema = ProductSchema(many=True)
    products = product_schema.dump(get_products)

    return make_response(jsonify({"products": products}))
Exemple #7
0
def get_one_product(product_id):
    get_product = Product.query.get(product_id)
    product_schema = ProductSchema()
    product = product_schema.dump(get_product)
    return make_response(jsonify({"product": product}))
Exemple #8
0
from flask_restful import Resource
from flask import request
from werkzeug.security import safe_str_cmp

from flask_jwt_extended import create_access_token, create_refresh_token, jwt_required
from models.product import ProductModel
from schemas.product import ProductSchema, ProductDetailsSchema

product_schema = ProductSchema()
product_details_schema = ProductDetailsSchema()


class ProductRes(Resource):
    @classmethod
    @jwt_required()
    def post(cls):
        product_json = request.get_json()
        product_data = product_schema.load(product_json)
        product = ProductModel.get_product_by_id(product_data.product_id)

        if product:
            product.product_name = product.product_name.rstrip()
            discount = ((product.mrp - product.sale_price) / 100) * 100
            product.discount = discount
            return product_details_schema.dump(product), 200

        return {"message": "Cannot find product"}, 404
Exemple #9
0
def route_product_get_all():
    products = Product.query.all
    return jsonify({"products": [i.as_dict() for i in products()]})


@app.route("/products/<int:product_id>", methods=["GET"])
def route_product_get_by_id(product_id):
    product = Product.query.filter(Product.id == product_id).first()
    if not product:
        return abort(
            400, "The product with id: {0} does not exists".format(product_id))
    return jsonify(product.as_dict())


@app.route("/products", methods=["POST"])
@marshal_with(ProductSchema())
@use_kwargs({
    "name": fields.Str(),
    "description": fields.Str(),
    "company_id": fields.Int(),
})
def route_product_create(name, description, company_id):
    company = Company.query.filter(Company.id == company_id).first()
    product = Product(name=name, description=description, company=company)
    db.session.add(product)
    db.session.commit()
    db.session.refresh(product)
    return product


@app.route("/products/<int:product_id>", methods=["PUT"])