Merge branch 'tag_user' of https://github.com/Maix0/fft into tag_user

This commit is contained in:
nathan 2025-01-12 00:55:19 +01:00
commit e04f9275c2
9 changed files with 93 additions and 43 deletions

View file

@ -73,7 +73,7 @@ class Api:
self.add_rate()
data, status, headers = self.get("/campus/1/users", [f"filter[login]={login}"])
if status == 200:
if (len(data)):
if len(data):
return data[0]["id"]
return 0

4
app.py
View file

@ -1,11 +1,7 @@
import dotenv
dotenv.load_dotenv()
from flask import Flask, g
from maps.maps import place_to_btn, percent_to_btn
from db import Db
import os
import sentry_sdk
import config
import routes.finder
import importlib

View file

@ -9,7 +9,7 @@ update_key = os.environ.get("F42_UPDATE_KEY")
# Configuration
db_path = os.environ.get("F42_DB", default="database.db")
domain = os.environ.get("F42_DOMAIN")
redirect_url = f"http://{domain}:8888/auth"
redirect_url = f"http://{domain}/auth"
auth_link = f"https://api.intra.42.fr/oauth/authorize?client_id={key}&redirect_uri={redirect_url}&response_type=code&scope=public"
redis_host = os.environ.get("F42_REDIS_HOST")
redis_port = os.environ.get("F42_REDIS_PORT")

26
db.py
View file

@ -498,18 +498,6 @@ class Db:
"SELECT tag FROM PERMISSIONS WHERE user_id = ?", [user_id]
)
return req.fetchall()
def get_tag(self, user_id: int):
req = self.cur.execute(
"SELECT tag FROM USERS WHERE id = ?", [user_id]
)
return req.fetchall()
def set_tag(self, user_id: int, tag: str):
req = self.cur.execute(
"UPDATE USERS SET tag= ? WHERE id = ?", [tag, user_id]
)
return req.fetchall()
def admin_change_tag(self, user_id: int, tag: str):
self.cur.execute(
@ -527,6 +515,20 @@ class Db:
)
return req.fetchone()
def get_tag(self, user_id: int):
req = self.cur.execute("SELECT tag FROM USERS WHERE id = ?", [user_id])
return req.fetchone()
def set_tag(self, user_id: int, tag: str):
req = self.cur.execute("UPDATE USERS SET tag= ? WHERE id = ?", [tag, user_id])
return req.fetchall()
def get_all_tags(self):
req = self.cur.execute(
"SELECT id, name, tag FROM USERS WHERE tag IS NOT NULL", []
)
return req.fetchall()
def get_special_user_by_id(self, sp_id: int):
req = self.cur.execute("SELECT * FROM SPECIAL_USERS WHERE sp_id = ?", [sp_id])
return req.fetchone()

View file

@ -23,14 +23,7 @@ def admin(userid):
silents = db.get_all_silents()
whitelist = db.get_all_whitelist()
admin = db.get_all_admins()
tag = db.get_tag(user_id=userid["userid"])
if len(tag):
if db.get_tag(user_id=userid["userid"])[0]["tag"]:
userid.update({"tag": db.get_tag(user_id=userid["userid"])[0]["tag"]})
else :
userid.update({"tag": ""})
else:
userid.update({"tag": ""})
tags = db.get_all_tags()
return render_template(
"admin.html",
user=userid,
@ -38,6 +31,7 @@ def admin(userid):
silents=silents,
whitelist=whitelist,
admins=admin,
tags=tags,
is_admin=True,
)
@ -101,7 +95,8 @@ def remove_tutor_station(ban_id, csrf, userid):
db.remove_tutor_station(int(ban_id))
return ""
@app.route("/admin/settag", methods=["POST"])
@app.route("/admin/set_usertag", methods=["POST"])
@auth_required
def settag(userid):
if not userid["admin"] or userid["admin"]["level"] < 1:
@ -110,9 +105,14 @@ def settag(userid):
return "Please refresh and try again", 401
with Db() as db:
id = api.get_user_id_by_login(login=request.form["login"])
db.set_tag(user_id=id, tag=request.form["tag"])
tag = request.form["tag"]
if tag == "":
db.set_tag(user_id=id, tag=None)
else:
db.set_tag(user_id=id, tag=tag)
return ""
@app.route("/admin/piscine_add", methods=["POST"])
@auth_required
def insert_piscine(userid):

View file

@ -66,7 +66,6 @@ def settings(userid):
db = Db(config.db_path)
login = db.get_user_by_id(userid["userid"])["name"]
user = db.get_user_profile(login)
notif = db.has_notifications(userid["userid"])
theme = db.get_theme(userid["userid"])
cookies = db.get_user_cookies(userid["userid"])
campus_id = db.get_user_by_id(userid["userid"])["campus"]
@ -77,7 +76,6 @@ def settings(userid):
return render_template(
"settings.html",
user=user,
notif=notif,
theme=theme,
cookies=cookies,
kiosk_buildings=kiosk_buildings,

View file

@ -51,8 +51,10 @@ def auth_required(function):
return "You are not whitelist from this website.", 403
details = db.get_user_by_id(userid["userid"])
theme = db.get_theme(userid["userid"])
tag = db.get_tag(userid["userid"])
db.close()
userid["admin"] = is_admin
userid["tag"] = tag["tag"]
userid["campus"] = details["campus"]
userid["login"] = details["name"]
userid["image_medium"] = proxy_images(details["image_medium"])

View file

@ -7,8 +7,8 @@ CREATE TABLE IF NOT EXISTS USERS
pool VARCHAR(100),
lang VARCHAR(3) DEFAULT 'fr',
active TIMESTAMP DEFAULT 0,
tag TEXT,
campus INTEGER DEFAULT 1
campus INTEGER DEFAULT 1,
tag TEXT DEFAULT NULL
);
CREATE TABLE IF NOT EXISTS COOKIES

