fix: remove .query reference of db.Model

This commit is contained in:
Yeuoly 2024-10-21 20:23:27 +08:00
parent bedbd658fe
commit 53e1b45d40
No known key found for this signature in database
GPG Key ID: A66E7E320FB19F61

View File

@ -1,6 +1,8 @@
import flask_restful import flask_restful
from flask_login import current_user from flask_login import current_user
from flask_restful import Resource, fields, marshal_with from flask_restful import Resource, fields, marshal_with
from sqlalchemy import select
from sqlalchemy.orm import Session
from werkzeug.exceptions import Forbidden from werkzeug.exceptions import Forbidden
from extensions.ext_database import db from extensions.ext_database import db
@ -25,7 +27,13 @@ api_key_list = {"data": fields.List(fields.Nested(api_key_fields), attribute="it
def _get_resource(resource_id, tenant_id, resource_model): def _get_resource(resource_id, tenant_id, resource_model):
resource = resource_model.query.filter_by(id=resource_id, tenant_id=tenant_id).first() if resource_model == App:
with Session(db.engine) as session:
resource = session.execute(
select(resource_model).filter_by(id=resource_id, tenant_id=tenant_id)
).scalar_one_or_none()
else:
resource = resource_model.query.filter_by(id=resource_id, tenant_id=tenant_id).first()
if resource is None: if resource is None:
flask_restful.abort(404, message=f"{resource_model.__name__} not found.") flask_restful.abort(404, message=f"{resource_model.__name__} not found.")