PaperParser/app/blueprints/api/users/routes.py

58 lines
1.8 KiB
Python
Raw Normal View History

2024-01-31 19:37:01 +10:00
from flask import Blueprint, render_template, request
from app.models.session import Session
from app.models.user import User
from app.extensions import db
from paper.parser import PaperParser
users = Blueprint("users", __name__, url_prefix="/users", template_folder="templates")
@users.route("/parse/<int:session_id>", methods=["POST"])
async def parse_users(session_id: int):
session = Session.query.get_or_404(session_id)
data = request.form
async with PaperParser(session.name) as parser:
users = await parser.get_participants(data.get("group"))
for user in users:
if not user.username:
continue
exists = db.session.query(User.id).filter_by(username=user.username).first()
if not exists:
db.session.add(
User(
first_name=user.first_name,
last_name=user.last_name,
username=user.username,
phone=user.phone,
session=session,
)
)
db.session.commit()
return render_template("user_cards.j2", users=session.users)
@users.route("/<int:session_id>", methods=["GET"])
def get_users(session_id: int):
users = Session.query.get_or_404(session_id).users
return render_template("user_cards.j2", users=users)
@users.route("/<int:session_id>", methods=["DELETE"])
def delete_users(session_id):
session: Session = Session.query.get_or_404(session_id)
if session.has_depending_tasks():
return 'Есть зависимые задачи', 409
User.query.filter_by(session=session).delete()
db.session.commit()
return render_template("user_cards.j2", users=[])