Fixed db related issues #69
@ -107,7 +107,7 @@ async def ip2location_middleware(request, handler):
|
|||||||
user["city"]
|
user["city"]
|
||||||
if user["city"] != location.city:
|
if user["city"] != location.city:
|
||||||
user["country_long"] = location.country
|
user["country_long"] = location.country
|
||||||
user["country_short"] = locaion.country_short
|
user["country_short"] = location.country_short
|
||||||
user["city"] = location.city
|
user["city"] = location.city
|
||||||
user["region"] = location.region
|
user["region"] = location.region
|
||||||
user["latitude"] = location.latitude
|
user["latitude"] = location.latitude
|
||||||
@ -451,7 +451,7 @@ class Application(BaseApplication):
|
|||||||
|
|
||||||
async def get_user_template_loader(self, uid=None):
|
async def get_user_template_loader(self, uid=None):
|
||||||
template_paths = []
|
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)
|
user_template_path = await self.services.user.get_template_path(admin_uid)
|
||||||
if user_template_path:
|
if user_template_path:
|
||||||
template_paths.append(user_template_path)
|
template_paths.append(user_template_path)
|
||||||
|
@ -6,11 +6,11 @@ class UserMapper(BaseMapper):
|
|||||||
table_name = "user"
|
table_name = "user"
|
||||||
model_class = UserModel
|
model_class = UserModel
|
||||||
|
|
||||||
def get_admin_uids(self):
|
async def get_admin_uids(self):
|
||||||
try:
|
try:
|
||||||
return [
|
return [
|
||||||
user["uid"]
|
user["uid"]
|
||||||
for user in self.db.query(
|
for user in await self.db.query(
|
||||||
"SELECT uid FROM user WHERE is_admin = :is_admin",
|
"SELECT uid FROM user WHERE is_admin = :is_admin",
|
||||||
{"is_admin": True},
|
{"is_admin": True},
|
||||||
)
|
)
|
||||||
|
@ -156,22 +156,22 @@ class ChannelMessageService(BaseService):
|
|||||||
elif page > 0:
|
elif page > 0:
|
||||||
async for model in self.query(
|
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",
|
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,
|
"channel_uid": channel_uid,
|
||||||
"page_size": page_size,
|
"page_size": page_size,
|
||||||
"offset": offset,
|
"offset": offset,
|
||||||
"timestamp": timestamp,
|
"timestamp": timestamp,
|
||||||
},
|
}.values(),
|
||||||
):
|
):
|
||||||
results.append(model)
|
results.append(model)
|
||||||
else:
|
else:
|
||||||
async for model in self.query(
|
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",
|
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,
|
"channel_uid": channel_uid,
|
||||||
"page_size": page_size,
|
"page_size": page_size,
|
||||||
"offset": offset,
|
"offset": offset,
|
||||||
},
|
}.values(),
|
||||||
):
|
):
|
||||||
results.append(model)
|
results.append(model)
|
||||||
|
|
||||||
|
@ -745,8 +745,13 @@ class AsyncDataSet:
|
|||||||
def _build_where(where: Optional[Dict[str, Any]]) -> tuple[str, List[Any]]:
|
def _build_where(where: Optional[Dict[str, Any]]) -> tuple[str, List[Any]]:
|
||||||
if not where:
|
if not where:
|
||||||
return "", []
|
return "", []
|
||||||
clauses, vals = zip(*[(f"`{k}` = ?", v) for k, v in where.items()])
|
clauses, vals = zip(
|
||||||
return " WHERE " + " AND ".join(clauses), list(vals)
|
*[
|
||||||
|
(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(
|
async def _server_insert(
|
||||||
self, table: str, args: Dict[str, Any], return_id: bool = False
|
self, table: str, args: Dict[str, Any], return_id: bool = False
|
||||||
|
@ -53,7 +53,7 @@ async def auth_middleware(request, handler):
|
|||||||
request["user"] = None
|
request["user"] = None
|
||||||
if request.session.get("uid") and request.session.get("logged_in"):
|
if request.session.get("uid") and request.session.get("logged_in"):
|
||||||
request["user"] = await request.app.services.user.get(
|
request["user"] = await request.app.services.user.get(
|
||||||
uid=request.app.session.get("uid")
|
uid=request.session.get("uid")
|
||||||
)
|
)
|
||||||
return await handler(request)
|
return await handler(request)
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ class BaseService:
|
|||||||
return await self.mapper.new()
|
return await self.mapper.new()
|
||||||
|
|
||||||
async def query(self, sql, *args):
|
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
|
yield record
|
||||||
|
|
||||||
async def get(self, *args, **kwargs):
|
async def get(self, *args, **kwargs):
|
||||||
|
Loading…
Reference in New Issue
Block a user