I am using marshmallow and Flask Restplus for my one of API code where i have table with few columns i wan to post data into table using the POST method. In table i have one of column as BLOB which accept files JPEG or PDF and stores it into database.
I was able to save record into DB using the regular flask forms and now i want to enable to this functionality using the API.
For that I have bellow code where
Expense Model.py
from flask_sqlalchemy import SQLAlchemy
from app import db,ma
from datetime import datetime
from flask import url_for, current_app
from app.utils.exceptions import ValidationError
from sqlalchemy.dialects.mysql import LONGBLOB
from marshmallow import fields, pre_load, validate
from app.models.Person_model import PersonSchema
from app.models.ExpType_model import ExpTypeSchema
#This is model defination for the Expnese Table and its api calls with get post put all covered in here.
class Expenses(db.Model):
__tablename__ = 'Expesnes'
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
per_id = db.Column(db.Integer, db.ForeignKey('Persons.id'), nullable=False)
U_id = db.Column(db.Integer, db.ForeignKey('Users.id'), nullable=False, index=True)
Exp_per_name = db.Column(db.String(64), index=True)
Exp_type_name = db.Column(db.String(100),index=True)
Exp_amt = db.Column(db.Float)
Exp_img = db.Column(db.LargeBinary)
Exp_FileName = db.Column(db.String(300))
Exp_date = db.Column(db.DateTime,index=True)
Exp_comm = db.Column(db.String(200))
def __init__(self,Exp_per_name,Exp_type_name,Exp_amt,Exp_img,Exp_FileName,Exp_date,Exp_comm):
self.Exp_per_name = Exp_per_name,
self.Exp_type_name = Exp_type_name,
self.Exp_amt = Exp_amt,
self.Exp_img = Exp_img,
self.Exp_FileName = Exp_FileName,
self.Exp_date = Exp_date,
self.Exp_comm = Exp_comm
# Custom validator
def must_not_be_blank(data):
if not data:
raise ValidationError('Data not provided.')
class ExpensesSchema(ma.Schema):
class Meta:
model = Expenses
id = fields.Integer(dump_only=True)
u_id = fields.Integer(dump_only=True)
Exp_per_name = fields.Nested(PersonSchema, validate=must_not_be_blank)
ExpType_name = fields.Nested(ExpTypeSchema,validate=must_not_be_blank)
Exp_amt =fields.Float(required=True)
Exp_img = fields.Raw()
Exp_FileName = fields.Str(allow_none=None)
Exp_comm = fields.Str(allow_none=None)
Expense_Resource.py File
from flask_restplus import Resource
import json
from flask import request
from flask_login import current_user, login_required
from app.models.Expense_model import Expenses, ExpensesSchema
from datetime import datetime
from app import db
expenses_schema = ExpensesSchema(many=True)
expense_schema = ExpensesSchema()
class ExpenseRes(Resource):
# This is method for all ExpTypes
#classmethod
#login_required
def get(cls):
exptypes = Expenses.query.filter_by(u_id=current_user.id)
invtypes = expenses_schema.dump(exptypes).data
return {'status': 'success', 'data': exptypes}, 200
# This is new records creation Persons
#classmethod
#login_required
def post(cls):
json_data = request.get_json(force=True)
if not json_data:
return {'message': 'No input data provided'}, 400
# Validate and deserialize input
data, errors = expense_schema.load(json_data)
print data
if errors:
return errors, 422
expensetype = Expenses.query.filter_by(u_id=current_user.id,
Exp_per_name=data['Exp_per_name'],
Exp_type_name = data['Exp_type_name'],
Exp_amt=data['Exp_amt'],
Exp_date=data['Exp_date']
).first()
if expensetype:
return {'message': 'Expense name already exists'}, 400
newexpensetype = Expenses(u_id=current_user.id,
Exp_per_name=data['Exp_per_name'],
Exp_type_name=data['Exp_type_name'],
Exp_img = request.json['binary'],
Exp_FileName= request.json['file_name'],
Exp_amt=data['Exp_amt'],
Exp_date=data['Exp_date'],
Exp_comm= data['Exp_comm']
)
try:
db.session.add(newexpensetype)
db.session.commit()
result = expense_schema.dump(newexpensetype).data
return {"status": 'success', 'data': result}, 201
except:
return {'message': 'An error occurred while creating Investment Type'}, 500
How to save file in to database and how do i pass file pay load to my API.
Is there way to only allow JPEG and PDF file with size less 15 MB only to go
Thank you for your time and appreciate your feedback.
Related
I have created a flask app. To store the data I have used sqlalchemy to create a db file.
My aim is to create an event calendar. I have decided to use FullCalendar.io . But the issue is that to feed the data it needs json file. Can anyone help me out
from flask import Flask, render_template, request, redirect
from flask.wrappers import Request
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from sqlalchemy.orm import query
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///caldb.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class CalData(db.Model):
date = db.Column(db.String(200), nullable=False)
title = db.Column(db.String(200), nullable=False, primary_key=True)
desc = db.Column(db.String(500), nullable=False)
time = db.Column(db.Integer, nullable=False)
link = db.Column(db.String(200), nullable=True)
def __repr__(self) -> str:
return f"{self.title} - {self.time}"
#app.route('/cal2')
def cal2():
cd = CalData.query.all()
return render_template("cal2.html", data=cd)
I'm not familiar with FullCalendar.io, but let's assume that you need a list of objects, each representing a single calendar event, in the format you've described in the ORM model. Then, you only need to put all the data from the instances you get from your query into dicts, and then send it out with jsonify:
from flask import jsonify
...
...
...
#app.route('/cal2')
def cal2():
result = []
calendar_entries = CalData.query.all()
for entry in calendar_entries:
obj = dict()
obj['date'] = entry.date
obj['title'] = entry.title
...
result.append(obj)
return jsonify(result)
I have created and configure a database and i am able to post data other than image or file and i am able to fetch the those data but i am unable to fetch the image data .
I am getting this error
> UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position
> 0: invalid start byte
This is my code :
from app import app, api, db, ma, Resource, request, images_schema, image_schema, ImageData, json, jsonify
import base64
from base64 import b64encode
class AllImage(Resource):
def get(self):
images = ImageData.query.all()
print(images)
return images_schema.dump(images)
def post(self):
if 'image' in request.files:
imagedata = request.files['image']
imageD = imagedata.filename
imageD = imagedata.read()
image = ImageData(
uname = request.form['uname'],
fname = request.form['fname'],
lname = request.form['lname'],
email = request.form['email'],
# image = base64.b64encode(imageD)
image= imageD
)
db.session.add(image)
db.session.commit()
# return image_schema.dump(image)
print(type(image))
# return "success"
return image_schema.dump(image)
api.add_resource(AllImage,'/image/data')
This is my DB config
from flask import Flask, json, jsonify, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow # new
from flask_restful import Api, Resource # new
from flask_cors import CORS
import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///image.db'
db = SQLAlchemy(app)
ma = Marshmallow(app)
api = Api(app)
CORS(app, resources={r"/loan/*": {"origins": "*"}})
class ImageData(db.Model):
id = db.Column(db.Integer, primary_key = True)
uname = db.Column(db.VARCHAR(20), nullable = True, unique=True)
fname = db.Column(db.VARCHAR(20), nullable = True)
lname = db.Column(db.VARCHAR(20), nullable = True)
email = db.Column(db.VARCHAR(50), nullable = True, unique=True)
# image = db.Column(db.LargeBinary, nullable=True)
image =db.Column(db.BLOB)
class ImageDataSchema(ma.Schema):
class Meta:
fields = ('id','uname','fname','lname','email','image')
image_schema = ImageDataSchema()
images_schema = ImageDataSchema(many=True)
I tried largebinary and blob type, what am i doing wrong here ?
A bit of an late answer -
you need to do something along the lines of
imageD = base64.b64encode(files.read())
but it's very likely your schema is then going to have trouble auto-dumping/serializing that when you try to "display" it.
I'm trying to use the sqlalchemy for writing one of the GET API. Underlaying database is postgresSQL.
My datetime column is not getting json serialized. Can you please help me to resolve this issue:
Model Class:
models\rpt.py
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class RptModel(db.Model):
__tablename__='rpt_detail'
id = db.Column(db.Integer, primary_key=True)
user_nm = db.Column(db.VARCHAR(100), nullable=False)
created_dt = db.column(db.DateTime(timezone=True))
def __init__(self, _id, user_nm, created_dt):
self.id = _id
self.user_nm = user_nm
self.created_dt = created_dt
def json():
return {'id': self.id, 'user_nm': self.user_nm, 'created_dt': self.created_dt}
#classmethod
def get_rpt_by_id(Cls, id):
return Cls.query.filter_by(id=id).first()
Resource Class
resources\rpt.py
from flask_restful import Resource
from models.rpt import RptModel
class GetRpt(Resource):
def get(self, id):
rpt = RptModel.get_rpt_by_id(id)
if rpt:
return rpt.json(), 200
return {'message': 'rpt not found'}, 404
app.py
from flask import Flask
from flask_restful import Api
from resources.rpt import GetRpt
app = Flask(__name__)
app.config['POSTGRESQL_USER'] = 'AAAA'
app.config['POSTGRESQL_PASSWORD'] = 'BBBB'
app.config['POSTGRESQL_HOST'] = 'CCCC.DDD.COM'
app.config['POSTGRESQL_DB'] = 'EEEE'
postgres_db_uri = 'postgresql://'+app.config['POSTGRESQL_USER']+':'+app.config['POSTGRESQL_PASSWORD']+'#'+app.config['POSTGRESQL_HOST']+'/'+app.config['POSTGRESQL_DB']
app.config['SQLALCHEMY_DATABASE_URI'] = postgres_db_uri
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
api = APi(app)
api.add_resource(GetRpt, 'rpt/<string:id>')
if __name__='__main__':
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
db.init_app(app)
app.run(debug=True)
Postgres RPT Table
Table: rpt_detail
column Type Collation Nullable Default
id integer not null nextval('rpt_detail_id_seq'::regclass)
user_nm character varying(100) not null
created_dt timestamp with time zone not null
When I'm trying to hit this from the postman, I'm getting error stating
TypeError: <sqlalchemy.sql.elements.ColumnClause at 0x3ee9890; DATETIME> is not JSON serializable
I'm not able to convert the created_dt field to JSON format. Can anyone provide any resolution to this.
I have two servers behind a load balancer. If a PUT or POST transaction is being run on one server, the other server will not be able to query the latest data i.e.
Request 1: Change vehicle's name, for id 2, from Car 001 to Car 101.
Request 2: Get vehicle's name for id 2 and it will show Car 001 until a few minutes then it will change to Car 101.
My folder structure is like this:
-api
--helpers
--models
---init.py
---vehicle.py
--resources
---init.py
---vehicles.py
Here is the code for init.py in models:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "url"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_POOL_RECYCLE'] = 5
app.config['SQLALCHEMY_POOL_SIZE'] = 10
db = SQLAlchemy(app)
ma = Marshmallow(app)
Here is the vehicle model:
import arrow, json, uuid
import jwt
from datetime import datetime
import datetime as dt
from functools import wraps
from api.models import db, ma
from api.titan.v1.models.vehicle_state import VehicleState
class Vehicle(db.Model):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(300), index=True)
createdAt = db.Column(db.DateTime)
plateNumber = db.Column(db.String(300))
Here is the vehicle requests handling:
import arrow, json, uuid, pytz
import requests
from datetime import datetime
from flask_restful import reqparse, abort, Api, Resource
from werkzeug.exceptions import BadRequest
from flask import request, jsonify
from api.models import db
class Vehicles(Resource):
def get(self, vehicle_id=None):
response_object = {
'object': 'vehicle',
'verb': 'GET',
'timestamp': arrow.utcnow().timestamp
}
if vehicle_id is None:
vehicles = db.session.query(Vehicle).filter_by(companyId=user['companyId']).all()
response_object['data'] = [schema.dump(i).data for i in vehicles]
else:
vehicle = db.session.query(Vehicle).filter_by(id=vehicle_id, companyId=user['companyId']).first()
response_object['data'] = schema.dump(vehicle).data
return response_object, 200
def put(self, vehicle_id):
data = request.get_json()
response_object = {
'object': 'vehicle',
'verb': 'PUT',
'timestamp': arrow.utcnow().timestamp,
}
vehicle = db.session.query(Vehicle).filter_by(id=vehicle_id).first()
vehicle.name = data['name']
db.session.commit()
response_object['data'] = vehicle
return response_object, 200
I'm trying to get a todo_ID using GET method. I'm still new at using sqlalchemy and most of the things i have tried are telling me that sqlalchemy doesn't use them. Also, can someone tell me how to use HEAD, i want my methods to return http statuses, i kinda tried using them and imported the render template but when i try to use them it says it has no idea what they are.
this is my attempt at looking at a tutorial and making changes
from flask import Flask, jsonify,json, request, render_template, abort
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_pyfile('Config.py')
db = SQLAlchemy(app)
class JsonModel(object): # Class for making objects JSON serializable
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class User(db.Model, JsonModel): # Class which is a model for the User table in the database
User_ID = db.Column(db.Integer, primary_key = True)
FirstName = db.Column(db.String(20))
LastName = db.Column(db.String(20))
def __init__(self,User_ID,FirstName, LastName):
self.User_ID = User_ID
self.FirstName = FirstName
self.LastName = LastName
class Todo(db.Model, JsonModel): # Class which is a model for the Todo table in the database
todo_ID = db.Column(db.Integer, primary_key = True)
UserID = db.Column(db.Integer, db.ForeignKey("user.User_ID"))
details = db.Column(db.String(30))
def __init__(self, UserID, details):
self.UserID = UserID
self.details = details
#app.route('/todo', methods = ['GET']) # Uses GET method to return all information in the database.
def index():
return json.dumps([u.as_dict() for u in User.query.all()+Todo.query.all()]), 201
#app.route('/todo/<int:todo_ID>', methods = ['GET'])
def get(todo_ID):
query = Todo.query.get()
return {'todo': [dict(zip(tuple(query.keys()), i)) for i in query.cursor if i[1] == todo_ID]}
#app.before_first_request #Creates everything before the first request.
def startup():
db.create_all()
if __name__ == '__main__':
app.run()
My most recent attempt was:
#app.route('/todo/<int:todo_ID>', methods = ['GET'])
def get(todo_ID):
query = Todo.query("select * from Todo")
return {'todo': [dict(zip(tuple(query.keys()), i)) for i in query.cursor if i[1] == todo_ID]}
And the error that I get is this.
query = Todo.query("select * from Todo")
TypeError: 'BaseQuery' object is not callable
127.0.0.1 - - [30/Nov/2016 21:15:28] "GET /todo/1 HTTP/1.1" 500 -
If you want to query Todo by primary key and only return one record you can use:
from flask import jsonify
#app.route('/todo/<int:todo_ID>', methods = ['GET'])
def get(todo_ID):
response = {}
todo = Todo.query.get(todo_ID)
response['id'] = todo.id
response['user_id'] = todo.UserID
response['details'] = todo.details
response['status_code'] = 201
return jsonify(response)
Or you can use Marshmallow to have a serializer for each of your models so it can serialize them for you automatically.
Not sure I understand your problem, if your intention is to return json output as response and you want to control the status code, you could
use jsonify
from flask import jsonify
#app.route('/todo/<int:todo_ID>', methods = ['GET'])
def get(todo_ID):
query = Todo.query("select * from Todo")
response = {'todo': [dict(zip(tuple(query.keys()), i)) for i in query.cursor if i[1] == todo_ID]}
response = jsonify(response)
response.status_code = 201
return response