UPDATE: api search
This commit is contained in:
@@ -55,6 +55,7 @@ class HistoryModel(BaseModel):
|
||||
|
||||
|
||||
class HistoryByUserModel(BaseModel):
|
||||
id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
|
||||
user_name: Union[str, None] = None
|
||||
created_at: Optional[datetime] = datetime.now(tz=tz)
|
||||
status: str = None
|
||||
|
||||
@@ -8,6 +8,8 @@ from ..models.post import (
|
||||
ShowPostModel
|
||||
)
|
||||
from datetime import datetime, time, timedelta
|
||||
import pytz
|
||||
tz = pytz.timezone('Asia/Ho_Chi_Minh')
|
||||
|
||||
|
||||
class PyObjectId(ObjectId):
|
||||
@@ -31,7 +33,7 @@ class HistoryFindModel(BaseModel):
|
||||
token: str = None
|
||||
sick: List[str] = None
|
||||
authorities: List[str] = None
|
||||
created_at: Optional[datetime] = None
|
||||
created_at: Optional[datetime] = datetime.now(tz=tz)
|
||||
post_id: str = None
|
||||
key_find: str = None
|
||||
|
||||
@@ -58,7 +60,7 @@ class HistoryFindByUserModel(BaseModel):
|
||||
user_name: Union[str, None] = None
|
||||
sick: List[str] = None
|
||||
authorities: List[str] = None
|
||||
created_at: Optional[datetime] = None
|
||||
created_at: Optional[datetime] = datetime.now(tz=tz)
|
||||
post_id: str
|
||||
key_find: str
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ from fastapi import (
|
||||
status,
|
||||
HTTPException
|
||||
)
|
||||
from bson import ObjectId
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
@@ -66,13 +67,14 @@ async def get_list_post_view_by_username(token: str):
|
||||
if user_name == None:
|
||||
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
|
||||
history_find = await db["history"].find({"user_name": user_name}).to_list(100)
|
||||
print(history_find)
|
||||
output = []
|
||||
for dt in history_find:
|
||||
if dt.get("note", None) != '':
|
||||
post = await db["posts"].find_one({"_id": dt.get("note", None)})
|
||||
input = []
|
||||
print(dt)
|
||||
if post == None:
|
||||
dt["data"] = "bài viết đã bị xóa"
|
||||
else:
|
||||
dt["data"] = post["translation_post"]
|
||||
output.append(dt)
|
||||
return output
|
||||
@@ -89,6 +91,8 @@ async def get_key_find_view_by_username(token: str = None):
|
||||
all_user = []
|
||||
out_data = []
|
||||
i = 0
|
||||
if history_find == []:
|
||||
return history_find
|
||||
print(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 = []
|
||||
out_data = []
|
||||
i = 0
|
||||
print(history_find[0])
|
||||
if history_find == []:
|
||||
return history_find
|
||||
|
||||
all_user.append(history_find[0])
|
||||
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:
|
||||
break
|
||||
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")
|
||||
|
||||
@@ -99,16 +99,17 @@ async def create_post(post: PostModel,
|
||||
history_user["user_name"] = data
|
||||
history_user["status"] = "Thêm mới bài viết"
|
||||
history_user["note"] = new_post.inserted_id
|
||||
history_user["data"] = ''
|
||||
del history_user["original_post"]
|
||||
del history_user["translation_post"]
|
||||
del history_user["link"]
|
||||
del history_user["is_active"]
|
||||
# del history_user["created_at"]
|
||||
del history_user["updated_at"]
|
||||
del history_user["point"]
|
||||
del history_user["specialist"]
|
||||
del history_user["data"]
|
||||
del history_user["summary"]
|
||||
history_user["data"] = "post name"
|
||||
print(history_user)
|
||||
history_new = await db["history"].insert_one(history_user)
|
||||
create_history = await db["history"].find_one({"_id": history_new.inserted_id})
|
||||
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(
|
||||
token: TokenModel = None
|
||||
):
|
||||
posts = await db["posts"].find().to_list(1000)
|
||||
posts = await db["posts"].find().sort("created_at", -1).to_list(1000)
|
||||
output = []
|
||||
if token.token != None:
|
||||
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)
|
||||
output = []
|
||||
print("hoangf anh")
|
||||
print(token)
|
||||
# history.created_at = datetime.datetime.now(tz=tz)
|
||||
if token != None:
|
||||
print(9)
|
||||
data_token = await get_current_user(token)
|
||||
data = data_token.get("user_name", None)
|
||||
print(data)
|
||||
user_type = data_token.get("user_type", None)
|
||||
history.created_at = datetime.datetime.now(tz=tz)
|
||||
print(90)
|
||||
history = jsonable_encoder(history)
|
||||
history_user = history
|
||||
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["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)
|
||||
print(check_data)
|
||||
if check_data != []:
|
||||
# print(check_data["count"])
|
||||
# print(type(check_data["count"]))
|
||||
history_user["count"] = int(check_data[0]["count"]) + 1
|
||||
else:
|
||||
history_user["count"] = 1
|
||||
@@ -232,6 +225,23 @@ async def find_list_post(key_find: str, token: str = None, history: HistoryFindM
|
||||
output.append(post)
|
||||
return output
|
||||
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
|
||||
|
||||
|
||||
@@ -250,9 +260,25 @@ async def get_post_by_name(history: HistoryFindModel):
|
||||
if not ROLE_PUBLIC in dt["level"]:
|
||||
if dt["level"] != ["*"]:
|
||||
post["data"].remove(dt)
|
||||
history_find = 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})
|
||||
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:
|
||||
return post
|
||||
else:
|
||||
@@ -273,7 +299,6 @@ async def get_post_by_name(history: HistoryFindModel):
|
||||
if not user_type in dt["level"]:
|
||||
post["data"].remove(dt)
|
||||
history_find = jsonable_encoder(history)
|
||||
print(history_find)
|
||||
count = await db["post_save"].count_documents({"post_id": post["_id"], "username": data})
|
||||
if count != 0:
|
||||
post["post_save"] = True
|
||||
@@ -285,7 +310,7 @@ async def get_post_by_name(history: HistoryFindModel):
|
||||
history_user["user_name"] = data
|
||||
history_user["status"] = "xem bài viết"
|
||||
history_user["note"] = history_user["post_id"]
|
||||
history_user["data"] = ''
|
||||
history_user["data"] = "post name"
|
||||
del history_user["_id"]
|
||||
del history_user["token"]
|
||||
del history_user["sick"]
|
||||
@@ -336,21 +361,20 @@ async def delete_user(id: str,
|
||||
token: str,
|
||||
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 = data_token.get("user_name", None)
|
||||
user_type = data_token.get("user_type", None)
|
||||
if data == None:
|
||||
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)
|
||||
history_user = {}
|
||||
history_user["user_name"] = data
|
||||
history_user["status"] = "xóa bài viết"
|
||||
history_user["note"] = id
|
||||
history_user["data"] = ''
|
||||
history_user["data"] = "post name"
|
||||
history_new = await db["history"].insert_one(history_user)
|
||||
create_history = await db["history"].find_one({"_id": history_new.inserted_id})
|
||||
print(history_new)
|
||||
@@ -538,7 +562,6 @@ async def edit_post(id: str, post: UpdatePostModel, token: str):
|
||||
history_user["user_name"] = data
|
||||
history_user["status"] = "sửa bài viết"
|
||||
history_user["note"] = id
|
||||
history_user["data"] = ""
|
||||
history_user["created_at"] = history_user["updated_at"]
|
||||
del history_user["original_post"]
|
||||
del history_user["translation_post"]
|
||||
@@ -551,6 +574,7 @@ async def edit_post(id: str, post: UpdatePostModel, token: str):
|
||||
del history_user["summary"]
|
||||
print(history_user)
|
||||
del history_user["point"]
|
||||
history_user["data"] = "post_name"
|
||||
history_new = await db["history"].insert_one(history_user)
|
||||
print(history_user)
|
||||
create_history = await db["history"].find_one({"_id": history_new.inserted_id})
|
||||
|
||||
Reference in New Issue
Block a user