Commit 3a4c181f authored by ZZH's avatar ZZH

add amber admin 2024-03-6

parent 8cd74ae9
...@@ -483,3 +483,5 @@ TD_TBL_POSTFIX = {"electric": "ele", "soe": "soe", "scope": "scp", ...@@ -483,3 +483,5 @@ TD_TBL_POSTFIX = {"electric": "ele", "soe": "soe", "scope": "scp",
S030_TOPIC = ["electric", "soe", "scope", "adio", "scope", "appliance", S030_TOPIC = ["electric", "soe", "scope", "adio", "scope", "appliance",
"workshop", "log"] "workshop", "log"]
WG_TOPIC = ["tsp", "water", "water_bromake", "ws"] WG_TOPIC = ["tsp", "water", "water_bromake", "ws"]
OSS_NEWS = "filedata/official_web/news_info"
\ No newline at end of file
# -*- coding:utf-8 -*-
"""
DATE:2024/3/6 11:39
"""
# -*- coding:utf-8 -*-
"""
DATE:2024/3/6 11:40
"""
# -*- coding:utf-8 -*-
"""
DATE:2024/3/5 13:56
"""
from dataclasses import dataclass
from pot_libs.sanic_api import Model
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))
@dataclass
class NewsInfoRsp(Model):
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("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")
pub_time: str = Str("发布时间").eg("2023-12-29 18:06")
@dataclass
class NewsPagesReq(Model):
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("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!")
pub_time: str = Str("发布时间").eg("2023-12-29 18:06")
top: int = Opt(Int("是否置顶").eg(0))
contents: str = Opt(Str("新闻内容").eg("喜报丨热烈庆祝清科优能被评定为深圳市“专精特新”企业!"))
cover_url: str = Opt(Str("新闻封面URL").eg("xxxxxxxxxx"))
@dataclass
class NewsPagesRsp(Model):
total: int = Int("文章总数").eg(99)
articles: list = List("文章列表").items(NewsInfo)
# -*- coding:utf-8 -*-
"""
DATE:2024/3/6 11:48
"""
# -*- coding:utf-8 -*-
"""
DATE:2024/3/5 16:27
"""
import pendulum
from uuid import uuid4
from unify_api.constants import OSS_NEWS
from pot_libs.logger import log
from unify_api.modules.common.procedures.file_operate_pds import upload_file
from pot_libs.mysql_util.mysql_util import MysqlUtil
async def upload_news_qs(request):
"""附件上传"""
files = request.files.getlist("file")
url_article, url_cover = "", ""
file_name_lst = [file.name for file in files]
if len(file_name_lst) != len(set(file_name_lst)):
return False, url_article, url_cover
for i, file in enumerate(files):
file_name = file.name
file_name_lst = file_name.rsplit(".", 1)
if len(file_name_lst) <= 1:
return False, url_article, url_cover
file_body = file.body
if file_body:
now_dt = pendulum.now()
yr, month = now_dt.year, now_dt.month
file_url = f"{OSS_NEWS}/{yr}/{month}/{uuid4()}.{file_name_lst[1]}"
log.info(f"准备上传文件 {file_name} {file_url}")
is_upload = await upload_file(file, file_url)
if not is_upload:
return False, url_article, url_cover
if i == 0:
url_article = file_url
else:
url_cover = file_url
else:
return False, url_article, url_cover
log.info(f"上传新闻和封面成功 {url_article} {url_cover}")
return True, url_article, url_cover
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:
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:
sql = "SELECT count(*) FROM official_web.news_info;"
total = conn.fetch_value(sql)
sql = f"SELECT id, title, 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))
return total, articles
# -*- coding:utf-8 -*-
"""
DATE:2024/3/6 11:48
"""
# -*- coding:utf-8 -*-
"""
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.amber_admin.components.news_info_cps import (
PubNewsReq, NewsPagesReq, NewsPagesRsp, NewsInfo
)
from unify_api.modules.amber_admin.service.news_mgr_srv import (
upload_news_qs, save_news_info, load_news_pages
)
@summary("发布新闻")
async def post_publish_news(request, body: PubNewsReq) -> SuccessRsp:
title, pub_time = body.title, body.pub_time
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=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)
rsp_articles = [
NewsInfo(id=r["id"], title=r["title"], pub_time=r["pub_time"],
top=r["top"]) for r in articles]
return NewsPagesRsp(total=total, articles=rsp_articles)
...@@ -2,7 +2,7 @@ from dataclasses import dataclass ...@@ -2,7 +2,7 @@ from dataclasses import dataclass
from pot_libs.common.components.fields import Cid from pot_libs.common.components.fields import Cid
from pot_libs.sanic_api import Model from pot_libs.sanic_api import Model
from pot_libs.sanic_api.column import List, Str from pot_libs.sanic_api.column import List, Str, Int, Opt
@dataclass @dataclass
...@@ -36,3 +36,10 @@ class CidStartEndReq(Model): ...@@ -36,3 +36,10 @@ class CidStartEndReq(Model):
cid: Cid cid: Cid
start: str = Str("开始时间").eg("2021-06-01 00:00:00") start: str = Str("开始时间").eg("2021-06-01 00:00:00")
end: str = Str("结束时间").eg("2021-06-30 23:59:59") end: str = Str("结束时间").eg("2021-06-30 23:59:59")
@dataclass
class SuccessRsp(Model):
success: int = Int('请求成功').eg(1)
message: str = Str("具体错误信息").eg("操作成功")
fid: list = Opt(List("文件列表id,这里的id是字符串").eg(["212122121", "2323232332"]))
# -*- coding:utf-8 -*-
"""
DATE:2024/3/6 13:47
"""
from pot_libs.logger import log
from pot_libs.qingstor_util.qs_client import QsClient
async def upload_file(file, qs_url):
file_body = file.body
if file_body:
async with QsClient() as client:
resp = await client.put_file(key=qs_url, data=file_body)
log.info(f"upload to qingstor the resp: {resp} {qs_url}")
return 1
else:
return None
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