def get(self, id): product = Product.query.filter_by(id=id).first() if product is not None: product_schema = ProductSchema() response_obj = { "success": True, "data": product_schema.dump(product) } return response_obj else: response_obj = {"success": False, "message": "No such Product"} return make_response(jsonify(response_obj), 404)
def put(self, id): product = Product.query.filter_by(id=id).first() if product is not None: post_data = request.get_json(force=True) for key in post_data: setattr(product, key.lower(), post_data.get(key.lower())) db.session.commit() product_schema = ProductSchema() response_obj = { "success": True, "data": product_schema.dump(product) } return response_obj else: response_obj = {"success": False, "message": "No such Product"} return make_response(jsonify(response_obj), 404)
def get(self, model: str): user_id = get_jwt_identity() user = User.query.filter_by(id=user_id).first() args_query = request.args if len(args_query) == 0: return abort(400) if model.lower() == "product": schema = ProductSchema(many=True) query = f"%{args_query['name']}%" results = pagination.paginate(Product.query.filter(Product.name.like(query)), schema) elif model.lower() == "user": if user.id is None: return abort(401) elif not user.admin: return abort(403) schema = UserSchema(many=True, only=("email", "username", "email_verified", "active")) query = f"%{args_query['username']}%" results = pagination.paginate(User.query.filter(User.username.like(query)), schema) else: return abort(404) response_obj = { "success": True, "data": { "results": results } } return response_obj
def post(self): user_identifier = get_jwt_identity() user = User.query.filter_by(id=user_identifier).first() post_data = request.get_json(force=True) product = Product.query.filter_by(name=post_data.get("name")).first() product_schema = ProductSchema() if user.admin: if not product: try: post_data = product_schema.load(post_data) except ValidationError as err: return {"errors": err.messages}, 422 product = Product(name=post_data.get("name"), retail_price=post_data.get("retail_price"), description=post_data.get("description"), stock=post_data.get("stock"), pic_filename=post_data.get("pic_filename")) db.session.add(product) db.session.commit() product_schema = ProductSchema() response_obj = { "success": True, "data": product_schema.dump(product) } return make_response(jsonify(response_obj), 201) else: response_obj = { "success": False, 'message': "Product already exists.", } return make_response(jsonify(response_obj), 202) else: response_obj = {"success": False, "message": "Unauthorised Access"} return make_response(jsonify(response_obj), 403)
def put(self, id): user_identifier = get_jwt_identity() user = User.query.filter_by(id=user_identifier).first() product = Product.query.filter_by(id=id).first() post_data = request.get_json(force=True) product_schema = ProductSchema() if user.admin: if product is not None: try: post_data = product_schema.load(post_data) except ValidationError as err: return {"errors": err.messages}, 422 for key in post_data: setattr(product, key.lower(), post_data.get(key.lower())) db.session.commit() product_schema = ProductSchema() response_obj = { "success": True, "data": product_schema.dump(product) } return response_obj else: response_obj = {"success": False, "message": "No such Product"} return make_response(jsonify(response_obj), 404) else: response_obj = {"success": False, "message": "Unauthorised Access"} return make_response(jsonify(response_obj), 403)
def post(self): post_data = request.get_json(force=True) product = Product.query.filter_by(name=post_data.get("name")).first() if not product: product = Product(name=post_data.get("name"), retail_price=post_data.get("retail_price"), description=post_data.get("description"), stock=post_data.get("stock"), pic_filename=post_data.get("pic_filename")) db.session.add(product) db.session.commit() product_schema = ProductSchema() response_obj = { "success": True, "data": product_schema.dump(product) } return make_response(jsonify(response_obj), 201) else: response_obj = { "success": False, 'message': "Product already exists.", } return make_response(jsonify(response_obj), 202)
def get(self, model: str): user = get_jwt_identity() schema = None if model.lower() == "product": schema = ProductSchema(many=True) elif model.lower() == "user": if user is None: return abort(401) schema = UserSchema(many=True) query = f"SELECT * FROM {model} WHERE " args_query = request.args if len(args_query) == 0: return abort(400) else: for key in args_query: query += f"{key} LIKE '%{args_query[key]}%' " results = db.session.execute(query).fetchall() response_obj = { "success": True, "data": { "results": schema.dump(results) } } return response_obj
def get(self): product_schema = ProductSchema(many=True) products = pagination.paginate(Product, product_schema, True) response_obj = {"success": True, "data": {"products": products}} return response_obj