Commit 1a17c015 authored by ZZH's avatar ZZH

fix news search 2024-03-9

parent af97846d
...@@ -108,21 +108,17 @@ async def load_news_pages(params): ...@@ -108,21 +108,17 @@ async def load_news_pages(params):
news_type, se_title = params["news_type"], params["se_title"] news_type, se_title = params["news_type"], params["se_title"]
s_dts, e_dts = params["s_dts"], params["e_dts"] s_dts, e_dts = params["s_dts"], params["e_dts"]
offsets = 0 if page_num == 0 else (page_num - 1) * page_size offsets = 0 if page_num == 0 else (page_num - 1) * page_size
conds, param_lst = [], [] conds = []
if news_type: if news_type:
param_lst.append(news_type) conds.append(f"news_type={news_type}")
conds.append(f"news_type=%s")
if s_dts: if s_dts:
s_dt = pendulum.parse(s_dts, tz="Asia/Shanghai") s_dt = pendulum.parse(s_dts, tz="Asia/Shanghai")
param_lst.append(str(s_dt)) conds.append(f"pub_time>='{str(s_dt)}'")
conds.append(f"pub_time>=%s")
if e_dts: if e_dts:
e_dt = pendulum.parse(e_dts, tz="Asia/Shanghai") e_dt = pendulum.parse(e_dts, tz="Asia/Shanghai")
param_lst.append(str(e_dt)) conds.append(f"pub_time<'{str(e_dt)}'")
conds.append(f"pub_time<%s")
if se_title: if se_title:
param_lst.append(se_title) conds.append(f"title LIKE '%%{se_title}%%'")
conds.append(f"title LIKE '%{se_title}%'")
if len(conds) > 1: if len(conds) > 1:
conds_str = "WHERE " + " AND ".join(conds) conds_str = "WHERE " + " AND ".join(conds)
...@@ -131,7 +127,6 @@ async def load_news_pages(params): ...@@ -131,7 +127,6 @@ async def load_news_pages(params):
else: else:
conds_str = "" conds_str = ""
param_lst += [page_size, offsets]
async with MysqlUtil(db="official_web") as conn: async with MysqlUtil(db="official_web") as conn:
if news_type: if news_type:
sql = "SELECT count(*) FROM official_web.news_info " \ sql = "SELECT count(*) FROM official_web.news_info " \
...@@ -144,9 +139,9 @@ async def load_news_pages(params): ...@@ -144,9 +139,9 @@ async def load_news_pages(params):
sql = f"SELECT id, title, author, news_type, pub_time, top " \ sql = f"SELECT id, title, author, news_type, pub_time, top " \
f"FROM official_web.news_info " \ f"FROM official_web.news_info " \
f"{conds_str} " \ f"{conds_str} " \
f"ORDER BY pub_time DESC, id LIMIT %s OFFSET %s;" f"ORDER BY pub_time DESC, id LIMIT {page_size} OFFSET {offsets};"
articles = await conn.fetchall(sql, (tuple(param_lst))) articles = await conn.fetchall(sql)
return total, articles return total, articles
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment