save 19.8
This commit is contained in:
7
app/main.py
Executable file → Normal file
7
app/main.py
Executable file → Normal file
@@ -6,20 +6,21 @@ from fastapi import (
|
|||||||
Request,
|
Request,
|
||||||
)
|
)
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from src.dependecies import authenticate_user
|
from src.dependecies import *
|
||||||
from src.routers.routers import router
|
from src.routers.routers import router
|
||||||
from src.routers.post import post
|
from src.routers.post import post
|
||||||
from src.routers.history_find import history_find
|
from src.routers.history_find import history_find
|
||||||
from src.routers.post_save import post_save
|
from src.routers.post_save import post_save
|
||||||
from src.routers.history import history
|
from src.routers.history import history
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
import binascii
|
import binascii
|
||||||
|
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
# ------------------ FastAPI variable ----------------------------------
|
# ------------------ FastAPI variable ----------------------------------
|
||||||
app = FastAPI()
|
|
||||||
|
app = FastAPI(docs_url="/docs",
|
||||||
|
redoc_url="/docs")
|
||||||
|
|
||||||
app.mount("/post", StaticFiles(directory="post"), name="post")
|
app.mount("/post", StaticFiles(directory="post"), name="post")
|
||||||
|
|
||||||
|
|||||||
@@ -74,9 +74,8 @@ class PostModel(BaseModel):
|
|||||||
"original_post": "Joh111",
|
"original_post": "Joh111",
|
||||||
"translation_post": "Doe11111",
|
"translation_post": "Doe11111",
|
||||||
"link": "simple mortal111",
|
"link": "simple mortal111",
|
||||||
"tag": "",
|
"tag": [""],
|
||||||
"is_active": False,
|
"is_active": False,
|
||||||
"created_at": "07/20/22 02: 26: 54",
|
|
||||||
"specialist": "",
|
"specialist": "",
|
||||||
"summary": "",
|
"summary": "",
|
||||||
"data": [
|
"data": [
|
||||||
@@ -136,7 +135,7 @@ class UpdatePostModel(BaseModel):
|
|||||||
"original_post": "Joh111",
|
"original_post": "Joh111",
|
||||||
"translation_post": "Doe11111",
|
"translation_post": "Doe11111",
|
||||||
"link": "simple mortal111",
|
"link": "simple mortal111",
|
||||||
"tag": "",
|
"tag": [""],
|
||||||
"is_active": False,
|
"is_active": False,
|
||||||
"specialist": "",
|
"specialist": "",
|
||||||
"summary": "",
|
"summary": "",
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ from bson.objectid import ObjectId
|
|||||||
import re
|
import re
|
||||||
import pytz
|
import pytz
|
||||||
import datetime
|
import datetime
|
||||||
|
from ..models.models import *
|
||||||
tz = pytz.timezone('Asia/Ho_Chi_Minh')
|
tz = pytz.timezone('Asia/Ho_Chi_Minh')
|
||||||
history = APIRouter()
|
history = APIRouter()
|
||||||
##############################POST###############################################
|
##############################POST###############################################
|
||||||
@@ -46,7 +47,6 @@ async def create_history(username: str, status: str, note: str, history: History
|
|||||||
history["user_name"] = username
|
history["user_name"] = username
|
||||||
history["status"] = status
|
history["status"] = status
|
||||||
history["note"] = note
|
history["note"] = note
|
||||||
print(history)
|
|
||||||
history_new = await db["history"].insert_one(history)
|
history_new = await db["history"].insert_one(history)
|
||||||
create_history = await db["history"].find_one({"_id": history_new.inserted_id})
|
create_history = await db["history"].find_one({"_id": history_new.inserted_id})
|
||||||
create_history = jsonable_encoder(create_history)
|
create_history = jsonable_encoder(create_history)
|
||||||
@@ -58,17 +58,22 @@ async def create_history(username: str, status: str, note: str, history: History
|
|||||||
"/list_history", response_description="List all posts", response_model=List[HistoryByUserModel]
|
"/list_history", response_description="List all posts", response_model=List[HistoryByUserModel]
|
||||||
)
|
)
|
||||||
async def list_post():
|
async def list_post():
|
||||||
history_find = await db["history"].find().to_list(1000)
|
history_find = await db["history"].find().to_list(20)
|
||||||
return history_find
|
return history_find
|
||||||
|
|
||||||
|
|
||||||
@history.get("/user_history", response_description="Get list history", response_model=List[HistoryByUserModel])
|
@history.get("/user_history", response_description="Get list history", response_model=List[HistoryByUserModel])
|
||||||
async def get_list_post_view_by_username(token: str):
|
async def get_list_post_view_by_username(token: token_test = Depends(get_current_user),
|
||||||
data_token = await get_current_user(token)
|
):
|
||||||
user_name = data_token.get("user_name", None)
|
try:
|
||||||
|
user_name = token.get("user_name", None)
|
||||||
|
|
||||||
|
# data_token = await get_current_user(Nonetoken)
|
||||||
|
# data = data_token.get("user_name", None)
|
||||||
|
# user_type = data_token.get("user_type", 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"})
|
||||||
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(20)
|
||||||
output = []
|
output = []
|
||||||
for dt in history_find:
|
for dt in history_find:
|
||||||
if dt.get("note", None) != '':
|
if dt.get("note", None) != '':
|
||||||
@@ -80,23 +85,26 @@ async def get_list_post_view_by_username(token: str):
|
|||||||
dt["data"] = post["translation_post"]
|
dt["data"] = post["translation_post"]
|
||||||
output.append(dt)
|
output.append(dt)
|
||||||
return output
|
return output
|
||||||
|
except NameError:
|
||||||
|
return NameError
|
||||||
|
|
||||||
|
|
||||||
@history.get("/user_history_key_find", response_description="Get list history key find", response_model=List[HistoryByUserModel])
|
@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 = None):
|
async def get_key_find_view_by_username(token: token_test = Depends(get_current_user),
|
||||||
if token != None:
|
):
|
||||||
data_token = await get_current_user(token)
|
|
||||||
user_name = data_token.get("user_name", None)
|
user_name = token.get("user_name", None)
|
||||||
if user_name == None:
|
|
||||||
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
|
# data_token = await get_current_user(Nonetoken)
|
||||||
|
# data = data_token.get("user_name", None)
|
||||||
|
# user_type = data_token.get("user_type", None)
|
||||||
|
if user_name != None:
|
||||||
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"}).sort("created_at", -1).to_list(1000)
|
||||||
all_user = []
|
all_user = []
|
||||||
out_data = []
|
out_data = []
|
||||||
i = 0
|
i = 0
|
||||||
if history_find == []:
|
if history_find == []:
|
||||||
return history_find
|
return history_find
|
||||||
print(history_find[0])
|
|
||||||
|
|
||||||
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"])
|
||||||
for dt in history_find:
|
for dt in history_find:
|
||||||
@@ -128,27 +136,30 @@ async def get_key_find_view_by_username(token: str = None):
|
|||||||
|
|
||||||
|
|
||||||
@history.delete("/delete_user_history/{id}", response_description="Delete history by user")
|
@history.delete("/delete_user_history/{id}", response_description="Delete history by user")
|
||||||
async def delete_user(id: str,
|
async def delete_user_history(id: str,
|
||||||
# current_user: UserModel = Depends(get_current_user)
|
# current_user: UserModel = Depends(get_current_user)
|
||||||
token: str,
|
token: token_test = Depends(get_current_user),
|
||||||
):
|
):
|
||||||
data_token = await get_current_user(token)
|
try:
|
||||||
user_name = data_token.get("user_name", None)
|
user_name = token.get("user_name", None)
|
||||||
user_type = data_token.get("user_type", None)
|
|
||||||
print(ObjectId(id))
|
# data_token = await get_current_user(Nonetoken)
|
||||||
|
# data = data_token.get("user_name", None)
|
||||||
|
# user_type = data_token.get("user_type", 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"})
|
||||||
delete_result = await db["history"].delete_one({"_id": ObjectId(id)})
|
|
||||||
delete = await db["history"].delete_one({"_id": id})
|
delete = await db["history"].delete_one({"_id": id})
|
||||||
print(delete.deleted_count)
|
delete_result = await db["history"].delete_one({"_id": ObjectId(id)})
|
||||||
if delete_result.deleted_count == 1:
|
if delete_result.deleted_count == 1:
|
||||||
return JSONResponse(status_code=status.HTTP_204_NO_CONTENT, content={"message": "Delete successfull"})
|
return JSONResponse(status_code=status.HTTP_204_NO_CONTENT, content={})
|
||||||
elif delete.deleted_count == 1:
|
elif delete.deleted_count == 1:
|
||||||
return JSONResponse(status_code=status.HTTP_204_NO_CONTENT, content={"message": "Delete successfull"})
|
return JSONResponse(status_code=status.HTTP_204_NO_CONTENT, content={})
|
||||||
raise HTTPException(status_code=404, detail=f"History not found")
|
raise HTTPException(status_code=404, detail=f"History not found")
|
||||||
|
except NameError:
|
||||||
|
return NameError
|
||||||
|
|
||||||
|
|
||||||
@history.get("/statistics_key_find", response_description="Get list history key find", response_model=List[HistoryByUserModel])
|
@ history.get("/statistics_key_find", response_description="Get list history key find", response_model=List[HistoryByUserModel])
|
||||||
async def statistics_view_by_username():
|
async def statistics_view_by_username():
|
||||||
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)
|
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 = []
|
all_user = []
|
||||||
@@ -168,10 +179,9 @@ async def statistics_view_by_username():
|
|||||||
return all_user
|
return all_user
|
||||||
|
|
||||||
|
|
||||||
@history.post("/create_khoa", response_description="history", response_model=StatisticalSpecialistModel)
|
@ history.post("/create_khoa", response_description="history", response_model=StatisticalSpecialistModel)
|
||||||
async def create_history(token: str, ss: StatisticalSpecialistModel = None):
|
async def create_history(token: str, ss: StatisticalSpecialistModel = None):
|
||||||
ss = jsonable_encoder(ss)
|
ss = jsonable_encoder(ss)
|
||||||
print(ss)
|
|
||||||
StatisticalSpecialist_new = await db["StatisticalSpecialist"].insert_one(ss)
|
StatisticalSpecialist_new = await db["StatisticalSpecialist"].insert_one(ss)
|
||||||
create_ss = await db["StatisticalSpecialist"].find_one({"_id": StatisticalSpecialist_new.inserted_id})
|
create_ss = await db["StatisticalSpecialist"].find_one({"_id": StatisticalSpecialist_new.inserted_id})
|
||||||
create_ss = jsonable_encoder(create_ss)
|
create_ss = jsonable_encoder(create_ss)
|
||||||
@@ -179,18 +189,17 @@ async def create_history(token: str, ss: StatisticalSpecialistModel = None):
|
|||||||
return create_ss
|
return create_ss
|
||||||
|
|
||||||
|
|
||||||
@history.post("/auto_create_khoa", response_description="history", response_model=List[StatisticalSpecialistModel])
|
@ history.post("/auto_create_khoa", response_description="history", response_model=List[StatisticalSpecialistModel])
|
||||||
async def create_history():
|
async def create_history():
|
||||||
data = []
|
data = []
|
||||||
for i in KHOA:
|
for i in KHOA:
|
||||||
print(i)
|
|
||||||
StatisticalSpecialist_new = await db["StatisticalSpecialist"].insert_one(i)
|
StatisticalSpecialist_new = await db["StatisticalSpecialist"].insert_one(i)
|
||||||
create_ss = await db["StatisticalSpecialist"].find_one({"_id": StatisticalSpecialist_new.inserted_id})
|
create_ss = await db["StatisticalSpecialist"].find_one({"_id": StatisticalSpecialist_new.inserted_id})
|
||||||
data.append(create_ss)
|
data.append(create_ss)
|
||||||
return JSONResponse(status_code=status.HTTP_201_CREATED, content=data)
|
return JSONResponse(status_code=status.HTTP_201_CREATED, content=data)
|
||||||
|
|
||||||
|
|
||||||
@history.get(
|
@ history.get(
|
||||||
"/statistical_specialist", response_description="Statistical Specialist", response_model=List[StatisticalSpecialistModel]
|
"/statistical_specialist", response_description="Statistical Specialist", response_model=List[StatisticalSpecialistModel]
|
||||||
)
|
)
|
||||||
async def list_post():
|
async def list_post():
|
||||||
@@ -198,7 +207,7 @@ async def list_post():
|
|||||||
return StatisticalSpecialist
|
return StatisticalSpecialist
|
||||||
|
|
||||||
|
|
||||||
@history.delete("/statistical_specialist111"
|
@ history.delete("/statistical_specialist111"
|
||||||
)
|
)
|
||||||
async def delete():
|
async def delete():
|
||||||
StatisticalSpecialist = await db["StatisticalSpecialist"].delete_many({})
|
StatisticalSpecialist = await db["StatisticalSpecialist"].delete_many({})
|
||||||
|
|||||||
@@ -63,7 +63,6 @@ async def get_list_post_view_by_username(username: str, current_user: ShowUserMo
|
|||||||
# history_find = await db["history_find"].find({"username": username}).to_list(10)
|
# history_find = await db["history_find"].find({"username": username}).to_list(10)
|
||||||
# post_view = []
|
# post_view = []
|
||||||
# for dt in history_find:
|
# for dt in history_find:
|
||||||
# print(dt["post_id"])
|
|
||||||
# post = await db["posts"].find_one({"_id": dt["post_id"]})
|
# post = await db["posts"].find_one({"_id": dt["post_id"]})
|
||||||
# post_view.append(post)
|
# post_view.append(post)
|
||||||
# return post_view
|
# return post_view
|
||||||
@@ -74,7 +73,6 @@ async def get_list_post_view_by_username(current_user: ShowUserModel = Depends(g
|
|||||||
history_find = await db["history_find"].find({"username": current_user["username"]}).to_list(10)
|
history_find = await db["history_find"].find({"username": current_user["username"]}).to_list(10)
|
||||||
post_view = []
|
post_view = []
|
||||||
for dt in history_find:
|
for dt in history_find:
|
||||||
print(dt["post_id"])
|
|
||||||
post = await db["posts"].find_one({"_id": dt["post_id"]})
|
post = await db["posts"].find_one({"_id": dt["post_id"]})
|
||||||
dt["post"] = post
|
dt["post"] = post
|
||||||
return history_find
|
return history_find
|
||||||
|
|||||||
@@ -56,18 +56,20 @@ async def post_html(content: str, request: Request):
|
|||||||
|
|
||||||
|
|
||||||
@post.post("/create_post", response_description="Add new post", response_model=PostModel)
|
@post.post("/create_post", response_description="Add new post", response_model=PostModel)
|
||||||
async def create_post(post: PostModel,
|
async def create_post(
|
||||||
|
post: PostModel,
|
||||||
# current_user: UserModel = Depends(get_current_user)
|
# current_user: UserModel = Depends(get_current_user)
|
||||||
# token: TokenModel
|
# token: TokenModel
|
||||||
token: str
|
token: token_test = Depends(get_current_user),
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
# print(post)
|
user_name = token.get("user_name", None)
|
||||||
|
|
||||||
# data_token = await get_current_user(Nonetoken)
|
# data_token = await get_current_user(Nonetoken)
|
||||||
# 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 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"})
|
||||||
|
|
||||||
# post.created_at = datetime.datetime.now(tz=tz)
|
# post.created_at = datetime.datetime.now(tz=tz)
|
||||||
post = jsonable_encoder(post)
|
post = jsonable_encoder(post)
|
||||||
@@ -107,7 +109,6 @@ async def create_post(post: PostModel,
|
|||||||
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"})
|
||||||
history_user = post
|
history_user = post
|
||||||
print(history_user)
|
|
||||||
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
|
||||||
@@ -121,7 +122,6 @@ async def create_post(post: PostModel,
|
|||||||
del history_user["data"]
|
del history_user["data"]
|
||||||
del history_user["summary"]
|
del history_user["summary"]
|
||||||
history_user["data"] = "post name"
|
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)
|
||||||
@@ -133,38 +133,28 @@ async def create_post(post: PostModel,
|
|||||||
"/list_post", response_description="List all posts"
|
"/list_post", response_description="List all posts"
|
||||||
)
|
)
|
||||||
async def list_post(
|
async def list_post(
|
||||||
token: TokenModel = None,
|
token: token_test = Depends(get_current_user),
|
||||||
|
is_active: bool = True,
|
||||||
page: int = 0,
|
page: int = 0,
|
||||||
limit: int = 10,
|
limit: int = 10,
|
||||||
):
|
):
|
||||||
posts = await db["posts"].find().sort("created_at", -1).sort("point.total", -1).skip(page*limit).to_list(limit)
|
|
||||||
|
if is_active == True:
|
||||||
|
posts = await db["posts"].find({"is_active": is_active}).sort("created_at", -1).sort("point.total", -1).skip(page*limit).to_list(limit)
|
||||||
|
count_total = await db["posts"].count_documents({"is_active": is_active})
|
||||||
|
else:
|
||||||
|
posts = await db["posts"].find().sort("created_at", -1).sort("is_active", -1).sort("point.total", -1).skip(page*limit).to_list(limit)
|
||||||
count_total = await db["posts"].count_documents({})
|
count_total = await db["posts"].count_documents({})
|
||||||
|
|
||||||
# print(count)
|
|
||||||
output = []
|
output = []
|
||||||
|
user_name = token.get("user_name", None)
|
||||||
if token.token != None:
|
if token != None:
|
||||||
posts = jsonable_encoder(posts)
|
posts = jsonable_encoder(posts)
|
||||||
for post in posts:
|
for post in posts:
|
||||||
url = URL_OAUTH2_GET_CHECK_TOKEN
|
if user_name == None:
|
||||||
|
|
||||||
payload = {'token': token.token}
|
|
||||||
|
|
||||||
headers = {
|
|
||||||
'Authorization': 'Basic RGljdGlvbmFyeU1lZGlob21lOlJ4aXR6ZnZvaWFmZmNtb2l0ZW0='
|
|
||||||
}
|
|
||||||
|
|
||||||
response = requests.request(
|
|
||||||
"POST", url, headers=headers, data=payload)
|
|
||||||
data_token = json.loads(response.text)
|
|
||||||
|
|
||||||
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"})
|
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
|
||||||
post = jsonable_encoder(post)
|
post = jsonable_encoder(post)
|
||||||
|
|
||||||
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": user_name})
|
||||||
if count != 0:
|
if count != 0:
|
||||||
post["post_save"] = True
|
post["post_save"] = True
|
||||||
else:
|
else:
|
||||||
@@ -184,7 +174,14 @@ 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 find_list_post(page: int = 0, limit: int = 10, key_find: str = None, token: str = None, history: HistoryFindModel = None):
|
async def find_list_post(
|
||||||
|
page: int = 0,
|
||||||
|
limit: int = 10,
|
||||||
|
key_find: str = None,
|
||||||
|
history: HistoryFindModel = None,
|
||||||
|
token: token_test = Depends(get_current_user),
|
||||||
|
):
|
||||||
|
user_name = token.get("user_name", None)
|
||||||
# point_data = ["point.less10",
|
# point_data = ["point.less10",
|
||||||
# "point.form10to20",
|
# "point.form10to20",
|
||||||
# "point.form20to30",
|
# "point.form20to30",
|
||||||
@@ -205,7 +202,10 @@ async def find_list_post(page: int = 0, limit: int = 10, key_find: str = None, t
|
|||||||
|
|
||||||
# ]).sort(age_sort, -1).to_list(100)
|
# ]).sort(age_sort, -1).to_list(100)
|
||||||
|
|
||||||
posts = await db["posts"].find({
|
posts = await db["posts"].find(
|
||||||
|
{"$and":
|
||||||
|
[
|
||||||
|
{
|
||||||
"$or": [
|
"$or": [
|
||||||
{"translation_post": {"$regex": key_find, "$options": 'i'}},
|
{"translation_post": {"$regex": key_find, "$options": 'i'}},
|
||||||
{"data.content": {"$regex": key_find, "$options": 'i'}},
|
{"data.content": {"$regex": key_find, "$options": 'i'}},
|
||||||
@@ -213,8 +213,15 @@ async def find_list_post(page: int = 0, limit: int = 10, key_find: str = None, t
|
|||||||
{"summary": {"$regex": key_find, "$options": 'i'}},
|
{"summary": {"$regex": key_find, "$options": 'i'}},
|
||||||
{"tag": {"$regex": key_find, "$options": 'xi'}},
|
{"tag": {"$regex": key_find, "$options": 'xi'}},
|
||||||
]
|
]
|
||||||
}).sort("point.total", -1).skip(page*limit).to_list(limit)
|
},
|
||||||
count_total = await db["posts"].count_documents({
|
{"is_active": True}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
).sort("point.total", -1).skip(page*limit).to_list(limit)
|
||||||
|
count_total = await db["posts"].count_documents(
|
||||||
|
{"$and":
|
||||||
|
[
|
||||||
|
{
|
||||||
"$or": [
|
"$or": [
|
||||||
{"translation_post": {"$regex": key_find, "$options": 'i'}},
|
{"translation_post": {"$regex": key_find, "$options": 'i'}},
|
||||||
{"data.content": {"$regex": key_find, "$options": 'i'}},
|
{"data.content": {"$regex": key_find, "$options": 'i'}},
|
||||||
@@ -222,16 +229,17 @@ async def find_list_post(page: int = 0, limit: int = 10, key_find: str = None, t
|
|||||||
{"summary": {"$regex": key_find, "$options": 'i'}},
|
{"summary": {"$regex": key_find, "$options": 'i'}},
|
||||||
{"tag": {"$regex": key_find, "$options": 'xi'}},
|
{"tag": {"$regex": key_find, "$options": 'xi'}},
|
||||||
]
|
]
|
||||||
})
|
},
|
||||||
|
{"is_active": True}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
)
|
||||||
output = []
|
output = []
|
||||||
# history.created_at = datetime.datetime.now(tz=tz)
|
# history.created_at = datetime.datetime.now(tz=tz)
|
||||||
if token != None:
|
if user_name != None:
|
||||||
data_token = await get_current_user(token)
|
|
||||||
data = data_token.get("user_name", None)
|
|
||||||
user_type = data_token.get("user_type", None)
|
|
||||||
history = jsonable_encoder(history)
|
history = jsonable_encoder(history)
|
||||||
history_user = history
|
history_user = history
|
||||||
history_user["user_name"] = data
|
history_user["user_name"] = user_name
|
||||||
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
|
||||||
@@ -244,14 +252,13 @@ async def find_list_post(page: int = 0, limit: int = 10, key_find: str = None, t
|
|||||||
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 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"})
|
||||||
post = jsonable_encoder(post)
|
post = jsonable_encoder(post)
|
||||||
|
|
||||||
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": user_name})
|
||||||
if count != 0:
|
if count != 0:
|
||||||
post["post_save"] = True
|
post["post_save"] = True
|
||||||
else:
|
else:
|
||||||
@@ -277,7 +284,6 @@ async def find_list_post(page: int = 0, limit: int = 10, key_find: str = None, t
|
|||||||
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)
|
||||||
return {
|
return {
|
||||||
"total": count_total,
|
"total": count_total,
|
||||||
@@ -339,7 +345,6 @@ async def find_list_post(key_find: str, token: str = None, history: HistoryFindM
|
|||||||
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:
|
||||||
@@ -369,7 +374,6 @@ async def find_list_post(key_find: str, token: str = None, history: HistoryFindM
|
|||||||
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)
|
||||||
return posts
|
return posts
|
||||||
|
|
||||||
@@ -378,20 +382,20 @@ async def find_list_post(key_find: str, token: str = None, history: HistoryFindM
|
|||||||
"/get_post_by_name"
|
"/get_post_by_name"
|
||||||
# , response_description="Get a single post", response_model=PostModel
|
# , response_description="Get a single post", response_model=PostModel
|
||||||
)
|
)
|
||||||
async def get_post_by_name(history: HistoryFindModel):
|
async def get_post_by_name(history: HistoryFindModel = None, access: token_test = Depends(get_current_user),):
|
||||||
try:
|
try:
|
||||||
# history.created_at = datetime.datetime.now(tz=tz)
|
|
||||||
|
user_name = access.get("user_name", None)
|
||||||
history = jsonable_encoder(history)
|
history = jsonable_encoder(history)
|
||||||
token = history.get("token", None)
|
if user_name == None:
|
||||||
if token == '':
|
|
||||||
post = await db["posts"].find_one({"_id": history["post_id"]})
|
post = await db["posts"].find_one({"_id": history["post_id"]})
|
||||||
for dt in post["data"]:
|
for dt in post["data"]:
|
||||||
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_find = jsonable_encoder(history)
|
||||||
history = jsonable_encoder(history)
|
# history = jsonable_encoder(history)
|
||||||
check_data = await db["history"].find({"post_id": post["_id"], "username": data, "status": "xem bài viết"}).sort("created_at", -1).to_list(1)
|
check_data = await db["history"].find({"post_id": post["_id"], "username": user_name, "status": "xem bài viết"}).sort("created_at", -1).to_list(1)
|
||||||
if check_data != []:
|
if check_data != []:
|
||||||
history_user["count"] = int(check_data[0]["count"]) + 1
|
history_user["count"] = int(check_data[0]["count"]) + 1
|
||||||
else:
|
else:
|
||||||
@@ -420,9 +424,8 @@ async def get_post_by_name(history: HistoryFindModel):
|
|||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
content={'message': "Bài viết không tồn tại"}
|
content={'message': "Bài viết không tồn tại"}
|
||||||
)
|
)
|
||||||
data_token = await get_current_user(token)
|
|
||||||
data = data_token.get("user_name", None)
|
user_type = access.get("user_type", None)
|
||||||
user_type = data_token.get("user_type", None)
|
|
||||||
if "ADMIN" == user_type:
|
if "ADMIN" == user_type:
|
||||||
user_type = "ORG_ADMIN"
|
user_type = "ORG_ADMIN"
|
||||||
if "PATIENT" == user_type:
|
if "PATIENT" == user_type:
|
||||||
@@ -431,7 +434,7 @@ async def get_post_by_name(history: HistoryFindModel):
|
|||||||
user_type = "ORG_OPERATOR"
|
user_type = "ORG_OPERATOR"
|
||||||
if "RECEIPTION" == user_type:
|
if "RECEIPTION" == user_type:
|
||||||
user_type = "SITE_RECEIPTION"
|
user_type = "SITE_RECEIPTION"
|
||||||
if data == 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"})
|
||||||
else:
|
else:
|
||||||
post = await db["posts"].find_one({"_id": history["post_id"]})
|
post = await db["posts"].find_one({"_id": history["post_id"]})
|
||||||
@@ -441,7 +444,7 @@ 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)
|
||||||
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": user_name})
|
||||||
if count != 0:
|
if count != 0:
|
||||||
post["post_save"] = True
|
post["post_save"] = True
|
||||||
else:
|
else:
|
||||||
@@ -449,7 +452,7 @@ async def get_post_by_name(history: HistoryFindModel):
|
|||||||
new_his = await db["history_find"].insert_one(history_find)
|
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 = history_find
|
||||||
history_user["user_name"] = data
|
history_user["user_name"] = user_name
|
||||||
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"] = "post name"
|
history_user["data"] = "post name"
|
||||||
@@ -457,7 +460,7 @@ async def get_post_by_name(history: HistoryFindModel):
|
|||||||
del history_user["token"]
|
del history_user["token"]
|
||||||
del history_user["sick"]
|
del history_user["sick"]
|
||||||
del history_user["authorities"]
|
del history_user["authorities"]
|
||||||
check_data = await db["history"].find({"post_id": post["_id"], "username": data, "status": "xem bài viết"}).sort("created_at", -1).to_list(1)
|
check_data = await db["history"].find({"post_id": post["_id"], "username": user_name, "status": "xem bài viết"}).sort("created_at", -1).to_list(1)
|
||||||
if check_data != []:
|
if check_data != []:
|
||||||
history_user["count"] = int(check_data[0]["count"]) + 1
|
history_user["count"] = int(check_data[0]["count"]) + 1
|
||||||
else:
|
else:
|
||||||
@@ -472,6 +475,7 @@ async def get_post_by_name(history: HistoryFindModel):
|
|||||||
content={'message': "Bài viết không tồn tại"}
|
content={'message': "Bài viết không tồn tại"}
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
content={'message': str(e)}
|
content={'message': str(e)}
|
||||||
@@ -481,9 +485,11 @@ async def get_post_by_name(history: HistoryFindModel):
|
|||||||
@post.post(
|
@post.post(
|
||||||
"/get_post_edit"
|
"/get_post_edit"
|
||||||
)
|
)
|
||||||
async def get_post_edit(history: HistoryFindModel):
|
async def get_post_edit(history: HistoryFindModel, token: token_test = Depends(get_current_user),):
|
||||||
try:
|
try:
|
||||||
# history.created_at = datetime.datetime.now(tz=tz)
|
user_name = token.get("user_name", None)
|
||||||
|
if user_name == None:
|
||||||
|
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
|
||||||
history = jsonable_encoder(history)
|
history = jsonable_encoder(history)
|
||||||
post = await db["posts"].find_one({"_id": history["post_id"]})
|
post = await db["posts"].find_one({"_id": history["post_id"]})
|
||||||
new_his = await db["history_find"].insert_one(history)
|
new_his = await db["history_find"].insert_one(history)
|
||||||
@@ -505,41 +511,39 @@ async def get_post_edit(history: HistoryFindModel):
|
|||||||
@post.delete("/delete_post/{id}", response_description="Delete a post")
|
@post.delete("/delete_post/{id}", response_description="Delete a post")
|
||||||
async def delete_user(id: str,
|
async def delete_user(id: str,
|
||||||
# current_user: UserModel = Depends(get_current_user)
|
# current_user: UserModel = Depends(get_current_user)
|
||||||
token: str,
|
token: token_test = Depends(get_current_user),):
|
||||||
post: UpdatePostModel = None
|
|
||||||
):
|
# post.updated_at = datetime.datetime.now(tz=tz)
|
||||||
data_token = await get_current_user(token)
|
user_name = token.get("user_name", None)
|
||||||
data = data_token.get("user_name", None)
|
if 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"})
|
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
|
||||||
delete_result = await db["posts"].delete_one({"_id": id})
|
delete_result = await db["posts"].delete_one({"_id": id})
|
||||||
if delete_result.deleted_count == 1:
|
if delete_result.deleted_count == 1:
|
||||||
post = jsonable_encoder(post)
|
# post = jsonable_encoder(post)
|
||||||
print(data)
|
delete_post_save = await db["post_save"].delete_many({"post_id": id})
|
||||||
history_user = {}
|
history_user = {}
|
||||||
history_user["user_name"] = data
|
history_user["user_name"] = user_name
|
||||||
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"] = "post name"
|
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)
|
return JSONResponse(status_code=status.HTTP_204_NO_CONTENT, content=True)
|
||||||
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")
|
raise HTTPException(status_code=404, detail=f"Post {id} not found")
|
||||||
|
|
||||||
|
|
||||||
@post.get("/score", response_description="score all post", response_model=List[ShowPostModel])
|
@post.get("/score", response_description="score all post", response_model=List[ShowPostModel])
|
||||||
async def score_all_post(
|
async def score_all_post(
|
||||||
# current_user: UserModel = Depends(get_current_user)
|
token: token_test = Depends(get_current_user),
|
||||||
):
|
):
|
||||||
|
|
||||||
|
user_name = token.get("user_name", None)
|
||||||
|
if user_name == None:
|
||||||
|
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
|
||||||
posts = await db["posts"].find().to_list(1000)
|
posts = await db["posts"].find().to_list(1000)
|
||||||
print(post)
|
|
||||||
for dt_post in posts:
|
for dt_post in posts:
|
||||||
data_old = dt_post
|
data_old = dt_post
|
||||||
dt_post["point"]["total"] = await db["history_find"].count_documents({"post_id": dt_post["_id"]})
|
dt_post["point"]["total"] = await db["history_find"].count_documents({"post_id": dt_post["_id"]})
|
||||||
# await db["posts"].update_one(data_old, dt_post)
|
|
||||||
await db["posts"].update_one({"_id": dt_post["_id"]}, {"$set": {
|
await db["posts"].update_one({"_id": dt_post["_id"]}, {"$set": {
|
||||||
"point": {
|
"point": {
|
||||||
|
|
||||||
@@ -550,15 +554,7 @@ async def score_all_post(
|
|||||||
return posts
|
return posts
|
||||||
|
|
||||||
|
|
||||||
# @post.post("/uploadfiles/")
|
# @post.post("/uploadfiles/")data
|
||||||
# async def create_upload_files(
|
|
||||||
# files: List[UploadFile] = File(...),
|
|
||||||
# # current_user: UserModel = Depends(get_current_user)
|
|
||||||
# ):
|
|
||||||
# try:
|
|
||||||
# file_name = []
|
|
||||||
# i = 0
|
|
||||||
# file_location = f"../media/"
|
|
||||||
# for file in files:
|
# for file in files:
|
||||||
# current_time = datetime.datetime.now(tz=tz)
|
# current_time = datetime.datetime.now(tz=tz)
|
||||||
# file_save = file_location + current_time + str(i) + file.filename
|
# file_save = file_location + current_time + str(i) + file.filename
|
||||||
@@ -657,24 +653,20 @@ async def create_upload_post(
|
|||||||
|
|
||||||
|
|
||||||
@post.post("/edit_post/{id}", response_description="score all post")
|
@post.post("/edit_post/{id}", response_description="score all post")
|
||||||
async def edit_post(id: str, post: UpdatePostModel, token: str):
|
async def edit_post(id: str, post: UpdatePostModel, token: token_test = Depends(get_current_user),):
|
||||||
try:
|
try:
|
||||||
# post.updated_at = datetime.datetime.now(tz=tz)
|
# post.updated_at = datetime.datetime.now(tz=tz)
|
||||||
|
user_name = token.get("user_name", None)
|
||||||
|
if user_name == None:
|
||||||
|
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
|
||||||
post = jsonable_encoder(post)
|
post = jsonable_encoder(post)
|
||||||
created_post = await db["posts"].find_one({"_id": id})
|
created_post = await db["posts"].find_one({"_id": id})
|
||||||
post["created_at"] = created_post["created_at"]
|
post["created_at"] = created_post["created_at"]
|
||||||
await db["posts"].update_one({"_id": id}, {"$set":
|
await db["posts"].update_one({"_id": id}, {"$set":
|
||||||
post
|
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
|
history_user = post
|
||||||
print(post)
|
history_user["user_name"] = user_name
|
||||||
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["created_at"] = history_user["updated_at"]
|
history_user["created_at"] = history_user["updated_at"]
|
||||||
@@ -687,11 +679,9 @@ async def edit_post(id: str, post: UpdatePostModel, token: str):
|
|||||||
del history_user["specialist"]
|
del history_user["specialist"]
|
||||||
del history_user["data"]
|
del history_user["data"]
|
||||||
del history_user["summary"]
|
del history_user["summary"]
|
||||||
print(history_user)
|
|
||||||
del history_user["point"]
|
del history_user["point"]
|
||||||
history_user["data"] = "post_name"
|
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)
|
|
||||||
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_200_OK, content={"massege": "update successfull"})
|
return JSONResponse(status_code=status.HTTP_200_OK, content={"massege": "update successfull"})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -757,7 +747,6 @@ async def find_list_post(status: str = None, specialist: str = None, key_find: s
|
|||||||
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:
|
||||||
@@ -795,7 +784,7 @@ async def find_list_post(status: str = None, specialist: str = None, key_find: s
|
|||||||
"/find_post_save", response_description="search list posts save"
|
"/find_post_save", response_description="search list posts save"
|
||||||
)
|
)
|
||||||
async def find_post_save(
|
async def find_post_save(
|
||||||
token: str,
|
token: token_test = Depends(get_current_user),
|
||||||
page: int = 0,
|
page: int = 0,
|
||||||
limit: int = 10,
|
limit: int = 10,
|
||||||
# key_find: str = None,
|
# key_find: str = None,
|
||||||
@@ -809,6 +798,7 @@ async def find_post_save(
|
|||||||
# "point.form50to60",
|
# "point.form50to60",
|
||||||
# ]
|
# ]
|
||||||
age_sort = "point.total"
|
age_sort = "point.total"
|
||||||
|
print(11111)
|
||||||
# history = jsonable_encoder(history)
|
# history = jsonable_encoder(history)
|
||||||
# if history.get("age", None) != None:
|
# if history.get("age", None) != None:
|
||||||
# if history.get("age") > 59:
|
# if history.get("age") > 59:
|
||||||
@@ -818,7 +808,6 @@ async def find_post_save(
|
|||||||
# posts = await db["posts"].find("$or": [
|
# posts = await db["posts"].find("$or": [
|
||||||
# {"translation_post": {"$regex": key_find}},
|
# {"translation_post": {"$regex": key_find}},
|
||||||
# {"translation_post": {"$regex": key_find}},
|
# {"translation_post": {"$regex": key_find}},
|
||||||
# print(key_find)
|
|
||||||
# # ]).sort(age_sort, -1).to_list(100)
|
# # ]).sort(age_sort, -1).to_list(100)
|
||||||
# if key_find != None:
|
# if key_find != None:
|
||||||
# posts = await db["posts"].find({
|
# posts = await db["posts"].find({
|
||||||
@@ -831,21 +820,21 @@ async def find_post_save(
|
|||||||
# ]
|
# ]
|
||||||
# }).sort("point.total", -1).skip(page*limit).to_list(limit)
|
# }).sort("point.total", -1).skip(page*limit).to_list(limit)
|
||||||
# else:
|
# else:
|
||||||
posts = await db["posts"].find().sort("point.total", -1).skip(page*limit).to_list(limit)
|
# posts = await db["posts"].find().sort("point.total", -1).skip(page*limit).to_list(limit)
|
||||||
count_total = 0
|
|
||||||
output = []
|
output = []
|
||||||
# history.created_at = datetime.datetime.now(tz=tz)
|
# history.created_at = datetime.datetime.now(tz=tz)
|
||||||
if token != None:
|
user_name = token.get("user_name", None)
|
||||||
data_token = await get_current_user(token)
|
if user_name != None:
|
||||||
data = data_token.get("user_name", None)
|
posts_save = await db["post_save"].find({"username": user_name}).skip(page*limit).to_list(limit)
|
||||||
user_type = data_token.get("user_type", None)
|
count_total = await db["post_save"].count_documents({"username": user_name})
|
||||||
history = jsonable_encoder(history)
|
history = jsonable_encoder(history)
|
||||||
history_user = history
|
history_user = history
|
||||||
history_user["user_name"] = data
|
history_user["user_name"] = user_name
|
||||||
history_user["status"] = "Xem danh sách bài viết đã lưu"
|
history_user["status"] = "Xem danh sách bài viết đã lưu"
|
||||||
history_user["note"] = ""
|
history_user["note"] = ""
|
||||||
history_user["data"] = ""
|
history_user["data"] = ""
|
||||||
check_data = await db["history"].find({"user_name": data, "status": "Xem danh sách bài viết đã lưu"}).sort("created_at", -1).to_list(1)
|
check_data = await db["history"].find({"user_name": user_name, "status": "Xem danh sách bài viết đã lưu"}).sort("created_at", -1).to_list(1)
|
||||||
if check_data != []:
|
if check_data != []:
|
||||||
history_user["count"] = int(check_data[0]["count"]) + 1
|
history_user["count"] = int(check_data[0]["count"]) + 1
|
||||||
else:
|
else:
|
||||||
@@ -854,20 +843,19 @@ async def find_post_save(
|
|||||||
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_save:
|
||||||
if data == None:
|
|
||||||
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
|
|
||||||
post = jsonable_encoder(post)
|
post = jsonable_encoder(post)
|
||||||
|
count = await db["posts"].find_one({"_id": post["post_id"]})
|
||||||
count = await db["post_save"].count_documents({"post_id": post["_id"], "username": data})
|
if count == None:
|
||||||
if count != 0:
|
print(post["post_id"])
|
||||||
post["post_save"] = True
|
count["post_save"] = True
|
||||||
output.append(post)
|
output.append(count)
|
||||||
count_total = count_total + 1
|
print(output)
|
||||||
return {
|
return {
|
||||||
"total": count_total,
|
"total": count_total,
|
||||||
"data": output,
|
"data": output,
|
||||||
}
|
}
|
||||||
|
else:
|
||||||
|
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ from ..settings import *
|
|||||||
import json
|
import json
|
||||||
from typing import List
|
from typing import List
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
from ..models.models import *
|
||||||
import requests
|
import requests
|
||||||
import re
|
import re
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
@@ -41,27 +41,25 @@ post_save = APIRouter()
|
|||||||
|
|
||||||
|
|
||||||
@post_save.post("/save_post", response_description="save new post", response_model=SavePostModel)
|
@post_save.post("/save_post", response_description="save new post", response_model=SavePostModel)
|
||||||
async def create_post(post_save: SavePost):
|
async def create_post(post_save: SavePost, token: token_test = Depends(get_current_user),
|
||||||
|
):
|
||||||
try:
|
try:
|
||||||
url = URL_OAUTH2_GET_CHECK_TOKEN
|
user_name = token.get("user_name", None)
|
||||||
payload = {'token': post_save.token}
|
|
||||||
headers = {
|
# data_token = await get_current_user(Nonetoken)
|
||||||
'Authorization': 'Basic RGljdGlvbmFyeU1lZGlob21lOlJ4aXR6ZnZvaWFmZmNtb2l0ZW0='
|
# data = data_token.get("user_name", None)
|
||||||
}
|
# user_type = data_token.get("user_type", None)
|
||||||
response = requests.request(
|
if user_name == None:
|
||||||
"POST", url, headers=headers, data=payload)
|
|
||||||
data_output = json.loads(response.text)
|
|
||||||
data = data_output.get("user_name", 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"})
|
||||||
|
url = URL_OAUTH2_GET_CHECK_TOKEN
|
||||||
count = await db["posts"].count_documents({"_id": post_save.post_id})
|
count = await db["posts"].count_documents({"_id": post_save.post_id})
|
||||||
if count == 0:
|
if count == 0:
|
||||||
return JSONResponse(status_code=status.HTTP_409_CONFLICT, content={"message": "Bài viết không tồn tại"})
|
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.created_at = datetime.datetime.now(tz=tz)
|
||||||
post_save = jsonable_encoder(post_save)
|
post_save = jsonable_encoder(post_save)
|
||||||
del post_save["token"]
|
del post_save["token"]
|
||||||
post_save["username"] = data
|
post_save["username"] = user_name
|
||||||
created_post = await db["post_save"].count_documents({"post_id": post_save["post_id"], "username": data})
|
created_post = await db["post_save"].count_documents({"post_id": post_save["post_id"], "username": user_name})
|
||||||
if created_post == 1:
|
if created_post == 1:
|
||||||
return JSONResponse(status_code=status.HTTP_409_CONFLICT, content={"message": "Bài viết đã được lưu trước đó"})
|
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)
|
new_post = await db["post_save"].insert_one(post_save)
|
||||||
@@ -74,38 +72,34 @@ async def create_post(post_save: SavePost):
|
|||||||
@post_save.post(
|
@post_save.post(
|
||||||
"/list_save_post_by_user", response_description="List save posts", response_model=SavePostModel
|
"/list_save_post_by_user", response_description="List save posts", response_model=SavePostModel
|
||||||
)
|
)
|
||||||
async def list_post(post_save: SavePost):
|
async def list_post(post_save: SavePost, token: token_test = Depends(get_current_user),
|
||||||
url = URL_OAUTH2_GET_CHECK_TOKEN
|
):
|
||||||
payload = {'token': post_save.token}
|
try:
|
||||||
headers = {
|
user_name = token.get("user_name", None)
|
||||||
'Authorization': 'Basic RGljdGlvbmFyeU1lZGlob21lOlJ4aXR6ZnZvaWFmZmNtb2l0ZW0='
|
|
||||||
}
|
# data_token = await get_current_user(Nonetoken)
|
||||||
response = requests.request(
|
# data = data_token.get("user_name", None)
|
||||||
"POST", url, headers=headers, data=payload)
|
# user_type = data_token.get("user_type", None)
|
||||||
data_output = json.loads(response.text)
|
if user_name == None:
|
||||||
data = data_output.get("user_name", 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"})
|
||||||
posts = await db["post_save"].find({"username": data}).to_list(1000)
|
posts = await db["post_save"].find({"username": user_name}).to_list(20)
|
||||||
return JSONResponse(status_code=status.HTTP_200_OK, content=posts)
|
return JSONResponse(status_code=status.HTTP_200_OK, content=posts)
|
||||||
|
except NameError:
|
||||||
|
return NameError
|
||||||
|
|
||||||
|
|
||||||
@post_save.delete("/delete_save_post", response_description="Delete save post")
|
@post_save.delete("/delete_save_post", response_description="Delete save post")
|
||||||
async def delete_save_post(post_id: str, token: str):
|
async def delete_save_post(post_id: str, token: token_test = Depends(get_current_user),
|
||||||
# delete_result = await db["post_save"].delete_one({"_id": user_id})
|
):
|
||||||
try:
|
try:
|
||||||
url = URL_OAUTH2_GET_CHECK_TOKEN
|
user_name = token.get("user_name", None)
|
||||||
payload = {'token': token}
|
|
||||||
headers = {
|
# data_token = await get_current_user(Nonetoken)
|
||||||
'Authorization': 'Basic RGljdGlvbmFyeU1lZGlob21lOlJ4aXR6ZnZvaWFmZmNtb2l0ZW0='
|
# data = data_token.get("user_name", None)
|
||||||
}
|
# user_type = data_token.get("user_type", None)
|
||||||
response = requests.request(
|
if user_name == None:
|
||||||
"POST", url, headers=headers, data=payload)
|
|
||||||
data_output = json.loads(response.text)
|
|
||||||
data = data_output.get("user_name", 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["post_save"].delete_one({"post_id": post_id, "username": data})
|
delete_result = await db["post_save"].delete_one({"post_id": post_id, "username": user_name})
|
||||||
if delete_result.deleted_count == 1:
|
if delete_result.deleted_count == 1:
|
||||||
return JSONResponse(status_code=status.HTTP_200_OK, content={"message": "Delete post save successful"})
|
return JSONResponse(status_code=status.HTTP_200_OK, content={"message": "Delete post save successful"})
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
|
|||||||
@@ -89,8 +89,10 @@ async def login_for_access_token(body: LoginRequest):
|
|||||||
"POST", url, headers=headers, data=payload, files=files)
|
"POST", url, headers=headers, data=payload, files=files)
|
||||||
|
|
||||||
access_token = json.loads(response.text)
|
access_token = json.loads(response.text)
|
||||||
|
print(access_token)
|
||||||
url = URL_OAUTH2_GET_CHECK_TOKEN
|
url = URL_OAUTH2_GET_CHECK_TOKEN
|
||||||
|
if access_token.get("access_token", None) == None:
|
||||||
|
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "UNAUTHORIZED"})
|
||||||
payload = {'token': access_token["access_token"]}
|
payload = {'token': access_token["access_token"]}
|
||||||
headers = {
|
headers = {
|
||||||
'Authorization': 'Basic RGljdGlvbmFyeU1lZGlob21lOlJ4aXR6ZnZvaWFmZmNtb2l0ZW0='
|
'Authorization': 'Basic RGljdGlvbmFyeU1lZGlob21lOlJ4aXR6ZnZvaWFmZmNtb2l0ZW0='
|
||||||
@@ -162,23 +164,17 @@ async def login_for_access_token_2(body: OAuth2PasswordRequestForm = Depends()):
|
|||||||
|
|
||||||
|
|
||||||
@router.post("/current", response_description="Current User")
|
@router.post("/current", response_description="Current User")
|
||||||
async def current_user(token: TokenModel):
|
async def current_user(token: token_test = Depends(get_current_user),
|
||||||
try:
|
):
|
||||||
url = URL_OAUTH2_GET_CHECK_TOKEN
|
user_name = token.get("user_name", None)
|
||||||
payload = {'token': token.token}
|
|
||||||
headers = {
|
# data_token = await get_current_user(Nonetoken)
|
||||||
'Authorization': 'Basic RGljdGlvbmFyeU1lZGlob21lOlJ4aXR6ZnZvaWFmZmNtb2l0ZW0='
|
# data = data_token.get("user_name", None)
|
||||||
}
|
# user_type = data_token.get("user_type", None)
|
||||||
response = requests.request(
|
if user_name == None:
|
||||||
"POST", url, headers=headers, data=payload)
|
|
||||||
data_output = json.loads(response.text)
|
|
||||||
data = data_output.get("user_name", 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"})
|
||||||
else:
|
else:
|
||||||
return json.loads(response.text)
|
return token
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
# @router.get("/current", response_description="Current User", response_model=ShowUserModel)
|
# @router.get("/current", response_description="Current User", response_model=ShowUserModel)
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|||||||
URL_SERVER = os.getenv('URL_SERVER')
|
URL_SERVER = os.getenv('URL_SERVER')
|
||||||
URL_OAUTH2_GET_CHECK_TOKEN = os.getenv("URL_OAUTH2_GET_CHECK_TOKEN")
|
URL_OAUTH2_GET_CHECK_TOKEN = os.getenv("URL_OAUTH2_GET_CHECK_TOKEN")
|
||||||
URL_OAUTH2_GET_TOKEN = os.getenv("URL_OAUTH2_GET_TOKEN")
|
URL_OAUTH2_GET_TOKEN = os.getenv("URL_OAUTH2_GET_TOKEN")
|
||||||
|
SWAGGER = os.getenv("URL_SWAGGER")
|
||||||
|
|
||||||
|
|
||||||
class DateTimeEncoder(JSONEncoder):
|
class DateTimeEncoder(JSONEncoder):
|
||||||
@@ -360,4 +361,4 @@ KHOA = [
|
|||||||
]
|
]
|
||||||
# ----------------- Database variables (MongoDB) --------------------------
|
# ----------------- Database variables (MongoDB) --------------------------
|
||||||
client = motor.motor_asyncio.AsyncIOMotorClient(os.environ["DB_URL"])
|
client = motor.motor_asyncio.AsyncIOMotorClient(os.environ["DB_URL"])
|
||||||
db = client.myTestDB
|
db = client.mdDB
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- 80:80
|
- 80:80
|
||||||
environment:
|
environment:
|
||||||
- DB_URL=mongodb://db/myTestDB
|
- DB_URL=mongodb://db/mdDB
|
||||||
|
- URL_SWAGGER=True
|
||||||
volumes:
|
volumes:
|
||||||
- ./app:/app
|
- ./app:/app
|
||||||
restart: always
|
restart: always
|
||||||
@@ -19,8 +20,41 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- 27017:27017
|
- 27017:27017
|
||||||
volumes:
|
volumes:
|
||||||
- taskdb:/data/db
|
- ./taskdb:/data/db
|
||||||
restart: always
|
restart: always
|
||||||
|
|
||||||
volumes:
|
|
||||||
taskdb:
|
# # db1:
|
||||||
|
# # image: registry.vegastar.vn/hoanganh.c4i/mongodb_md
|
||||||
|
# # ports:
|
||||||
|
# # - 27018:27017
|
||||||
|
# # volumes:
|
||||||
|
# # - ./data/db:/data/db
|
||||||
|
# # restart: always
|
||||||
|
|
||||||
|
# volumes:
|
||||||
|
# taskdb:
|
||||||
|
|
||||||
|
# version: "3.3"
|
||||||
|
|
||||||
|
# services:
|
||||||
|
# api:
|
||||||
|
# image: registry.vegastar.vn/hoanganh.c4i/medihome-dictionary-be
|
||||||
|
# # build: ./app
|
||||||
|
# ports:
|
||||||
|
# - 80:80
|
||||||
|
# environment:
|
||||||
|
# - DB_URL=mongodb://db/mdDB
|
||||||
|
# volumes:
|
||||||
|
# - ./images:/app/post/images
|
||||||
|
# restart: always
|
||||||
|
# env_file:
|
||||||
|
# - .env
|
||||||
|
|
||||||
|
# db:
|
||||||
|
# image: registry.vegastar.vn/hoanganh.c4i/mongodb_md
|
||||||
|
# ports:
|
||||||
|
# - 27017:27017
|
||||||
|
# volumes:
|
||||||
|
# - ./data/db:/data/db
|
||||||
|
# restart: always
|
||||||
|
|||||||
Reference in New Issue
Block a user