UPDATE: api search

This commit is contained in:
2022-08-10 11:23:38 +07:00
parent 9a5512ee38
commit a9b02389bd
4 changed files with 77 additions and 29 deletions

View File

@@ -55,6 +55,7 @@ class HistoryModel(BaseModel):
class HistoryByUserModel(BaseModel): class HistoryByUserModel(BaseModel):
id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
user_name: Union[str, None] = None user_name: Union[str, None] = None
created_at: Optional[datetime] = datetime.now(tz=tz) created_at: Optional[datetime] = datetime.now(tz=tz)
status: str = None status: str = None

View File

@@ -8,6 +8,8 @@ from ..models.post import (
ShowPostModel ShowPostModel
) )
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):
@@ -31,7 +33,7 @@ class HistoryFindModel(BaseModel):
token: str = None token: str = None
sick: List[str] = None sick: List[str] = None
authorities: List[str] = None authorities: List[str] = None
created_at: Optional[datetime] = None created_at: Optional[datetime] = datetime.now(tz=tz)
post_id: str = None post_id: str = None
key_find: str = None key_find: str = None
@@ -58,7 +60,7 @@ class HistoryFindByUserModel(BaseModel):
user_name: Union[str, None] = None user_name: Union[str, None] = None
sick: List[str] = None sick: List[str] = None
authorities: List[str] = None authorities: List[str] = None
created_at: Optional[datetime] = None created_at: Optional[datetime] = datetime.now(tz=tz)
post_id: str post_id: str
key_find: str key_find: str

View File

@@ -4,6 +4,7 @@ from fastapi import (
status, status,
HTTPException HTTPException
) )
from bson import ObjectId
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder from fastapi.encoders import jsonable_encoder
from fastapi.security import OAuth2PasswordRequestForm from fastapi.security import OAuth2PasswordRequestForm
@@ -66,13 +67,14 @@ async def get_list_post_view_by_username(token: str):
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"})
history_find = await db["history"].find({"user_name": user_name}).to_list(100) history_find = await db["history"].find({"user_name": user_name}).to_list(100)
print(history_find)
output = [] output = []
for dt in history_find: for dt in history_find:
if dt.get("note", None) != '': if dt.get("note", None) != '':
post = await db["posts"].find_one({"_id": dt.get("note", None)}) post = await db["posts"].find_one({"_id": dt.get("note", None)})
input = [] input = []
print(dt) if post == None:
dt["data"] = "bài viết đã bị xóa"
else:
dt["data"] = post["translation_post"] dt["data"] = post["translation_post"]
output.append(dt) output.append(dt)
return output return output
@@ -89,6 +91,8 @@ async def get_key_find_view_by_username(token: str = None):
all_user = [] all_user = []
out_data = [] out_data = []
i = 0 i = 0
if history_find == []:
return history_find
print(history_find[0]) print(history_find[0])
all_user.append(history_find[0]) all_user.append(history_find[0])
@@ -106,7 +110,8 @@ async def get_key_find_view_by_username(token: str = None):
all_user = [] all_user = []
out_data = [] out_data = []
i = 0 i = 0
print(history_find[0]) if history_find == []:
return history_find
all_user.append(history_find[0]) all_user.append(history_find[0])
out_data.append(history_find[0]["data"]) out_data.append(history_find[0]["data"])
@@ -118,3 +123,19 @@ async def get_key_find_view_by_username(token: str = None):
if i == 9: if i == 9:
break break
return all_user return all_user
@history.delete("/delete_user_history/", response_description="Delete history by user")
async def delete_user(data: HistoryByUserModel,
# current_user: UserModel = Depends(get_current_user)
token: str,
):
data_token = await get_current_user(token)
user_name = data_token.get("user_name", None)
user_type = data_token.get("user_type", None)
if user_name == None:
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
delete_result = await db["history"].delete_one({"_id": data.id})
if delete_result.deleted_count == 1:
return JSONResponse(status_code=status.HTTP_204_NO_CONTENT, content={"message": "Delete successfull"})
raise HTTPException(status_code=404, detail=f"History not found")

