2021-01-16 22:15:10 +08:00
|
|
|
from flask import Flask
|
|
|
|
|
from flask import request
|
2026-05-14 21:13:13 +08:00
|
|
|
from dataclasses import dataclass
|
2026-05-15 11:08:57 +08:00
|
|
|
from typing import Any, Callable, ParamSpec, TypeVar, Generic
|
2026-05-14 21:13:13 +08:00
|
|
|
|
2021-01-16 22:15:10 +08:00
|
|
|
import config
|
|
|
|
|
import database
|
2021-02-03 16:08:40 +08:00
|
|
|
import utils
|
2026-05-14 21:13:13 +08:00
|
|
|
from logger import LOGGER
|
2026-05-15 11:08:57 +08:00
|
|
|
from database import ResponseBody
|
2021-01-19 22:20:11 +08:00
|
|
|
|
2026-05-11 22:20:06 +08:00
|
|
|
app = Flask(__name__)
|
|
|
|
|
calendar_db = database.CalendarDatabase()
|
2021-02-08 16:22:04 +08:00
|
|
|
|
2026-05-11 22:20:06 +08:00
|
|
|
# region: API Route
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-11 22:20:06 +08:00
|
|
|
# region: Common
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
@app.route("/common/salt", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_common_saltHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.common_salt, (FormField("username", str, False),), None
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
@app.route("/common/login", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_common_loginHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
clientInfo = FetchClientNetworkInfo()
|
|
|
|
|
|
|
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.common_login,
|
|
|
|
|
(
|
|
|
|
|
FormField("username", str, False),
|
|
|
|
|
FormField("password", str, False),
|
|
|
|
|
FormField("clientUa", str, False),
|
|
|
|
|
FormField("clientIp", str, False),
|
|
|
|
|
),
|
|
|
|
|
{"clientUa": clientInfo.user_agent, "clientIp": clientInfo.ip_addr},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/common/webLogin", methods=["POST"])
|
2021-01-20 22:57:41 +08:00
|
|
|
def api_common_webLoginHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
clientInfo = FetchClientNetworkInfo()
|
|
|
|
|
|
|
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.common_webLogin,
|
|
|
|
|
(
|
|
|
|
|
FormField("username", str, False),
|
|
|
|
|
FormField("password", str, False),
|
|
|
|
|
FormField("clientUa", str, False),
|
|
|
|
|
FormField("clientIp", str, False),
|
|
|
|
|
),
|
|
|
|
|
{"clientUa": clientInfo.user_agent, "clientIp": clientInfo.ip_addr},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/common/logout", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_common_logoutHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.common_logout, (FormField("token", str, False),), None
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
@app.route("/common/tokenValid", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_common_tokenValidHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.common_tokenValid, (FormField("token", str, False),), None
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-11 22:20:06 +08:00
|
|
|
# endregion
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-11 22:20:06 +08:00
|
|
|
# region: Calendar
|
|
|
|
|
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
@app.route("/calendar/getFull", methods=["POST"])
|
|
|
|
|
def api_calendar_getFullHandle():
|
|
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.calendar_getFull,
|
|
|
|
|
(
|
|
|
|
|
FormField("token", str, False),
|
|
|
|
|
FormField("startDateTime", int, False),
|
|
|
|
|
FormField("endDateTime", int, False),
|
|
|
|
|
),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/calendar/getList", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_calendar_getListHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.calendar_getList,
|
|
|
|
|
(
|
|
|
|
|
FormField("token", str, False),
|
|
|
|
|
FormField("startDateTime", int, False),
|
|
|
|
|
FormField("endDateTime", int, False),
|
|
|
|
|
),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/calendar/getDetail", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_calendar_getDetailHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.calendar_getDetail,
|
|
|
|
|
(FormField("token", str, False), FormField("uuid", str, False)),
|
|
|
|
|
None,
|
|
|
|
|
)
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
@app.route("/calendar/update", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_calendar_updateHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.calendar_update,
|
|
|
|
|
(
|
|
|
|
|
FormField("token", str, False),
|
|
|
|
|
FormField("uuid", str, False),
|
|
|
|
|
FormField("belongTo", str, True),
|
|
|
|
|
FormField("title", str, True),
|
|
|
|
|
FormField("description", str, True),
|
|
|
|
|
FormField("eventDateTimeStart", int, True),
|
|
|
|
|
FormField("eventDateTimeEnd", int, True),
|
|
|
|
|
FormField("loopRules", str, True),
|
|
|
|
|
FormField("timezoneOffset", int, True),
|
|
|
|
|
FormField("lastChange", str, False),
|
|
|
|
|
),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/calendar/add", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_calendar_addHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.calendar_add,
|
|
|
|
|
(
|
|
|
|
|
FormField("token", str, False),
|
|
|
|
|
FormField("belongTo", str, False),
|
|
|
|
|
FormField("title", str, False),
|
|
|
|
|
FormField("description", str, False),
|
|
|
|
|
FormField("eventDateTimeStart", int, False),
|
|
|
|
|
FormField("eventDateTimeEnd", int, False),
|
|
|
|
|
FormField("loopRules", str, False),
|
|
|
|
|
FormField("timezoneOffset", int, False),
|
|
|
|
|
),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/calendar/delete", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_calendar_deleteHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.calendar_delete,
|
|
|
|
|
(
|
|
|
|
|
FormField("token", str, False),
|
|
|
|
|
FormField("uuid", str, False),
|
|
|
|
|
FormField("lastChange", str, False),
|
|
|
|
|
),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-11 22:20:06 +08:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
|
|
# region: Collection
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
@app.route("/collection/getFullOwn", methods=["POST"])
|
2021-01-31 13:50:20 +08:00
|
|
|
def api_collection_getFullOwnHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.collection_getFullOwn, (FormField("token", str, False),), None
|
|
|
|
|
)
|
2021-01-31 13:50:20 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
@app.route("/collection/getListOwn", methods=["POST"])
|
2021-01-31 13:50:20 +08:00
|
|
|
def api_collection_getListOwnHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.collection_getListOwn, (FormField("token", str, False),), None
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-31 13:50:20 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
@app.route("/collection/getDetailOwn", methods=["POST"])
|
2021-01-31 13:50:20 +08:00
|
|
|
def api_collection_getDetailOwnHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.collection_getDetailOwn,
|
|
|
|
|
(FormField("token", str, False), FormField("uuid", str, False)),
|
|
|
|
|
None,
|
|
|
|
|
)
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
@app.route("/collection/addOwn", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_collection_addOwnHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.collection_addOwn,
|
|
|
|
|
(FormField("token", str, False), FormField("name", str, False)),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
@app.route("/collection/updateOwn", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_collection_updateOwnHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.collection_updateOwn,
|
|
|
|
|
(
|
|
|
|
|
FormField("token", str, False),
|
|
|
|
|
FormField("uuid", str, False),
|
|
|
|
|
FormField("name", str, False),
|
|
|
|
|
FormField("lastChange", str, False),
|
|
|
|
|
),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/collection/deleteOwn", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_collection_deleteOwnHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.collection_deleteOwn,
|
|
|
|
|
(
|
|
|
|
|
FormField("token", str, False),
|
|
|
|
|
FormField("uuid", str, False),
|
|
|
|
|
FormField("lastChange", str, False),
|
|
|
|
|
),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/collection/getSharing", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_collection_getSharingHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.collection_getSharing,
|
|
|
|
|
(FormField("token", str, False), FormField("uuid", str, False)),
|
|
|
|
|
None,
|
|
|
|
|
)
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
@app.route("/collection/deleteSharing", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_collection_deleteSharingHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.collection_deleteSharing,
|
|
|
|
|
(
|
|
|
|
|
FormField("token", str, False),
|
|
|
|
|
FormField("uuid", str, False),
|
|
|
|
|
FormField("target", str, False),
|
|
|
|
|
FormField("lastChange", str, False),
|
|
|
|
|
),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/collection/addSharing", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_collection_addSharingHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.collection_addSharing,
|
|
|
|
|
(
|
|
|
|
|
FormField("token", str, False),
|
|
|
|
|
FormField("uuid", str, False),
|
|
|
|
|
FormField("target", str, False),
|
|
|
|
|
FormField("lastChange", str, False),
|
|
|
|
|
),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/collection/getShared", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_collection_getSharedHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.collection_getShared, (FormField("token", str, False),), None
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-11 22:20:06 +08:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
|
|
# region: Todo
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
@app.route("/todo/getFull", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_todo_getFullHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.todo_getFull, (FormField("token", str, False),), None
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
@app.route("/todo/getList", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_todo_getListHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.todo_getList, (FormField("token", str, False),), None
|
|
|
|
|
)
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
@app.route("/todo/getDetail", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_todo_getDetailHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.todo_getDetail,
|
|
|
|
|
(FormField("token", str, False), FormField("uuid", str, False)),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
@app.route("/todo/add", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_todo_addHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(calendar_db.todo_add, (FormField("token", str, False),), None)
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
@app.route("/todo/update", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_todo_updateHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.todo_update,
|
|
|
|
|
(
|
|
|
|
|
FormField("token", str, False),
|
|
|
|
|
FormField("uuid", str, False),
|
|
|
|
|
FormField("data", str, False),
|
|
|
|
|
FormField("lastChange", str, False),
|
|
|
|
|
),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/todo/delete", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_todo_deleteHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.todo_delete,
|
|
|
|
|
(
|
|
|
|
|
FormField("token", str, False),
|
|
|
|
|
FormField("uuid", str, False),
|
|
|
|
|
FormField("lastChange", str, False),
|
|
|
|
|
),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-11 22:20:06 +08:00
|
|
|
# endregion
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-11 22:20:06 +08:00
|
|
|
# region: Admin
|
|
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
@app.route("/admin/get", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_admin_getHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(calendar_db.admin_get, (FormField("token", str, False),), None)
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
@app.route("/admin/add", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_admin_addHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.admin_add,
|
|
|
|
|
(FormField("token", str, False), FormField("username", str, False)),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
@app.route("/admin/update", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_admin_updateHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.admin_update,
|
|
|
|
|
(
|
|
|
|
|
FormField("token", str, False),
|
|
|
|
|
FormField("username", str, False),
|
|
|
|
|
FormField("password", str, True),
|
|
|
|
|
FormField("isAdmin", utils.Str2Bool, True),
|
|
|
|
|
),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/admin/delete", methods=["POST"])
|
2021-01-16 22:15:10 +08:00
|
|
|
def api_admin_deleteHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.admin_delete,
|
|
|
|
|
(FormField("token", str, False), FormField("username", str, False)),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
2021-03-08 21:56:03 +08:00
|
|
|
|
2026-05-11 22:20:06 +08:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
|
|
# region: Profile
|
2021-03-08 21:56:03 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
@app.route("/profile/isAdmin", methods=["POST"])
|
2021-03-08 21:56:03 +08:00
|
|
|
def api_profile_isAdminHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.profile_isAdmin, (FormField("token", str, False),), None
|
|
|
|
|
)
|
2021-03-08 21:56:03 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
@app.route("/profile/changePassword", methods=["POST"])
|
2021-03-08 21:56:03 +08:00
|
|
|
def api_profile_changePasswordHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.profile_changePassword,
|
|
|
|
|
(FormField("token", str, False), FormField("password", str, False)),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
2021-03-08 21:56:03 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
@app.route("/profile/getToken", methods=["POST"])
|
2021-03-08 21:56:03 +08:00
|
|
|
def api_profile_getTokenHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.profile_getToken, (FormField("token", str, False),), None
|
|
|
|
|
)
|
2021-03-08 21:56:03 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
@app.route("/profile/deleteToken", methods=["POST"])
|
2021-03-08 21:56:03 +08:00
|
|
|
def api_profile_deleteTokenHandle():
|
2026-05-15 11:08:57 +08:00
|
|
|
return SmartDbCaller(
|
|
|
|
|
calendar_db.profile_deleteToken,
|
|
|
|
|
(FormField("token", str, False), FormField("deleteToken", str, False)),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-11 22:20:06 +08:00
|
|
|
# endregion
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-11 22:20:06 +08:00
|
|
|
# endregion
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
# region: Utilities
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
|
class ClientNetworkInfo:
|
|
|
|
|
user_agent: str
|
|
|
|
|
"""The user agent of client."""
|
|
|
|
|
ip_addr: str
|
|
|
|
|
"""The IP address of client."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def FetchClientNetworkInfo() -> ClientNetworkInfo:
|
|
|
|
|
clientUa = request.user_agent.string
|
|
|
|
|
forwardIpList = request.headers.getlist("X-Forwarded-For")
|
|
|
|
|
if forwardIpList:
|
|
|
|
|
clientIp = forwardIpList[0]
|
|
|
|
|
else:
|
|
|
|
|
directIp = request.remote_addr
|
|
|
|
|
if directIp is not None:
|
|
|
|
|
clientIp = directIp
|
|
|
|
|
else:
|
|
|
|
|
clientIp = "0.0.0.0"
|
|
|
|
|
|
|
|
|
|
return ClientNetworkInfo(clientUa, clientIp)
|
|
|
|
|
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2026-05-14 21:13:13 +08:00
|
|
|
@dataclass(frozen=True)
|
|
|
|
|
class FormField:
|
|
|
|
|
name: str
|
|
|
|
|
"""The name of form field."""
|
|
|
|
|
ty: Callable[[str], Any]
|
|
|
|
|
"""The type of form field."""
|
|
|
|
|
is_optional: bool
|
|
|
|
|
"""True if this form field is optional, otherwise false."""
|
|
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
def SmartDbCaller(
|
|
|
|
|
db_method: Callable[..., ResponseBody[Any]],
|
|
|
|
|
fields: tuple[FormField, ...],
|
|
|
|
|
padding_form: dict[str, str] | None,
|
|
|
|
|
) -> dict[str, Any]:
|
2026-05-14 21:13:13 +08:00
|
|
|
opt_param_counter = 0
|
2026-05-15 11:08:57 +08:00
|
|
|
lost_required: bool = False
|
2026-05-14 21:13:13 +08:00
|
|
|
param_list: list[Any] = []
|
|
|
|
|
opt_param_dict: dict[str, Any] = {}
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
# fetch user passed form
|
|
|
|
|
user_form: dict[str, str] = request.form.to_dict()
|
|
|
|
|
LOGGER.debug(f"User Form: {user_form}")
|
|
|
|
|
# overwrite user form by our padding form
|
2026-05-14 21:13:13 +08:00
|
|
|
if padding_form is not None:
|
2026-05-15 11:08:57 +08:00
|
|
|
user_form.update(padding_form)
|
|
|
|
|
LOGGER.debug(f"Padded User Form: {user_form}")
|
|
|
|
|
|
|
|
|
|
# check fields one by one
|
2026-05-14 21:13:13 +08:00
|
|
|
for field in fields:
|
2026-05-15 11:08:57 +08:00
|
|
|
value = user_form.get(field.name, None)
|
2026-05-14 21:13:13 +08:00
|
|
|
if value is not None:
|
|
|
|
|
value = field.ty(value)
|
2026-05-15 11:08:57 +08:00
|
|
|
|
2026-05-14 21:13:13 +08:00
|
|
|
if field.is_optional:
|
2021-02-01 20:34:40 +08:00
|
|
|
# optional param
|
2026-05-14 21:13:13 +08:00
|
|
|
if value is not None:
|
|
|
|
|
opt_param_dict[field.name] = value
|
|
|
|
|
opt_param_counter += 1
|
2021-02-01 20:34:40 +08:00
|
|
|
else:
|
2026-05-14 21:13:13 +08:00
|
|
|
# required param
|
|
|
|
|
if value is None:
|
2026-05-15 11:08:57 +08:00
|
|
|
lost_required = True
|
|
|
|
|
else:
|
|
|
|
|
param_list.append(value)
|
|
|
|
|
|
|
|
|
|
# Only execute database function if there is no lost required fields.
|
|
|
|
|
# And fulfill one of following requirements:
|
|
|
|
|
# 1. There are all required fields (optional parameter count is zero).
|
|
|
|
|
# 1. Or, there is some optional parameter.
|
|
|
|
|
LOGGER.debug(f"Has Lost Required Parameter: {lost_required}")
|
|
|
|
|
LOGGER.debug(f"All Optional Parameter Count: {opt_param_counter}")
|
|
|
|
|
LOGGER.debug(f"Available Optional Parameter Count: {len(opt_param_dict)}")
|
|
|
|
|
result: ResponseBody[Any]
|
|
|
|
|
if lost_required == False and (opt_param_counter == 0 or len(opt_param_dict) != 0):
|
|
|
|
|
result = db_method(*param_list, **opt_param_dict)
|
|
|
|
|
else:
|
|
|
|
|
result = ResponseBody(False, "Invalid parameter", None)
|
2026-05-14 21:13:13 +08:00
|
|
|
|
2021-01-31 13:50:20 +08:00
|
|
|
return ConstructResponseBody(result)
|
2021-01-20 22:57:41 +08:00
|
|
|
|
2026-05-15 11:08:57 +08:00
|
|
|
|
|
|
|
|
def ConstructResponseBody(body: ResponseBody[Any]) -> dict[str, Any]:
|
|
|
|
|
return {"success": body.success, "error": body.error, "data": body.data}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# endregion
|
|
|
|
|
|
2021-01-20 22:57:41 +08:00
|
|
|
|
2021-01-16 22:15:10 +08:00
|
|
|
def run():
|
2021-01-25 20:42:06 +08:00
|
|
|
calendar_db.open()
|
2026-04-28 13:17:54 +08:00
|
|
|
app.run(port=config.get_config().web.port)
|
2021-01-25 20:42:06 +08:00
|
|
|
calendar_db.close()
|