Skip to content

Commit

Permalink
add db_reset api
Browse files Browse the repository at this point in the history
  • Loading branch information
hiltay committed Nov 1, 2022
1 parent 96cd5ed commit 3fe646d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
7 changes: 7 additions & 0 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,13 @@ async def crawler_status(payload: str = Depends(login_with_token_)):
resp["status"] = "未知"
return format_response.standard_response(**resp)

@app.delete("/db_reset", tags=["Manage"])
async def db_reset(payload: str = Depends(login_with_token_)):
"""
清空数据库中友链、文章表
"""
return await db_reset_()


if __name__ == "__main__":
if settings["DEPLOY_TYPE"] == "docker":
Expand Down
12 changes: 12 additions & 0 deletions api_dependencies/leancloud/leancloudapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,15 @@ async def login_(password: str):
except:
return format_response.CredentialsException
return format_response.standard_response(token=token)


async def db_reset_():
db_interface.db_init()
# 清除friend、post表
friendlist_query = leancloud.Query('friend_list')
friendlist = friendlist_query.limit(1000).find()
leancloud.Object.destroy_all(friendlist)
friendpoor_query = leancloud.Query('friend_poor')
friendpoor = friendpoor_query.limit(1000).find()
leancloud.Object.destroy_all(friendpoor)
return format_response.standard_response()
10 changes: 10 additions & 0 deletions api_dependencies/mongodb/mongodbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,13 @@ async def login_(password: str):
# 401
return format_response.CredentialsException
return format_response.standard_response(token=token)


async def db_reset_():
session = db_interface.db_init()
# 清除friend、post表
post_collection, friend_db_collection = session.Post, session.Friend
friend_db_collection.delete_many({})
post_collection.delete_many({})

return format_response.standard_response()
44 changes: 32 additions & 12 deletions api_dependencies/sql/sqlapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@
from api_dependencies import format_response, tools, dependencies as dep


async def vercel_update_db():
"""
vercel环境需要上传data.db到github
:return:
"""
# github+vercel将db上传
db_path = "/tmp/data.db"
with open(db_path, "rb") as f:
data = f.read()
gh_access_token = os.environ.get("GH_TOKEN", "")
gh_name = os.environ.get("GH_NAME", "")
gh_email = os.environ.get("GH_EMAIL", "")
repo_name = "hexo-circle-of-friends"
message = "Update data.db"
await create_or_update_file(gh_access_token, gh_name, gh_email, repo_name,
"data.db",
get_b64encoded_data(data), message)


def query_all(li, start: int = 0, end: int = -1, rule: str = "updated"):
session = db_interface.db_init()
article_num = session.query(Post).count()
Expand Down Expand Up @@ -288,18 +307,7 @@ async def login_(password: str):
session.commit()
session.close()
if tools.is_vercel():
# github+vercel将db上传
db_path = "/tmp/data.db"
with open(db_path, "rb") as f:
data = f.read()
gh_access_token = os.environ.get("GH_TOKEN", "")
gh_name = os.environ.get("GH_NAME", "")
gh_email = os.environ.get("GH_EMAIL", "")
repo_name = "hexo-circle-of-friends"
message = "Update data.db"
await create_or_update_file(gh_access_token, gh_name, gh_email, repo_name,
"data.db",
get_b64encoded_data(data), message)
await vercel_update_db()
elif len(auth) == 1:
# 保存了pwd,通过pwd验证
if dep.verify_password(password, auth[0].password):
Expand All @@ -314,3 +322,15 @@ async def login_(password: str):
return format_response.CredentialsException

return format_response.standard_response(token=token)


async def db_reset_():
session = db_interface.db_init()
# 清除friend、post表
session.query(Friend).delete()
session.query(Post).delete()
session.commit()
session.close()
if tools.is_vercel():
await vercel_update_db()
return format_response.standard_response()

0 comments on commit 3fe646d

Please sign in to comment.