ADD: lich su tim kiem

This commit is contained in:
2022-08-09 08:43:31 +07:00
parent 25cab24357
commit 9a5512ee38
4 changed files with 59 additions and 19 deletions

View File

@@ -60,6 +60,7 @@ class HistoryByUserModel(BaseModel):
status: str = None status: str = None
note: str = None note: str = None
data: str = None data: str = None
count: int = 0
class Config: class Config:
orm_mode = True orm_mode = True

View File

@@ -3,6 +3,8 @@ from bson import ObjectId
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from typing import List, Optional, Union from typing import List, Optional, Union
from datetime import datetime, time, timedelta from datetime import datetime, time, timedelta
import pytz
tz = pytz.timezone('Asia/Ho_Chi_Minh')
class PyObjectId(ObjectId): class PyObjectId(ObjectId):
@@ -116,7 +118,7 @@ class UpdatePostModel(BaseModel):
translation_post: Union[str, None] = None translation_post: Union[str, None] = None
link: Union[str, None] = None link: Union[str, None] = None
is_active: bool is_active: bool
created_at: Optional[datetime] = None created_at: Optional[datetime] = datetime.now(tz=tz)
updated_at: Optional[datetime] = None updated_at: Optional[datetime] = None
specialist: str specialist: str
summary: str = None summary: str = None

View File

@@ -78,13 +78,43 @@ async def get_list_post_view_by_username(token: str):
return output return output
@history.get("/user_history_key_find", response_description="Get list history key find") @history.get("/user_history_key_find", response_description="Get list history key find", response_model=List[HistoryByUserModel])
async def get_key_find_view_by_username(token: str): async def get_key_find_view_by_username(token: str = None):
if token != None:
data_token = await get_current_user(token) data_token = await get_current_user(token)
user_name = data_token.get("user_name", None) user_name = data_token.get("user_name", None)
if user_name == None: if user_name == None:
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"}) return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
print(0) history_find = await db["history"].find({"user_name": user_name, "status": "Tìm kiếm bài viết theo từ khóa"}).sort("created_at", -1).to_list(1000)
history_find = await db["history"].find({"user_name": user_name, "status": "Tìm kiếm bài viết theo từ khóa"}).to_list(10) all_user = []
print(history) out_data = []
return history_find i = 0
print(history_find[0])
all_user.append(history_find[0])
out_data.append(history_find[0]["data"])
for dt in history_find:
if not dt["data"] in out_data:
all_user.append(dt)
out_data.append(dt["data"])
i = i+1
if i == 9:
break
return all_user
else:
history_find = await db["history"].find({"status": "Tìm kiếm bài viết theo từ khóa"}).sort("count", -1).to_list(1000)
all_user = []
out_data = []
i = 0
print(history_find[0])
all_user.append(history_find[0])
out_data.append(history_find[0]["data"])
for dt in history_find:
if not dt["data"] in out_data:
all_user.append(dt)
out_data.append(dt["data"])
i = i+1
if i == 9:
break
return all_user

View File

@@ -159,7 +159,7 @@ async def list_post(
@post.post( @post.post(
"/find_list_post", response_description="search list posts" "/find_list_post", response_description="search list posts"
) )
async def list_post(key_find: str, token: str = None, history: HistoryFindModel = None): async def find_list_post(key_find: str, token: str = None, history: HistoryFindModel = None):
# point_data = ["point.less10", # point_data = ["point.less10",
# "point.form10to20", # "point.form10to20",
# "point.form20to30", # "point.form20to30",
@@ -189,28 +189,35 @@ async def list_post(key_find: str, token: str = None, history: HistoryFindModel
] ]
}).sort(age_sort, -1).to_list(100) }).sort(age_sort, -1).to_list(100)
output = [] output = []
print("hoangf anh")
print(token)
if token != None: if token != None:
print(9)
data_token = await get_current_user(token) data_token = await get_current_user(token)
data = data_token.get("user_name", None) data = data_token.get("user_name", None)
print(data)
user_type = data_token.get("user_type", None) user_type = data_token.get("user_type", None)
history.created_at = datetime.datetime.now(tz=tz) history.created_at = datetime.datetime.now(tz=tz)
print(90)
history = jsonable_encoder(history) history = jsonable_encoder(history)
history_user = history history_user = history
history_user["user_name"] = data history_user["user_name"] = data
history_user["status"] = "Tìm kiếm bài viết theo từ khóa" history_user["status"] = "Tìm kiếm bài viết theo từ khóa"
history_user["note"] = "" history_user["note"] = ""
history_user["data"] = key_find history_user["data"] = key_find
# check_data = await db["history"].find_one({"data": key_find, "status": "Tìm kiếm bài viết theo từ khóa"}) check_data = await db["history"].find({"data": key_find, "status": "Tìm kiếm bài viết theo từ khóa"}).sort("created_at", -1).to_list(1)
# print(check_data) print(check_data)
# count = check_data.get("count", None) if check_data != []:
# if count != None: # print(check_data["count"])
# history_user["count"] = count + 1 # print(type(check_data["count"]))
# else: history_user["count"] = int(check_data[0]["count"]) + 1
# history_user["count"] = 1 else:
history_user["count"] = 1
del history_user["_id"] del history_user["_id"]
del history_user["token"] del history_user["token"]
del history_user["sick"] del history_user["sick"]
del history_user["authorities"] del history_user["authorities"]
print(history_user)
history_new = await db["history"].insert_one(history_user) history_new = await db["history"].insert_one(history_user)
for post in posts: for post in posts:
if data == None: if data == None: