Skip to content

Commit

Permalink
refactor: use session.get where possible
Browse files Browse the repository at this point in the history
* recomended sqlalchemy syntax for sqlalchemy >= 2.0
  • Loading branch information
utnapischtim committed Oct 31, 2024
1 parent eeb91b7 commit 3df8f94
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions invenio_banners/records/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from flask import current_app
from invenio_db import db
from sqlalchemy import or_
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.sql import text
from sqlalchemy_utils.models import Timestamp

Expand Down Expand Up @@ -69,17 +68,20 @@ def create(cls, data):
def update(cls, data, id):
"""Update an existing banner."""
with db.session.begin_nested():
# NOTE:
# with db.session.get(cls, id) the model itself would be
# returned and this classmethod would be called
db.session.query(cls).filter_by(id=id).update(data)

db.session.commit()

@classmethod
def get(cls, id):
"""Get banner by its id."""
try:
return db.session.query(cls).filter_by(id=id).one()
except NoResultFound:
raise BannerNotExistsError(id)
if banner := db.session.get(cls, id):
return banner

raise BannerNotExistsError(id)

@classmethod
def delete(cls, banner):
Expand Down

0 comments on commit 3df8f94

Please sign in to comment.