Commit 0269e24c authored by ZZH's avatar ZZH

update news info 2024-03-6

parent 916d9f4b
......@@ -10,43 +10,46 @@ from pot_libs.sanic_api.column import Opt, Int, List, Str
@dataclass
class NewsInfoReq(Model):
news_id: str = Str("新闻id").eg("12")
@dataclass
class EntIndustryNewsPagesReq(Model):
news_type: int = Int("新闻类型(1:公司新闻, 2:行业新闻)").eg(1)
page_num: int = Int("页码").eg(1)
page_size: int = Opt(Int("条数").eg(10))
news_id: str = Str("文章id").eg("12")
@dataclass
class NewsInfoRsp(Model):
title: str = Str("新闻标题").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")
contents: str = Str("新闻内容").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")
title: str = Str("文章标题").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")
contents: str = Str("文章内容").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")
pub_time: str = Str("发布时间").eg("2023-12-29 18:06")
@dataclass
class PubNewsReq(Model):
title: str = Str("新闻标题").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")
title: str = Str("文章标题").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")
author: str = Opt(Str("文章作者").eg("清科优能"))
pub_time: str = Str("发布时间").eg("2023-12-29 18:06")
@dataclass
class ReviseNewsReq(Model):
news_id: str = Str("文章id").eg("12")
title: str = Str("文章标题").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")
@dataclass
class NewsPagesReq(Model):
news_type: int = Opt(Int("文章类型(1:公司新闻, 2:行业新闻) 不传则返回所有类型文章").eg(1))
page_num: int = Int("页码").eg(1)
page_size: int = Opt(Int("条数").eg(10))
@dataclass
class NewsInfo(Model):
id: int = Opt(Int("新闻ID").eg(1))
title: str = Str("新闻标题").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")
id: int = Opt(Int("文章ID").eg(1))
title: str = Str("文章标题").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")
author: str = Str("文章作者").eg("清科优能")
pub_time: str = Str("发布时间").eg("2023-12-29 18:06")
news_type: int = Int("文章类型(1:公司新闻, 2:行业新闻)").eg(1)
top: int = Opt(Int("是否置顶").eg(0))
contents: str = Opt(Str("新闻内容").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!"))
cover_url: str = Opt(Str("新闻封面URL").eg("xxxxxxxxxx"))
contents: str = Opt(Str("文章内容").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!"))
cover_url: str = Opt(Str("文章封面URL").eg("xxxxxxxxxx"))
@dataclass
......
......@@ -43,7 +43,7 @@ async def upload_news_qs(request):
else:
return False, url_article, url_cover
log.info(f"上传新闻和封面成功 {url_article} {url_cover}")
log.info(f"上传文章和封面成功 {url_article} {url_cover}")
return True, url_article, url_cover
......@@ -51,19 +51,44 @@ async def save_news_info(title, pub_time, news_url, cover_url):
sql = "INSERT INTO official_web.news_info (" \
"title, pub_time, news_url, cover_url) " \
"VALUES (%s, %s, %s, %s);"
async with MysqlUtil() as conn:
async with MysqlUtil(db="official_web") as conn:
await conn.execute(sql, (title, str(pub_time), news_url, cover_url))
async def load_news_pages(page_num, page_size):
async with MysqlUtil() as conn:
async def update_news_info(news_id, title, news_url, cover_url):
async with MysqlUtil(db="official_web") as conn:
if cover_url:
sql = "UPDATE official_web.news_info " \
"SET title=%s, news_url=%s, cover_url=%s WHERE id=%s;"
return await conn.execute(sql, (news_id, title, news_url,
cover_url))
sql = "UPDATE official_web.news_info " \
"SET title=%s, news_url=%s WHERE id=%s;"
return await conn.execute(sql, (news_id, title, news_url,))
async def load_news_pages(news_type, page_num, page_size):
async with MysqlUtil(db="official_web") as conn:
if news_type:
sql = "SELECT count(*) FROM official_web.news_info " \
"WHERE news_type=%s;"
total = conn.fetch_value(sql, (news_type,))
sql = f"SELECT id, title, author, news_type, pub_time, top " \
f"FROM official_web.news_info " \
f"WHERE news_type=%s " \
f"ORDER BY pub_time DESC, id LIMIT %s OFFSET %s;"
offsets = 0 if page_num == 0 else (page_num - 1) * page_size
articles = await conn.fetchall(sql, (news_type, page_size,
offsets,))
else:
sql = "SELECT count(*) FROM official_web.news_info;"
total = conn.fetch_value(sql)
sql = f"SELECT id, title, pub_time, top " \
sql = f"SELECT id, title, author, news_type, pub_time, top " \
f"FROM official_web.news_info " \
f"ORDER BY pub_time DESC, id LIMIT %s OFFSET %s;"
offsets = 0 if page_num == 0 else (page_num - 1) * page_size
articles = await conn.fetchall(sql, (page_size, offsets))
articles = await conn.fetchall(sql, (page_size, offsets,))
return total, articles
......@@ -6,30 +6,43 @@ DATE:2024/3/5 16:19
from pot_libs.sanic_api import summary
from unify_api.modules.common.components.common_cps import SuccessRsp
from unify_api.modules.qkadmin.components.news_info_cps import (
PubNewsReq, NewsPagesReq, NewsPagesRsp, NewsInfo
PubNewsReq, NewsPagesReq, NewsPagesRsp, NewsInfo, ReviseNewsReq
)
from unify_api.modules.qkadmin.service.news_mgr_srv import (
upload_news_qs, save_news_info, load_news_pages
upload_news_qs, save_news_info, load_news_pages, update_news_info
)
@summary("发布新闻")
@summary("发布文章")
async def post_publish_news(request, body: PubNewsReq) -> SuccessRsp:
title, pub_time = body.title, body.pub_time
title, pub_time, author = body.title, body.pub_time, body.author
is_success, url_article, url_cover = await upload_news_qs(request)
if is_success:
await save_news_info(title, pub_time, url_article, url_cover)
return SuccessRsp(success=1, message="发布新闻成功")
return SuccessRsp(success=1, message="发布文章成功")
return SuccessRsp(success=0, message="发布新闻失败")
return SuccessRsp(success=0, message="发布文章失败")
@summary("后台新闻列表")
@summary("修改文章")
async def post_revise_news(request, body: ReviseNewsReq) -> SuccessRsp:
news_id, title = body.news_id, body.title
is_success, url_article, url_cover = await upload_news_qs(request)
if is_success:
await update_news_info(news_id, title, url_article, url_cover)
return SuccessRsp(success=1, message="修改文章成功")
return SuccessRsp(success=0, message="修改文章失败")
@summary("后台文章列表")
async def get_news_pages(request, body: NewsPagesReq) -> NewsPagesRsp:
page_num = int(body.page_num)
page_size = int(body.page_size)
total, articles = load_news_pages(page_num, page_size)
news_type = int(body.page_size) if body.page_size else 0
total, articles = load_news_pages(news_type, page_num, page_size)
rsp_articles = [
NewsInfo(id=r["id"], title=r["title"], pub_time=r["pub_time"],
NewsInfo(id=r["id"], title=r["title"], author=r["author"],
pub_time=r["pub_time"], news_type=r["news_type"],
top=r["top"]) for r in articles]
return NewsPagesRsp(total=total, articles=rsp_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