View File

@@ -99,16 +99,17 @@ async def create_post(post: PostModel,
history_user["user_name"] = data history_user["user_name"] = data
history_user["status"] = "Thêm mới bài viết" history_user["status"] = "Thêm mới bài viết"
history_user["note"] = new_post.inserted_id history_user["note"] = new_post.inserted_id
history_user["data"] = ''
del history_user["original_post"] del history_user["original_post"]
del history_user["translation_post"] del history_user["translation_post"]
del history_user["link"] del history_user["link"]
del history_user["is_active"] del history_user["is_active"]
# del history_user["created_at"] del history_user["updated_at"]
del history_user["point"] del history_user["point"]
del history_user["specialist"] del history_user["specialist"]
del history_user["data"] del history_user["data"]
del history_user["summary"] del history_user["summary"]
history_user["data"] = "post name"
print(history_user)
history_new = await db["history"].insert_one(history_user) history_new = await db["history"].insert_one(history_user)
create_history = await db["history"].find_one({"_id": history_new.inserted_id}) create_history = await db["history"].find_one({"_id": history_new.inserted_id})
return JSONResponse(status_code=status.HTTP_201_CREATED, content=created_post) return JSONResponse(status_code=status.HTTP_201_CREATED, content=created_post)
@@ -122,7 +123,7 @@ async def create_post(post: PostModel,
async def list_post( async def list_post(
token: TokenModel = None token: TokenModel = None
): ):
posts = await db["posts"].find().to_list(1000) posts = await db["posts"].find().sort("created_at", -1).to_list(1000)
output = [] output = []
if token.token != None: if token.token != None:
posts = jsonable_encoder(posts) posts = jsonable_encoder(posts)
@@ -189,16 +190,11 @@ async def find_list_post(key_find: str, token: str = None, history: HistoryFindM
] ]
}).sort(age_sort, -1).to_list(100) }).sort(age_sort, -1).to_list(100)
output = [] output = []
print("hoangf anh") # history.created_at = datetime.datetime.now(tz=tz)
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)
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
@@ -206,10 +202,7 @@ async def find_list_post(key_find: str, token: str = None, history: HistoryFindM
history_user["note"] = "" history_user["note"] = ""
history_user["data"] = key_find history_user["data"] = key_find
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) 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)
if check_data != []: if check_data != []:
# print(check_data["count"])
# print(type(check_data["count"]))
history_user["count"] = int(check_data[0]["count"]) + 1 history_user["count"] = int(check_data[0]["count"]) + 1
else: else:
history_user["count"] = 1 history_user["count"] = 1
@@ -232,6 +225,23 @@ async def find_list_post(key_find: str, token: str = None, history: HistoryFindM
output.append(post) output.append(post)
return output return output
else: else:
history = jsonable_encoder(history)
history_user = history
history_user["user_name"] = "anonymous"
history_user["status"] = "Tìm kiếm bài viết theo từ khóa"
history_user["note"] = ""
history_user["data"] = key_find
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)
if check_data != []:
history_user["count"] = int(check_data[0]["count"]) + 1
else:
history_user["count"] = 1
del history_user["_id"]
del history_user["token"]
del history_user["sick"]
del history_user["authorities"]
print(history_user)
history_new = await db["history"].insert_one(history_user)
return posts return posts
@@ -250,9 +260,25 @@ async def get_post_by_name(history: HistoryFindModel):
if not ROLE_PUBLIC in dt["level"]: if not ROLE_PUBLIC in dt["level"]:
if dt["level"] != ["*"]: if dt["level"] != ["*"]:
post["data"].remove(dt) post["data"].remove(dt)
history_find = jsonable_encoder(history)
history = jsonable_encoder(history) history = jsonable_encoder(history)
new_his = await db["history_find"].insert_one(history) count = await db["post_save"].count_documents({"post_id": post["_id"], "username": data})
if count != 0:
post["post_save"] = True
else:
post["post_save"] = False
new_his = await db["history_find"].insert_one(history_find)
created = await db["history_find"].find_one({"_id": new_his.inserted_id}) created = await db["history_find"].find_one({"_id": new_his.inserted_id})
history_user = history_find
history_user["user_name"] = "anonymous"
history_user["status"] = "xem bài viết"
history_user["note"] = history_user["post_id"]
history_user["data"] = "post name"
del history_user["_id"]
del history_user["token"]
del history_user["sick"]
del history_user["authorities"]
history_new = await db["history"].insert_one(history_user)
if post is not None: if post is not None:
return post return post
else: else:
@@ -273,7 +299,6 @@ async def get_post_by_name(history: HistoryFindModel):
if not user_type in dt["level"]: if not user_type in dt["level"]:
post["data"].remove(dt) post["data"].remove(dt)
history_find = jsonable_encoder(history) history_find = jsonable_encoder(history)
print(history_find)
count = await db["post_save"].count_documents({"post_id": post["_id"], "username": data}) count = await db["post_save"].count_documents({"post_id": post["_id"], "username": data})
if count != 0: if count != 0:
post["post_save"] = True post["post_save"] = True
@@ -285,7 +310,7 @@ async def get_post_by_name(history: HistoryFindModel):
history_user["user_name"] = data history_user["user_name"] = data
history_user["status"] = "xem bài viết" history_user["status"] = "xem bài viết"
history_user["note"] = history_user["post_id"] history_user["note"] = history_user["post_id"]
history_user["data"] = '' history_user["data"] = "post name"
del history_user["_id"] del history_user["_id"]
del history_user["token"] del history_user["token"]
del history_user["sick"] del history_user["sick"]
@@ -336,21 +361,20 @@ async def delete_user(id: str,
token: str, token: str,
post: UpdatePostModel = None post: UpdatePostModel = None
): ):
delete_result = await db["posts"].delete_one({"_id": id})
if delete_result.deleted_count == 1:
post = jsonable_encoder(post)
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)
user_type = data_token.get("user_type", None) user_type = data_token.get("user_type", None)
if data == None: if data == None:
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"}) return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
delete_result = await db["posts"].delete_one({"_id": id})
if delete_result.deleted_count == 1:
post = jsonable_encoder(post)
print(data) print(data)
history_user = {} history_user = {}
history_user["user_name"] = data history_user["user_name"] = data
history_user["status"] = "xóa bài viết" history_user["status"] = "xóa bài viết"
history_user["note"] = id history_user["note"] = id
history_user["data"] = '' history_user["data"] = "post name"
history_new = await db["history"].insert_one(history_user) history_new = await db["history"].insert_one(history_user)
create_history = await db["history"].find_one({"_id": history_new.inserted_id}) create_history = await db["history"].find_one({"_id": history_new.inserted_id})
print(history_new) print(history_new)
@@ -538,7 +562,6 @@ async def edit_post(id: str, post: UpdatePostModel, token: str):
history_user["user_name"] = data history_user["user_name"] = data
history_user["status"] = "sửa bài viết" history_user["status"] = "sửa bài viết"
history_user["note"] = id history_user["note"] = id
history_user["data"] = ""
history_user["created_at"] = history_user["updated_at"] history_user["created_at"] = history_user["updated_at"]
del history_user["original_post"] del history_user["original_post"]
del history_user["translation_post"] del history_user["translation_post"]
@@ -551,6 +574,7 @@ async def edit_post(id: str, post: UpdatePostModel, token: str):
del history_user["summary"] del history_user["summary"]
print(history_user) print(history_user)
del history_user["point"] del history_user["point"]
history_user["data"] = "post_name"
history_new = await db["history"].insert_one(history_user) history_new = await db["history"].insert_one(history_user)
print(history_user) print(history_user)
create_history = await db["history"].find_one({"_id": history_new.inserted_id}) create_history = await db["history"].find_one({"_id": history_new.inserted_id})