2021-04-01 14:20:13 +02:00
|
|
|
from flask import render_template, request, flash, redirect
|
2021-04-01 14:09:16 +02:00
|
|
|
from flask_login import login_required, current_user
|
|
|
|
from sqlalchemy.orm import joinedload
|
|
|
|
|
|
|
|
from app.dashboard.base import dashboard_bp
|
2023-11-21 16:42:18 +01:00
|
|
|
from app.db import Session
|
2021-04-01 14:09:16 +02:00
|
|
|
from app.models import (
|
|
|
|
ClientUser,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/app", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def app_route():
|
2023-11-21 16:42:18 +01:00
|
|
|
"""
|
|
|
|
List of apps that user has used via the "Sign in with SimpleLogin"
|
|
|
|
"""
|
|
|
|
|
2021-04-01 14:09:16 +02:00
|
|
|
client_users = (
|
|
|
|
ClientUser.filter_by(user_id=current_user.id)
|
|
|
|
.options(joinedload(ClientUser.client))
|
|
|
|
.options(joinedload(ClientUser.alias))
|
|
|
|
.all()
|
|
|
|
)
|
|
|
|
|
|
|
|
sorted(client_users, key=lambda cu: cu.client.name)
|
|
|
|
|
2021-04-01 14:20:13 +02:00
|
|
|
if request.method == "POST":
|
|
|
|
client_user_id = request.form.get("client-user-id")
|
|
|
|
client_user = ClientUser.get(client_user_id)
|
|
|
|
if not client_user or client_user.user_id != current_user.id:
|
|
|
|
flash(
|
|
|
|
"Unknown error, sorry for the inconvenience, refresh the page", "error"
|
|
|
|
)
|
|
|
|
return redirect(request.url)
|
|
|
|
|
|
|
|
client = client_user.client
|
|
|
|
ClientUser.delete(client_user_id)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2021-04-01 14:20:13 +02:00
|
|
|
|
|
|
|
flash(f"Link with {client.name} has been removed", "success")
|
|
|
|
return redirect(request.url)
|
|
|
|
|
2021-04-01 14:09:16 +02:00
|
|
|
return render_template(
|
|
|
|
"dashboard/app.html",
|
|
|
|
client_users=client_users,
|
|
|
|
)
|