-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sim w/o global session (Sourcery refactored) #7
base: simulation-no-global
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,10 +53,12 @@ def __init__(self, session: Session): | |
self._query = select(EntityMapper) | ||
|
||
def get(self, entity_id: EntityID) -> Entity: | ||
dto = self._session.scalars(self._query.filter_by(uuid=entity_id)).one_or_none() | ||
if not dto: | ||
if dto := self._session.scalars( | ||
self._query.filter_by(uuid=entity_id) | ||
).one_or_none(): | ||
return Entity(dto) | ||
else: | ||
raise NotFound(entity_id) | ||
return Entity(dto) | ||
Comment on lines
-56
to
-59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def save(self, entity: Entity) -> None: | ||
self._session.add(entity.dto) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,8 +146,9 @@ class EngineConfig(ConfigBase): | |
@root_validator | ||
def scrub_execution_options(cls, values): | ||
if "execution_options" in values: | ||
execute_options = values["execution_options"].dict(exclude_defaults=True) | ||
if execute_options: | ||
if execute_options := values["execution_options"].dict( | ||
exclude_defaults=True | ||
): | ||
Comment on lines
-149
to
+151
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
values["execution_options"] = execute_options | ||
return values | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ def __init__( | |
config: t.Optional[SQLAlchemyConfig] = None, | ||
app: t.Optional[Quart] = None, | ||
): | ||
initialize = False if config is None else True | ||
initialize = config is not None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
super().__init__(config, initialize=initialize) | ||
|
||
if app is not None: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -301,7 +301,7 @@ def accumulate_mappings(class_, attribute) -> t.Dict[str, t.Any]: | |
if base_class is class_: | ||
continue | ||
args = getattr(base_class, attribute, {}) | ||
accumulated.update(args) | ||
accumulated |= args | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
return accumulated | ||
|
||
|
@@ -316,7 +316,7 @@ def accumulate_tuples_with_mapping(class_, attribute) -> t.Sequence[t.Any]: | |
args = getattr(base_class, attribute, ()) | ||
for arg in args: | ||
if isinstance(arg, t.Mapping): | ||
accumulated_map.update(arg) | ||
accumulated_map |= arg | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
else: | ||
accumulated_args.append(arg) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,17 +45,17 @@ def provide_global_contextual_session(func): | |
@wraps(func) | ||
def wrapper(self, *args, **kwargs): | ||
session_in_args = any( | ||
[isinstance(arg, (sa.orm.Session, sa.ext.asyncio.AsyncSession)) for arg in args] | ||
isinstance(arg, (sa.orm.Session, sa.ext.asyncio.AsyncSession)) | ||
for arg in args | ||
) | ||
session_in_kwargs = "session" in kwargs | ||
session_provided = session_in_args or session_in_kwargs | ||
|
||
if session_provided: | ||
return func(self, *args, **kwargs) | ||
else: | ||
session = session_proxy() | ||
session = session_proxy() | ||
|
||
return func(self, session, *args, **kwargs) | ||
return func(self, session, *args, **kwargs) | ||
Comment on lines
-48
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
return wrapper | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -228,7 +228,7 @@ def auth_endpoint_security(self): | |
results = self.authenticator.enforce(security_schemes, session) | ||
authorized_credentials = {} | ||
for result in results: | ||
authorized_credentials.update(result) | ||
authorized_credentials |= result | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
g.authorized_credentials = authorized_credentials | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,21 +16,22 @@ | |
sa = sqlalchemy | ||
|
||
|
||
|
||
|
||
class AppSettings(BaseSettings): | ||
|
||
class Config: | ||
env_file = ".env", ".secrets.env" | ||
|
||
LOAD_BLUEPRINTS: t.List[str] = Field( | ||
default_factory=lambda: list(("quart_sqlalchemy.sim.views.api",)) | ||
default_factory=lambda: ["quart_sqlalchemy.sim.views.api"] | ||
) | ||
LOAD_EXTENSIONS: t.List[str] = Field( | ||
default_factory=lambda: list( | ||
( | ||
"quart_sqlalchemy.sim.db.db", | ||
"quart_sqlalchemy.sim.app.schema", | ||
"quart_sqlalchemy.sim.auth.auth", | ||
) | ||
) | ||
default_factory=lambda: [ | ||
"quart_sqlalchemy.sim.db.db", | ||
"quart_sqlalchemy.sim.app.schema", | ||
"quart_sqlalchemy.sim.auth.auth", | ||
] | ||
Comment on lines
+19
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
) | ||
SECURITY_SCHEMES: t.Dict[str, SecuritySchemeBase] = Field( | ||
default_factory=lambda: { | ||
|
@@ -52,4 +53,5 @@ class Config: | |
WEB3_HTTPS_PROVIDER_URI: str = Field(env="WEB3_HTTPS_PROVIDER_URI") | ||
|
||
|
||
|
||
settings = AppSettings() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,19 +51,13 @@ def process_bind_param(self, value, dialect): | |
"""Data going into to the database will be transformed by this method. | ||
See ``ObjectID`` for the design and rational for this. | ||
""" | ||
if value is None: | ||
return None | ||
|
||
return ObjectID(value).decode() | ||
return None if value is None else ObjectID(value).decode() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def process_result_value(self, value, dialect): | ||
"""Data going out from the database will be explicitly casted to the | ||
``ObjectID``. | ||
""" | ||
if value is None: | ||
return None | ||
|
||
return ObjectID(value) | ||
return None if value is None else ObjectID(value) | ||
Comment on lines
-63
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
class MyBase(BaseMixins, sa.orm.DeclarativeBase): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,16 +99,11 @@ def update_app_name_by_id(self, session: sa.orm.Session, magic_client_id, app_na | |
""" | ||
client = self.logic.MagicClient.update_by_id(session, magic_client_id, app_name=app_name) | ||
|
||
if not client: | ||
return None | ||
|
||
return client.app_name | ||
return client.app_name if client else None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@provide_global_contextual_session | ||
def update_by_id(self, session: sa.orm.Session, magic_client_id, **kwargs): | ||
client = self.logic.MagicClient.update_by_id(session, magic_client_id, **kwargs) | ||
|
||
return client | ||
return self.logic.MagicClient.update_by_id(session, magic_client_id, **kwargs) | ||
Comment on lines
-109
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@provide_global_contextual_session | ||
def set_inactive_by_id(self, session: sa.orm.Session, magic_client_id): | ||
|
@@ -341,11 +336,10 @@ def sync_auth_wallet( | |
wallet_type: t.Optional[WalletType] = None, | ||
): | ||
with session.begin_nested(): | ||
existing_wallet = self.logic.AuthWallet.get_by_auth_user_id( | ||
if existing_wallet := self.logic.AuthWallet.get_by_auth_user_id( | ||
session, | ||
auth_user_id, | ||
) | ||
if existing_wallet: | ||
): | ||
Comment on lines
-344
to
+342
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
raise RuntimeError("WalletExistsForNetworkAndWalletType") | ||
|
||
return self.logic.AuthWallet.add( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,13 +31,12 @@ class LogicMeta(type): | |
def __init__(cls, name, bases, cls_dict): | ||
if not hasattr(cls, "_registry"): | ||
cls._registry = {} | ||
else: | ||
if cls.__name__ not in cls._ignore: | ||
model = getattr(cls, "model", None) | ||
if model is not None: | ||
name = model.__name__ | ||
elif cls.__name__ not in cls._ignore: | ||
model = getattr(cls, "model", None) | ||
if model is not None: | ||
name = model.__name__ | ||
|
||
cls._registry[name] = cls() | ||
cls._registry[name] = cls() | ||
Comment on lines
-34
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
super().__init__(name, bases, cls_dict) | ||
|
||
|
@@ -161,10 +160,7 @@ def add_by_email_and_client_id( | |
user_type=user_type, | ||
): | ||
logger.exception( | ||
"User duplication for email: {} (client_id: {})".format( | ||
email, | ||
client_id, | ||
), | ||
f"User duplication for email: {email} (client_id: {client_id})" | ||
Comment on lines
-164
to
+163
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
) | ||
raise DuplicateAuthUser() | ||
|
||
|
@@ -176,10 +172,7 @@ def add_by_email_and_client_id( | |
**kwargs, | ||
) | ||
logger.info( | ||
"New auth user (id: {}) created by email (client_id: {})".format( | ||
row.id, | ||
client_id, | ||
), | ||
f"New auth user (id: {row.id}) created by email (client_id: {client_id})" | ||
) | ||
|
||
return row | ||
|
@@ -203,7 +196,7 @@ def add_by_client_id( | |
date_verified=datetime.utcnow() if is_verified else None, | ||
) | ||
logger.info( | ||
"New auth user (id: {}) created by (client_id: {})".format(row.id, client_id), | ||
f"New auth user (id: {row.id}) created by (client_id: {client_id})" | ||
Comment on lines
-206
to
+199
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
) | ||
|
||
return row | ||
|
@@ -409,7 +402,7 @@ def add( | |
management_type=None, | ||
auth_user_id=None, | ||
): | ||
new_row = self._repository.add( | ||
return self._repository.add( | ||
Comment on lines
-412
to
+405
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
session, | ||
auth_user_id=auth_user_id, | ||
public_address=public_address, | ||
|
@@ -419,8 +412,6 @@ def add( | |
network=network, | ||
) | ||
|
||
return new_row | ||
|
||
@provide_global_contextual_session | ||
def get_by_id(self, session, model_id, allow_inactive=False, join_list=None): | ||
return self._repository.get_by_id( | ||
|
@@ -441,10 +432,7 @@ def get_by_public_address(self, session, public_address, network=None, is_active | |
|
||
row = self._repository.get_by(session, filters=filters, allow_inactive=not is_active) | ||
|
||
if not row: | ||
return None | ||
|
||
return one(row) | ||
return one(row) if row else None | ||
Comment on lines
-444
to
+435
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@provide_global_contextual_session | ||
def get_by_auth_user_id( | ||
|
@@ -470,10 +458,7 @@ def get_by_auth_user_id( | |
session, filters=filters, join_list=join_list, allow_inactive=not is_active | ||
) | ||
|
||
if not rows: | ||
return [] | ||
|
||
return rows | ||
return rows or [] | ||
Comment on lines
-473
to
+461
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@provide_global_contextual_session | ||
def update_by_id(self, session, model_id, **kwargs): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,11 +59,7 @@ def get_by( | |
|
||
join_list = join_list or () | ||
|
||
if order_by_clause is not None: | ||
order_by_clause = (order_by_clause,) | ||
else: | ||
order_by_clause = () | ||
|
||
order_by_clause = (order_by_clause, ) if order_by_clause is not None else () | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return self._adapted.select( | ||
session, | ||
conditions=filters, | ||
|
@@ -129,9 +125,7 @@ def count_by( | |
else: | ||
selectables = [sa.label("count", sa.func.count(self.model.id))] | ||
|
||
for group in group_by: | ||
selectables.append(group.expression) | ||
|
||
selectables.extend(group.expression for group in group_by) | ||
Comment on lines
-132
to
+128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
result = self._adapted.select(session, selectables, conditions=filters, group_by=group_by) | ||
|
||
return result.all() | ||
|
@@ -178,15 +172,16 @@ def yield_by_chunk( | |
): | ||
filters = filters or () | ||
join_list = join_list or () | ||
results = self._adapted.select( | ||
yield from self._adapted.select( | ||
session, | ||
conditions=filters, | ||
options=[sa.orm.selectinload(getattr(self.model, attr)) for attr in join_list], | ||
options=[ | ||
sa.orm.selectinload(getattr(self.model, attr)) | ||
for attr in join_list | ||
], | ||
include_inactive=allow_inactive, | ||
yield_by_chunk=chunk_size, | ||
) | ||
for result in results: | ||
yield result | ||
Comment on lines
-181
to
-189
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
class PydanticScalarResult(sa.ScalarResult, t.Generic[ModelSchemaT]): | ||
|
@@ -243,9 +238,7 @@ def insert( | |
create_data = create_schema.dict() | ||
result = super().insert(session, create_data) | ||
|
||
if sqla_model: | ||
return result | ||
return self.model_schema.from_orm(result) | ||
return result if sqla_model else self.model_schema.from_orm(result) | ||
Comment on lines
-246
to
+241
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def update( | ||
self, | ||
|
@@ -266,9 +259,7 @@ def update( | |
session.flush() | ||
session.refresh(existing) | ||
|
||
if sqla_model: | ||
return existing | ||
return self.model_schema.from_orm(existing) | ||
return existing if sqla_model else self.model_schema.from_orm(existing) | ||
Comment on lines
-269
to
+262
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def get( | ||
self, | ||
|
@@ -291,9 +282,7 @@ def get( | |
if row is None: | ||
return | ||
|
||
if sqla_model: | ||
return row | ||
return self.model_schema.from_orm(row) | ||
return row if sqla_model else self.model_schema.from_orm(row) | ||
Comment on lines
-294
to
+285
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def select( | ||
self, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,10 +55,7 @@ class Config: | |
|
||
@validator("status") | ||
def set_status_by_error_code(cls, v, values): | ||
error_code = values.get("error_code") | ||
if error_code: | ||
return "failed" | ||
return "ok" | ||
return "failed" if (error_code := values.get("error_code")) else "ok" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
class MagicClientSchema(BaseSchema): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,7 @@ def __lt__(self, other): | |
return False | ||
|
||
def __hash__(self): | ||
return hash(tuple([self._encoded_id, self._decoded_id])) | ||
return hash((self._encoded_id, self._decoded_id)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def __str__(self): | ||
return "{encoded_id}".format(encoded_id=self._encoded_id) | ||
|
@@ -88,10 +88,7 @@ def encode(self): | |
return self._encoded_id | ||
|
||
def _decode(self, value): | ||
if isinstance(value, int): | ||
return value | ||
else: | ||
return self.hashids.decode(value)[0] | ||
return value if isinstance(value, int) else self.hashids.decode(value)[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def decode(self): | ||
return self._decoded_id | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
provide_session
refactored with the following changes:remove-unnecessary-else
)