beta 2.0
This commit is contained in:
@@ -1,8 +1,25 @@
|
||||
from flask import Flask, request, jsonify, render_template
|
||||
from flask import Flask, redirect, request, jsonify, render_template, url_for
|
||||
from config import Config
|
||||
from db import PostScheduler, UsersMedods, VkAPI, VkPost, db, MedodsAPI, ApiEndpoint
|
||||
from medods_handler import updateMedodsUsers
|
||||
from scheduler import get_scheduler_status, init_scheduler, enable_publish_job
|
||||
from db import (
|
||||
BirthdateScheduler,
|
||||
PostScheduler,
|
||||
Protection,
|
||||
UsersBirthdate,
|
||||
UsersMedods,
|
||||
VkAPI,
|
||||
VkPost,
|
||||
db,
|
||||
MedodsAPI,
|
||||
ApiEndpoint,
|
||||
)
|
||||
from medods_handler import updateMedodsUsers, updateUsersBirthdate
|
||||
from scheduler import (
|
||||
enable_birthdate_job,
|
||||
get_birthdate_scheduler_status,
|
||||
get_scheduler_status,
|
||||
init_scheduler,
|
||||
enable_publish_job,
|
||||
)
|
||||
from http_client import send_request
|
||||
import logging
|
||||
import os
|
||||
@@ -139,7 +156,7 @@ def vk():
|
||||
@app.route("/posts", methods=["GET"])
|
||||
def posts():
|
||||
medodsUsers = UsersMedods.query.all()
|
||||
if len(medodsUsers) > 0:
|
||||
if medodsUsers:
|
||||
medodsUsers = [user.toDict() for user in medodsUsers]
|
||||
vkPost = VkPost.query.first()
|
||||
if vkPost:
|
||||
@@ -159,6 +176,21 @@ def posts():
|
||||
)
|
||||
|
||||
|
||||
@app.route("/birthdate", methods=["GET"])
|
||||
def birthdate():
|
||||
schedulerStatus = get_birthdate_scheduler_status()
|
||||
schedulerSettings = BirthdateScheduler.query.first()
|
||||
if schedulerSettings:
|
||||
schedulerSettings = schedulerSettings.toDict()
|
||||
return render_template(
|
||||
"birthdate.html",
|
||||
data={
|
||||
"schedulerStatus": schedulerStatus,
|
||||
"schedulerSettings": schedulerSettings,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@app.route("/api/medods", methods=["POST"])
|
||||
def api_medods():
|
||||
try:
|
||||
@@ -273,7 +305,8 @@ def api_requests():
|
||||
logger.info("Получен список запросов")
|
||||
|
||||
requestsDB = ApiEndpoint.query.all()
|
||||
requestsList = [r.toDict() for r in requestsDB]
|
||||
if requestsDB:
|
||||
requestsList = [r.toDict() for r in requestsDB]
|
||||
return jsonify(
|
||||
{
|
||||
"status": "ok",
|
||||
@@ -286,6 +319,79 @@ def api_requests():
|
||||
return jsonify({"status": "error"}), 405
|
||||
|
||||
|
||||
@app.route("/api/birthdate", methods=["GET", "POST", "PATCH"])
|
||||
def api_birthdate():
|
||||
match request.method:
|
||||
case "POST":
|
||||
reqData = request.json
|
||||
userUpdate = reqData["userUpdate"]
|
||||
if userUpdate:
|
||||
logger.info("Обновлен пользователь")
|
||||
userId = userUpdate["userId"]
|
||||
userData = userUpdate["userData"]
|
||||
try:
|
||||
userDB = UsersBirthdate.query.filter_by(id=userId).first()
|
||||
userDB.photo_link = userData["photoLink"]
|
||||
userDB.congratulations = userData["congratulations"]
|
||||
db.session.commit()
|
||||
return jsonify({"status": "ok"})
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка при обновлении пользователя: {e}")
|
||||
return jsonify({"status": "error"}), 500
|
||||
scheduleSettings = reqData["scheduleSettings"]
|
||||
if scheduleSettings:
|
||||
logger.info("Обновлены настройки расписания")
|
||||
try:
|
||||
scheduleDB = BirthdateScheduler.query.first()
|
||||
if scheduleDB:
|
||||
scheduleDB.hour = scheduleSettings["hour"]
|
||||
scheduleDB.minute = scheduleSettings["minute"]
|
||||
scheduleDB.enabled = scheduleSettings["enabled"]
|
||||
else:
|
||||
scheduleDB = BirthdateScheduler(
|
||||
hour=scheduleSettings["hour"],
|
||||
minute=scheduleSettings["minute"],
|
||||
enabled=scheduleSettings["enabled"],
|
||||
)
|
||||
db.session.add(scheduleDB)
|
||||
db.session.commit()
|
||||
enable_birthdate_job()
|
||||
scheduleInfo = get_birthdate_scheduler_status()
|
||||
return jsonify(
|
||||
{
|
||||
"status": "ok",
|
||||
"next_run_time": scheduleInfo.get("next_run_time"),
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка при обновлении настройки расписания: {e}")
|
||||
return jsonify({"status": "error"}), 500
|
||||
|
||||
case "GET":
|
||||
logger.info("Получен список пользователей")
|
||||
|
||||
users = UsersBirthdate.query.all()
|
||||
if users:
|
||||
users = [u.toDict() for u in users]
|
||||
return jsonify(
|
||||
{
|
||||
"status": "ok",
|
||||
"users": users,
|
||||
}
|
||||
)
|
||||
|
||||
case "PATCH":
|
||||
success = updateUsersBirthdate()
|
||||
if success:
|
||||
return jsonify({"status": "ok"})
|
||||
else:
|
||||
return jsonify({"status": "error"}), 500
|
||||
|
||||
case _:
|
||||
logger.error("Неверный метод запроса")
|
||||
return jsonify({"status": "error"}), 405
|
||||
|
||||
|
||||
@app.route("/api/vk", methods=["POST"])
|
||||
def api_vk():
|
||||
requestData = request.json
|
||||
@@ -410,13 +516,84 @@ def api_posts():
|
||||
return jsonify({"status": "error"}), 405
|
||||
|
||||
|
||||
@app.route("/login", methods=["GET", "POST", "PATCH"])
|
||||
def login():
|
||||
match request.method:
|
||||
case "GET":
|
||||
return render_template("login.html")
|
||||
case "POST":
|
||||
data = request.get_json()
|
||||
password = data.get("password")
|
||||
|
||||
protection = Protection.query.first()
|
||||
if protection:
|
||||
if not protection.verify_password(password):
|
||||
return (
|
||||
jsonify({"status": "error", "errorMessage": "Неверный пароль"}),
|
||||
401,
|
||||
)
|
||||
else:
|
||||
protection.generate_token()
|
||||
db.session.commit()
|
||||
else:
|
||||
protection = Protection()
|
||||
protection.set_password(password)
|
||||
protection.generate_token()
|
||||
|
||||
db.session.add(protection)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({"status": "ok", "token": protection.token}), 200
|
||||
case "PATCH":
|
||||
data = request.get_json()
|
||||
new_password = data.get("password")
|
||||
|
||||
if not new_password:
|
||||
return {"status": "error", "errorMessage": "Пароль не задан"}, 400
|
||||
|
||||
protection = Protection.query.first()
|
||||
if not protection:
|
||||
protection = Protection()
|
||||
protection.set_password(password)
|
||||
protection.generate_token()
|
||||
|
||||
db.session.add(protection)
|
||||
db.session.commit()
|
||||
else:
|
||||
protection.set_password(password)
|
||||
db.session.commit()
|
||||
|
||||
return {"status": "ok"}
|
||||
case _:
|
||||
logger.error("Неверный метод запроса")
|
||||
return jsonify({"status": "error"}), 405
|
||||
|
||||
|
||||
def init_app():
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
enable_publish_job()
|
||||
enable_birthdate_job()
|
||||
logger.info("Приложение запущено")
|
||||
|
||||
|
||||
@app.before_request
|
||||
def check_auth_cookie():
|
||||
endpoint = request.endpoint
|
||||
|
||||
if endpoint is None:
|
||||
return
|
||||
|
||||
if endpoint == "login" or endpoint.startswith("static"):
|
||||
return
|
||||
|
||||
p = Protection.query.first()
|
||||
|
||||
if not p or not p.verify_token(request.cookies.get("auth_token")):
|
||||
return redirect(url_for("login"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
init_app()
|
||||
app.run(host="0.0.0.0", port=80)
|
||||
app.run(debug=True, host="0.0.0.0", port=80)
|
||||
# app.run(host="0.0.0.0", port=80)
|
||||
|
||||
Reference in New Issue
Block a user