Fixed db related issues #69

Merged
retoor merged 2 commits from BordedDev/snek:bugfix/retoors-new-db into main 2025-08-10 00:58:13 +02:00
6 changed files with 32 additions and 27 deletions

View File

@ -107,7 +107,7 @@ async def ip2location_middleware(request, handler):
user["city"]
if user["city"] != location.city:
user["country_long"] = location.country
user["country_short"] = locaion.country_short
user["country_short"] = location.country_short
user["city"] = location.city
user["region"] = location.region
user["latitude"] = location.latitude
@ -451,7 +451,7 @@ class Application(BaseApplication):
async def get_user_template_loader(self, uid=None):
template_paths = []
for admin_uid in self.services.user.get_admin_uids():
for admin_uid in await self.services.user.get_admin_uids():
user_template_path = await self.services.user.get_template_path(admin_uid)
if user_template_path:
template_paths.append(user_template_path)

View File

@ -6,11 +6,11 @@ class UserMapper(BaseMapper):
table_name = "user"
model_class = UserModel
def get_admin_uids(self):
async def get_admin_uids(self):
try:
return [
user["uid"]
for user in self.db.query(
for user in await self.db.query(
"SELECT uid FROM user WHERE is_admin = :is_admin",
{"is_admin": True},
)

View File

@ -156,22 +156,22 @@ class ChannelMessageService(BaseService):
elif page > 0:
async for model in self.query(
f"SELECT * FROM channel_message WHERE channel_uid=:channel_uid WHERE created_at < :timestamp {history_start_filter} ORDER BY created_at DESC LIMIT :page_size",
{
*{
"channel_uid": channel_uid,
"page_size": page_size,
"offset": offset,
"timestamp": timestamp,
},
}.values(),
):
results.append(model)
else:
async for model in self.query(
f"SELECT * FROM channel_message WHERE channel_uid=:channel_uid {history_start_filter} ORDER BY created_at DESC LIMIT :page_size OFFSET :offset",
{
*{
"channel_uid": channel_uid,
"page_size": page_size,
"offset": offset,
},
}.values(),
):
results.append(model)

View File

@ -745,8 +745,13 @@ class AsyncDataSet:
def _build_where(where: Optional[Dict[str, Any]]) -> tuple[str, List[Any]]:
if not where:
return "", []
clauses, vals = zip(*[(f"`{k}` = ?", v) for k, v in where.items()])
return " WHERE " + " AND ".join(clauses), list(vals)
clauses, vals = zip(
*[
(f"`{k}` = ?" if v is not None else f"`{k}` IS NULL", v)
for k, v in where.items()
]
)
return " WHERE " + " AND ".join(clauses), list(v for v in vals if v is not None)
async def _server_insert(
self, table: str, args: Dict[str, Any], return_id: bool = False

View File

@ -53,7 +53,7 @@ async def auth_middleware(request, handler):
request["user"] = None
if request.session.get("uid") and request.session.get("logged_in"):
request["user"] = await request.app.services.user.get(
uid=request.app.session.get("uid")
uid=request.session.get("uid")
)
return await handler(request)

View File

@ -36,7 +36,7 @@ class BaseService:
return await self.mapper.new()
async def query(self, sql, *args):
for record in self.app.db.query(sql, *args):
for record in await self.app.db.query(sql, *args):
yield record
async def get(self, *args, **kwargs):