Skip to content

Commit

Permalink
improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Micheal Gendy committed Feb 2, 2022
1 parent 592ebba commit 6f79635
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions orm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
"lte": "__le__",
}

MODEL = typing.TypeVar("MODEL", bound="Model")


def _update_auto_now_fields(values, fields):
for key, value in fields.items():
Expand Down Expand Up @@ -468,7 +466,7 @@ async def update(self, **kwargs) -> None:
await self.database.execute(expr)

async def bulk_update(
self, objs: typing.List[MODEL], fields: typing.List[str]
self, objs: typing.List["Model"], fields: typing.List[str]
) -> None:
fields = {
key: field.validator
Expand All @@ -477,29 +475,25 @@ async def bulk_update(
}
validator = typesystem.Schema(fields=fields)
new_objs = [
_update_auto_now_fields(validator.validate(value), self.model_cls.fields)
for value in [
{
key: _convert_value(value)
for key, value in obj.__dict__.items()
if key in fields
}
for obj in objs
]
{
key: _convert_value(value)
for key, value in obj.__dict__.items()
if key in fields
}
for obj in objs
]
expr = (
self.table.update()
.where(
getattr(self.table.c, self.pkname) == sqlalchemy.bindparam(self.pkname)
)
.values(
{
field: sqlalchemy.bindparam(field)
for obj in new_objs
for field in obj.keys()
}
)
)
new_objs = [
_update_auto_now_fields(validator.validate(obj), self.model_cls.fields)
for obj in new_objs
]
pk_column = getattr(self.table.c, self.pkname)
expr = self.table.update().where(pk_column == sqlalchemy.bindparam(self.pkname))
kwargs = {
field: sqlalchemy.bindparam(field)
for obj in new_objs
for field in obj.keys()
}
expr = expr.values(kwargs)
pk_list = [{self.pkname: getattr(obj, self.pkname)} for obj in objs]
joined_list = [{**pk, **value} for pk, value in zip(pk_list, new_objs)]
await self.database.execute_many(str(expr), joined_list)
Expand Down

0 comments on commit 6f79635

Please sign in to comment.