Skip to content

Commit

Permalink
Query accessible records: SQLAlchemy 2.0 syntax (#380)
Browse files Browse the repository at this point in the history
* use the correct method to add options and columns if using sqlalchemy 2.0

* bugfix

* install specific filter and static ngix brotli modules from ppa to fix dependencies mismatch
  • Loading branch information
Theodlz authored Feb 28, 2024
1 parent 1160f75 commit 5648054
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ jobs:
sudo apt install -y wget nodejs unzip firefox
# if nginx is already installed, remove it
sudo apt remove -y nginx nginx-common nginx-core
sudo apt remove -y nginx nginx-common nginx-core nginx-full
sudo apt purge -y nginx nginx-common nginx-core nginx-full
sudo add-apt-repository ppa:ondrej/nginx-mainline -y
# add the PPA repository with brotli support for nginx
sudo add-apt-repository ppa:ondrej/nginx -y
sudo apt update -y
sudo apt install -y nginx libnginx-mod-brotli
sudo apt install nginx libnginx-mod-http-brotli-static libnginx-mod-http-brotli-filter -y
pip install --upgrade pip
Expand Down
26 changes: 19 additions & 7 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import declarative_base, relationship, scoped_session, sessionmaker
from sqlalchemy.orm import (
declarative_base,
load_only,
relationship,
scoped_session,
sessionmaker,
)
from sqlalchemy_utils import EmailType, PhoneNumberType

from .custom_exceptions import AccessError
Expand Down Expand Up @@ -1218,12 +1224,15 @@ def query_accessible_rows(self, cls, user_or_token, columns=None):

if self.query is not None:
query = self.query
# retrieve specified columns if requested
if columns is not None:
query = query.with_entities(*columns)
else:
query = self.query_generator(cls, user_or_token)

# retrieve specified columns if requested
if columns is not None:
query = query.with_entities(*columns)
# here query is not of type sqlalchemy.Query, but sqlalchemy.Select
# so we use the appropriate method to retrieve the columns
if columns is not None:
query = query.options(load_only(*columns))

return query

Expand Down Expand Up @@ -1423,9 +1432,12 @@ def query_records_accessible_by(
)

logic = getattr(cls, mode)
return logic.query_accessible_rows(cls, user_or_token, columns=columns).options(
options
accessible_rows = logic.query_accessible_rows(
cls, user_or_token, columns=columns
)
if len(options) > 0:
return accessible_rows.options(*options)
return accessible_rows

@classmethod
def get(
Expand Down

0 comments on commit 5648054

Please sign in to comment.