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.
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'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 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.
I have problems operating with the exiting database in mysql using a sqlalchemy as I need it for building Flask RESTful api.
I get funny errors about circular dependencies here:
from flask import Flask, request, jsonify, make_response
from flaskext.mysql import MySQL
from flask_sqlalchemy import SQLAlchemy
#from webapi import models
# mysql = MySQL()
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secretkey' #for use later
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://user:password#127.0.0.1/mydb'
db = SQLAlchemy(app)
class Extensions(db.Model):
__tablename__ = "Extensions"
extnbr = db.Column(db.Integer, primary_key=True, default="NULL")
authname = db.Column(db.String(256), nullable=True, default="#nobody")
extpriority = db.Column(db.Integer, nullable=True, default=0)
callaccess = db.Column(db.Integer, nullable=True, default=0)
extlocation = db.Column(db.String, nullable=True, default="NULL")
display = db.Column(db.String, nullable=True, default="NULL")
#app.route("/")
def hello():
return "Welcome to Python Flask App!"
#app.route("/extensions", methods=["GET"])
def get_all_extensions():
users = Extensions.query.all()
output = []
for user in users:
user_data = {}
user_data['extnbr'] = user.extnbr
user_data['authname'] = user.authname
user_data['extpriority'] = user.extpriority
user_data['callaccess'] = user.callaccess
user_data['extlocation'] = user.extlocation
user_data['display'] = user.display
output.append({'extensions' : output})
return jsonify({'users' : output})
if __name__ == "__main__":
app.run(debug=True)
I get error about circular dependence:
Debug and trace:
https://dpaste.de/F3uX
This has to do with your output. You're appending a dictionary containing output to output itself, creating a funky data structure causing the json error.
Try changing
output.append({'extensions' : output})
to this:
output.append({'extensions' : user_data })
(Which is what I assume you want anyways)
The problem is:
output = []
for user in users:
...
output.append({'extensions' : output}) # here you are adding output to the output
check this example:
out = []
for i in range(2):
out.append({'ext': out})
# output
[{'ext': [...]}, {'ext': [...]}]
You have the same output. I am sure this is not what you want. So change the line with problem to this one: output.append({'extensions' : user_data})
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