update 8-8-2022
This commit is contained in:
@@ -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):
|
||||
@@ -29,9 +31,11 @@ class PyObjectId(ObjectId):
|
||||
class HistoryModel(BaseModel):
|
||||
id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
|
||||
token: str = None
|
||||
created_at: Optional[datetime] = None
|
||||
created_at: Optional[datetime] = datetime.now(tz=tz)
|
||||
status: str = None
|
||||
note: str = None
|
||||
post_id: str = None
|
||||
key_find: str = None
|
||||
# count: int = 0
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
@@ -43,18 +47,19 @@ class HistoryModel(BaseModel):
|
||||
"example": {
|
||||
"token": "",
|
||||
"status": "",
|
||||
"note": ""
|
||||
"post_id": "",
|
||||
"key_find": ""
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class HistoryByUserModel(BaseModel):
|
||||
id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
|
||||
user_name: Union[str, None] = None
|
||||
created_at: Optional[datetime] = None
|
||||
created_at: Optional[datetime] = datetime.now(tz=tz)
|
||||
status: str = None
|
||||
note: str = None
|
||||
data: str = None
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
@@ -28,7 +28,7 @@ class PyObjectId(ObjectId):
|
||||
|
||||
class SavePostModel(BaseModel):
|
||||
id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
|
||||
user_name: str = None
|
||||
username: str = None
|
||||
# is_active: str = None
|
||||
created_at: Optional[datetime] = None
|
||||
post_id: str = None
|
||||
|
||||
@@ -47,7 +47,6 @@ async def create_history(username: str, status: str, note: str, history: History
|
||||
history_new = await db["history"].insert_one(history)
|
||||
create_history = await db["history"].find_one({"_id": history_new.inserted_id})
|
||||
create_history = jsonable_encoder(create_history)
|
||||
print(type(create_history))
|
||||
# return JSONResponse(status_code=status.HTTP_201_CREATED, content=create_history)
|
||||
return create_history
|
||||
|
||||
@@ -60,11 +59,32 @@ async def list_post():
|
||||
return history_find
|
||||
|
||||
|
||||
@ history.get("/user_history", response_description="Get list Posts viewed")
|
||||
@history.get("/user_history", response_description="Get list history", response_model=List[HistoryByUserModel])
|
||||
async def get_list_post_view_by_username(token: str):
|
||||
data_token = await get_current_user(token)
|
||||
user_name = data_token.get("user_name", None)
|
||||
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)
|
||||
dt["data"] = post["translation_post"]
|
||||
output.append(dt)
|
||||
return output
|
||||
|
||||
|
||||
@history.get("/user_history_key_find", response_description="Get list history key find")
|
||||
async def get_key_find_view_by_username(token: str):
|
||||
data_token = await get_current_user(token)
|
||||
user_name = data_token.get("user_name", None)
|
||||
if user_name == None:
|
||||
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"}).to_list(10)
|
||||
print(history)
|
||||
return history_find
|
||||
|
||||
@@ -52,7 +52,7 @@ async def list_post_in_history_find():
|
||||
return history_find
|
||||
|
||||
|
||||
@ history_find.get("/list_history_by_user_find", response_description="Get list Posts viewed", response_model=List[HistoryFindModel])
|
||||
@history_find.get("/list_history_by_user_find", response_description="Get list Posts viewed", response_model=List[HistoryFindModel])
|
||||
async def get_list_post_view_by_username(username: str, current_user: ShowUserModel = Depends(get_current_user)):
|
||||
history_find = await db["history_find"].find({"username": current_user["username"]}).to_list(10)
|
||||
return history_find
|
||||
|
||||
@@ -60,6 +60,7 @@ async def post_html(content: str, request: Request):
|
||||
async def create_post(post: PostModel,
|
||||
# current_user: UserModel = Depends(get_current_user)
|
||||
# token: TokenModel
|
||||
token: str
|
||||
):
|
||||
try:
|
||||
# print(post)
|
||||
@@ -87,12 +88,35 @@ async def create_post(post: PostModel,
|
||||
created_post = await db["posts"].find_one({"_id": new_post.inserted_id})
|
||||
# aaa = create_history(username=data, status="tạo bài viết",
|
||||
# note=new_post.inserted_id, history=None)
|
||||
# post.created_at = datetime.datetime.now(tz=tz)
|
||||
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"})
|
||||
history_user = post
|
||||
print(history_user)
|
||||
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["point"]
|
||||
del history_user["specialist"]
|
||||
del history_user["data"]
|
||||
del history_user["summary"]
|
||||
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)
|
||||
except NameError:
|
||||
return NameError
|
||||
|
||||
|
||||
@ post.post(
|
||||
@post.post(
|
||||
"/list_post", response_description="List all posts"
|
||||
)
|
||||
async def list_post(
|
||||
@@ -132,11 +156,10 @@ async def list_post(
|
||||
return posts
|
||||
|
||||
|
||||
@ post.post(
|
||||
@post.post(
|
||||
"/find_list_post", response_description="search list posts"
|
||||
)
|
||||
async def list_post(key_find: str, token: TokenModel = None):
|
||||
|
||||
async def list_post(key_find: str, token: str = None, history: HistoryFindModel = None):
|
||||
# point_data = ["point.less10",
|
||||
# "point.form10to20",
|
||||
# "point.form20to30",
|
||||
@@ -166,12 +189,30 @@ async def list_post(key_find: str, token: TokenModel = None):
|
||||
]
|
||||
}).sort(age_sort, -1).to_list(100)
|
||||
output = []
|
||||
if token.token != None:
|
||||
posts = jsonable_encoder(posts)
|
||||
for post in posts:
|
||||
data_token = await get_current_user(token.token)
|
||||
if token != None:
|
||||
data_token = await get_current_user(token)
|
||||
data = data_token.get("user_name", None)
|
||||
user_type = data_token.get("user_type", None)
|
||||
history.created_at = datetime.datetime.now(tz=tz)
|
||||
history = jsonable_encoder(history)
|
||||
history_user = history
|
||||
history_user["user_name"] = data
|
||||
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_one({"data": key_find, "status": "Tìm kiếm bài viết theo từ khóa"})
|
||||
# print(check_data)
|
||||
# count = check_data.get("count", None)
|
||||
# if count != None:
|
||||
# history_user["count"] = count + 1
|
||||
# else:
|
||||
# history_user["count"] = 1
|
||||
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)
|
||||
for post in posts:
|
||||
if data == None:
|
||||
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
|
||||
post = jsonable_encoder(post)
|
||||
@@ -183,9 +224,11 @@ async def list_post(key_find: str, token: TokenModel = None):
|
||||
post["post_save"] = False
|
||||
output.append(post)
|
||||
return output
|
||||
else:
|
||||
return posts
|
||||
|
||||
|
||||
@ post.post(
|
||||
@post.post(
|
||||
"/get_post_by_name"
|
||||
# , response_description="Get a single post", response_model=PostModel
|
||||
)
|
||||
@@ -222,10 +265,26 @@ async def get_post_by_name(history: HistoryFindModel):
|
||||
if not ROLE_PUBLIC in dt["level"]:
|
||||
if not user_type in dt["level"]:
|
||||
post["data"].remove(dt)
|
||||
history = jsonable_encoder(history)
|
||||
new_his = await db["history_find"].insert_one(history)
|
||||
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
|
||||
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})
|
||||
print(post)
|
||||
history_user = history_find
|
||||
history_user["user_name"] = data
|
||||
history_user["status"] = "xem bài viết"
|
||||
history_user["note"] = history_user["post_id"]
|
||||
history_user["data"] = ''
|
||||
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)
|
||||
create_history = await db["history"].find_one({"_id": history_new.inserted_id})
|
||||
if post is not None:
|
||||
return post
|
||||
else:
|
||||
@@ -267,10 +326,29 @@ async def get_post_edit(history: HistoryFindModel):
|
||||
@post.delete("/delete_post/{id}", response_description="Delete a post")
|
||||
async def delete_user(id: str,
|
||||
# current_user: UserModel = Depends(get_current_user)
|
||||
token: str,
|
||||
post: UpdatePostModel = None
|
||||
):
|
||||
|
||||
delete_result = await db["posts"].delete_one({"_id": id})
|
||||
if delete_result.deleted_count == 1:
|
||||
return JSONResponse(status_code=status.HTTP_204_NO_CONTENT)
|
||||
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"})
|
||||
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_new = await db["history"].insert_one(history_user)
|
||||
create_history = await db["history"].find_one({"_id": history_new.inserted_id})
|
||||
print(history_new)
|
||||
print(history_user)
|
||||
return JSONResponse(status_code=status.HTTP_204_NO_CONTENT, content={"message": "Delete successfull"})
|
||||
raise HTTPException(status_code=404, detail=f"Post {id} not found")
|
||||
|
||||
|
||||
@@ -432,8 +510,8 @@ async def video_endpoint(video_name
|
||||
return StreamingResponse(iterfile(), media_type="video/mp4")
|
||||
|
||||
|
||||
@post.post("/edit_post/{id}", response_description="score all post", response_model=UpdatePostModel)
|
||||
async def edit_post(id: str, post: UpdatePostModel):
|
||||
@post.post("/edit_post/{id}", response_description="score all post")
|
||||
async def edit_post(id: str, post: UpdatePostModel, token: str):
|
||||
try:
|
||||
post.updated_at = datetime.datetime.now(tz=tz)
|
||||
post = jsonable_encoder(post)
|
||||
@@ -443,7 +521,33 @@ async def edit_post(id: str, post: UpdatePostModel):
|
||||
post
|
||||
})
|
||||
|
||||
return JSONResponse(status_code=status.HTTP_200_OK, content=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"})
|
||||
history_user = post
|
||||
print(post)
|
||||
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"]
|
||||
del history_user["link"]
|
||||
del history_user["is_active"]
|
||||
# del history_user["created_at"]
|
||||
del history_user["updated_at"]
|
||||
del history_user["specialist"]
|
||||
del history_user["data"]
|
||||
del history_user["summary"]
|
||||
print(history_user)
|
||||
del history_user["point"]
|
||||
history_new = await db["history"].insert_one(history_user)
|
||||
print(history_user)
|
||||
create_history = await db["history"].find_one({"_id": history_new.inserted_id})
|
||||
return JSONResponse(status_code=status.HTTP_200_OK, content={"massege": "update successfull"})
|
||||
except Exception as e:
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
|
||||
@@ -54,16 +54,16 @@ async def create_post(post_save: SavePost):
|
||||
data = data_output.get("user_name", None)
|
||||
if data == None:
|
||||
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
|
||||
count = await db["posts"].count_documents({"post_id": post_save.post_id, "username": data})
|
||||
count = await db["posts"].count_documents({"_id": post_save.post_id})
|
||||
if count == 0:
|
||||
return f"Post {post_save.post_id} not found"
|
||||
return JSONResponse(status_code=status.HTTP_409_CONFLICT, content={"message": "Bài viết không tồn tại"})
|
||||
post_save.created_at = datetime.datetime.now(tz=tz)
|
||||
post_save = jsonable_encoder(post_save)
|
||||
del post_save["token"]
|
||||
post_save["username"] = data
|
||||
created_post = await db["post_save"].count_documents({"post_id": post_save["post_id"], "username": data})
|
||||
if created_post == 1:
|
||||
return {"message": f'Post {post_save["post_id"]} saved'}
|
||||
return JSONResponse(status_code=status.HTTP_409_CONFLICT, content={"message": "Bài viết đã được lưu trước đó"})
|
||||
new_post = await db["post_save"].insert_one(post_save)
|
||||
created_post = await db["post_save"].find_one({"_id": new_post.inserted_id})
|
||||
return JSONResponse(status_code=status.HTTP_201_CREATED, content=created_post)
|
||||
@@ -87,17 +87,15 @@ async def list_post(post_save: SavePost):
|
||||
if data == None:
|
||||
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
|
||||
posts = await db["post_save"].find({"username": data}).to_list(1000)
|
||||
print(posts)
|
||||
print(len(posts))
|
||||
return JSONResponse(status_code=status.HTTP_200_OK, content=posts)
|
||||
|
||||
|
||||
@post_save.delete("/delete_save_post", response_description="Delete save post")
|
||||
async def delete_save_post(post_save: SavePost):
|
||||
async def delete_save_post(post_id: str, token: str):
|
||||
# delete_result = await db["post_save"].delete_one({"_id": user_id})
|
||||
try:
|
||||
url = "https://sandboxapi.ebacsi.com.vn/auth/oauth/check_token"
|
||||
payload = {'token': post_save.token}
|
||||
payload = {'token': token}
|
||||
headers = {
|
||||
'Authorization': 'Basic RGljdGlvbmFyeU1lZGlob21lOlJ4aXR6ZnZvaWFmZmNtb2l0ZW0='
|
||||
}
|
||||
@@ -107,13 +105,10 @@ async def delete_save_post(post_save: SavePost):
|
||||
data = data_output.get("user_name", None)
|
||||
if data == None:
|
||||
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
|
||||
|
||||
post_save = jsonable_encoder(post_save)
|
||||
delete_result = await db["post_save"].delete_one({"post_id": post_save["post_id"], "username": data})
|
||||
delete_result = await db["post_save"].delete_one({"post_id": post_id, "username": data})
|
||||
if delete_result.deleted_count == 1:
|
||||
return JSONResponse(status_code=status.HTTP_200_OK, content={"message": "Delete post save successful"})
|
||||
|
||||
raise HTTPException(
|
||||
status_code=404, detail=f'Post save {post_save["post_id"]} not found')
|
||||
status_code=404, detail=f'Post save {post_id} not found')
|
||||
except NameError:
|
||||
return NameError
|
||||
|
||||
@@ -44,6 +44,7 @@ class TokenModel(BaseModel):
|
||||
token: str = None
|
||||
refresh_token: str = None
|
||||
grant_type: str = None
|
||||
created_at: datetime = None
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
from passlib.context import CryptContext
|
||||
|
||||
import json
|
||||
import datetime
|
||||
from json import JSONEncoder
|
||||
import os
|
||||
import motor.motor_asyncio
|
||||
|
||||
@@ -12,6 +14,14 @@ ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
||||
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
|
||||
class DateTimeEncoder(JSONEncoder):
|
||||
# Override the default method
|
||||
def default(self, obj):
|
||||
if isinstance(obj, (datetime.date, datetime.datetime)):
|
||||
return obj.isoformat()
|
||||
|
||||
|
||||
oauth2_scheme = OAuth2PasswordBearer(
|
||||
tokenUrl="https://sandboxapi.ebacsi.com.vn/auth/oauth/token")
|
||||
ROLE_PUBLIC = "PUBLIC"
|
||||
|
||||
Reference in New Issue
Block a user