View file

@ -8,6 +8,10 @@
.badge-sm {
font-size: 1rem;
}
.hidden {
display: none;
}
</style>
{% endblock %}
{% block content %}
@ -16,7 +20,7 @@
<div class="modal-content">
<form method="POST" id="changeTag">
<div class="modal-header">
<h1 class="modal-title fs-5">Changer le Tag</h1>
<h1 class="modal-title fs-5">Changer le Admin Tag</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
@ -36,23 +40,23 @@
</div>
</div>
</div>
<div class="modal fade" id="setTagModal" tabindex="-1" aria-hidden="true">
<div class="modal fade" id="ChangeUserTagModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST" id="setTag">
<form method="POST" id="setUserTag">
<div class="modal-header">
<h1 class="modal-title fs-5">Changer le Tag</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="form-floating mb-3 hidden" id="changeUserTagLogin">
<input type="text" name="login" class="form-control" id="input-desc" placeholder="Lorem ipsum...">
<label for="input-desc">Login</label>
</div>
<div class="form-floating mb-3">
<input type="text" name="tag" class="form-control" id="input-desc" placeholder="Lorem ipsum...">
<label for="input-desc">Tag</label>
</div>
<div class="form-floating mb-3">
<input type="text" name="login" class="form-control" id="input-desc" placeholder="Lorem ipsum...">
<label for="input-desc">Login</label>
</div>
</div>
<input type="hidden" name="csrf" value="{{ create_csrf() }}">
<input type="hidden" name="user_id">
@ -180,7 +184,9 @@
<div class="p-5 mb-3 text-center bg-body-tertiary rounded-3 align-vertical">
<img class="bi mt-4 mb-3 rounded-circle" src="{{ user.image_medium }}" width="100" height="100" alt="pp">
<h1 class="text-body-emphasis"> {{ user.login }} [perm:{{ user.admin.level }}]
<span class="badge badge-sm align-vertical bg-info text-dark">{{ user.tag | safe }}</span>
{%- if user.tag -%}
<span class="badge badge-sm align-vertical bg-info text-dark">{{ user.tag | safe }}</span>
{%- endif -%}
<span class="badge badge-sm align-vertical bg-danger">{{ user.admin.tag | safe }}</span></h1>
</div>
@ -213,7 +219,7 @@
<tr>
<td>{{ user.user_id }}</td>
<td>{{ user.level }}</td>
<td>{{ user.tag }}</td>
<td> <span class="badge badge-sm align-vertical bg-danger">{{ user.tag | safe }}</span> </td>
<td>
<button class="btn btn-success btn-sm" onclick="openChangeTagFor({{ user.user_id }} , '{{ user.tag }}')">+</button>
</td>
@ -252,6 +258,34 @@
</tbody>
</table>
</div>
<div class="container mb-2 p-2 shadow rounded bg-body-tertiary">
<h4>Tags
<button class="btn btn-success btn-sm" onclick="openChangeUserTagFor(null, '')">+</button>
</h4>
<table class="table">
<thead>
<tr>
<th scope="col">User ID</th>
<th scope="col">Login</th>
<th scope="col">Tag</th>
<th scope="col">+</th>
</tr>
</thead>
<tbody>
{% for user in tags %}
<tr>
<th scope="row">{{ user.id }}</th>
<td> {{ user.name }}</td>
<td> <span class="badge badge-sm align-vertical bg-info text-dark">{{ user.tag | safe }}</span> </td>
<td>
<button class="btn btn-success btn-sm" onclick="openChangeUserTagFor('{{ user.name }}', '{{ user.tag }}')">+</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="container mb-2 p-2 shadow rounded bg-body-tertiary">
@ -411,6 +445,24 @@
modalElem.querySelector("input[name='tag']").value = current_tag;
let modal = new bootstrap.Modal(modalElem).show();
}
function openChangeUserTagFor(login, current_tag)
{
let modalElem = document.querySelector("#ChangeUserTagModal");
if (!modalElem)
return ;
if (login !== null)
{
modalElem.querySelector("input[name='login']").value = login.toString();
}
else
{
modalElem.querySelector("input[name='login']").type = "";
modalElem.querySelector("#changeUserTagLogin").classList.remove("hidden");
}
modalElem.querySelector("input[name='tag']").value = current_tag;
let modal = new bootstrap.Modal(modalElem).show();
}
document.getElementById('changeTag').addEventListener('submit', function (event) {
event.preventDefault();
@ -441,12 +493,12 @@
}).catch(error => triggerToast(`Une erreur s'est produite (${error.status})`, false));
});
document.getElementById('setTag').addEventListener('submit', function (event) {
document.getElementById('setUserTag').addEventListener('submit', function (event) {
event.preventDefault();
const formData = new FormData(event.target);
fetch('/admin/settag', {
fetch('/admin/set_usertag', {
method: 'POST',
body: formData
}).then(async response => {