unfavorite fixed in user dashboard

This commit is contained in:
teraflops 2025-06-07 01:06:19 +02:00
parent 5a6de0b2dd
commit e3adeb642f
3 changed files with 102 additions and 46 deletions

View File

@ -71,6 +71,9 @@ HEADERS = {
"Content-Type": "application/json" "Content-Type": "application/json"
} }
def get_client_ip():
return request.headers.get("X-Forwarded-For", request.remote_addr).split(',')[0].strip()
def get_readable_languages(): def get_readable_languages():
""" """
@ -712,6 +715,8 @@ def detect_language(content, unique_filename=None, original_filename=None, detec
def init_routes(app): def init_routes(app):
@app.route('/file/<filename>', methods=['GET']) @app.route('/file/<filename>', methods=['GET'])
def get_file(filename): def get_file(filename):
ip = get_client_ip()
current_app.logger.info(f"Vista del paste {id} desde IP: {ip}")
file_path = os.path.join(UPLOAD_FOLDER, filename) file_path = os.path.join(UPLOAD_FOLDER, filename)
if not os.path.exists(file_path): if not os.path.exists(file_path):
@ -733,6 +738,8 @@ def init_routes(app):
@app.route('/', methods=['GET']) @app.route('/', methods=['GET'])
def index(): def index():
ip = get_client_ip()
current_app.logger.info(f"Vista del paste {id} desde IP: {ip}")
base_url = request.host_url.rstrip('/') base_url = request.host_url.rstrip('/')
user_name = None user_name = None
@ -763,6 +770,8 @@ def init_routes(app):
@app.route('/paste/<int:id>/json', methods=['GET']) @app.route('/paste/<int:id>/json', methods=['GET'])
def get_paste_json(id): def get_paste_json(id):
ip = get_client_ip()
current_app.logger.info(f"Vista del paste {id} desde IP: {ip}")
paste = Paste.query.get(id) paste = Paste.query.get(id)
if not paste: if not paste:
@ -802,6 +811,8 @@ def init_routes(app):
@app.route('/paste/<int:id>/raw', methods=['GET']) @app.route('/paste/<int:id>/raw', methods=['GET'])
def get_paste_raw(id): def get_paste_raw(id):
ip = get_client_ip()
current_app.logger.info(f"Vista del paste {id} desde IP: {ip}")
paste = Paste.query.get(id) paste = Paste.query.get(id)
if not paste: if not paste:
@ -971,6 +982,8 @@ def init_routes(app):
@app.route('/paste/<int:id>', methods=['GET']) @app.route('/paste/<int:id>', methods=['GET'])
def get_paste(id): def get_paste(id):
ip = get_client_ip()
current_app.logger.info(f"Vista del paste {id} desde IP: {ip}")
paste = Paste.query.get(id) paste = Paste.query.get(id)
if not paste: if not paste:
@ -1316,6 +1329,8 @@ def init_routes(app):
''' '''
@app.route('/paste/<int:id>/download', methods=['GET']) @app.route('/paste/<int:id>/download', methods=['GET'])
def download_paste(id): def download_paste(id):
ip = get_client_ip()
current_app.logger.info(f"Vista del paste {id} desde IP: {ip}")
paste = Paste.query.get_or_404(id) paste = Paste.query.get_or_404(id)
# Comprobación de permisos para pastes privados # Comprobación de permisos para pastes privados
@ -1689,6 +1704,8 @@ def init_routes(app):
@app.route('/paste/<int:id>/download_file', methods=['GET']) @app.route('/paste/<int:id>/download_file', methods=['GET'])
def download_file(id): def download_file(id):
ip = get_client_ip()
current_app.logger.info(f"Vista del paste {id} desde IP: {ip}")
logging.info(f"Confirmed download request for paste ID: {id}") logging.info(f"Confirmed download request for paste ID: {id}")
paste = Paste.query.get(id) paste = Paste.query.get(id)
if not paste: if not paste:
@ -1733,6 +1750,8 @@ def init_routes(app):
@app.route('/paste/<int:id>/stream_download', methods=['GET']) @app.route('/paste/<int:id>/stream_download', methods=['GET'])
def stream_download(id): def stream_download(id):
ip = get_client_ip()
current_app.logger.info(f"Vista del paste {id} desde IP: {ip}")
paste = Paste.query.get(id) paste = Paste.query.get(id)
if not paste or not paste.filename: if not paste or not paste.filename:
return jsonify({"error": "Paste or file not found"}), 404 return jsonify({"error": "Paste or file not found"}), 404

View File

@ -1,8 +1,6 @@
document.addEventListener("DOMContentLoaded", function () { document.addEventListener("DOMContentLoaded", function () {
const favoriteButton = document.getElementById("favorite-button"); const favoriteButton = document.getElementById("favorite-button");
if (!favoriteButton) return; if (favoriteButton) {
// Ajuste para visibilidad en tema oscuro
favoriteButton.classList.add("text-light"); favoriteButton.classList.add("text-light");
favoriteButton.addEventListener("click", function () { favoriteButton.addEventListener("click", function () {
@ -17,9 +15,7 @@ document.addEventListener("DOMContentLoaded", function () {
fetch(`/paste/${pasteId}/${action}`, { fetch(`/paste/${pasteId}/${action}`, {
method: "POST", method: "POST",
headers: { headers: { "Content-Type": "application/json" },
"Content-Type": "application/json"
},
credentials: "include" credentials: "include"
}) })
.then(response => response.json()) .then(response => response.json())
@ -40,5 +36,45 @@ document.addEventListener("DOMContentLoaded", function () {
showToast("Error updating favorites.", "bg-danger"); showToast("Error updating favorites.", "bg-danger");
}); });
}); });
}
document.querySelectorAll(".unfavorite-btn").forEach(button => {
button.addEventListener("click", function () {
const pasteId = this.dataset.id;
fetch(`/paste/${pasteId}/unfavorite`, {
method: "POST",
headers: { "Content-Type": "application/json" },
credentials: "include"
})
.then(response => response.json())
.then(data => {
if (data.success) {
const row = this.closest("tr");
if (row) row.remove();
// Si ya no hay filas, mostrar mensaje vacío
const tableBody = document.querySelector("#favorites-body");
if (tableBody && tableBody.rows.length === 0) {
tableBody.innerHTML = `
<tr>
<td colspan="6" class="text-center text-muted">
No favorites yet.
</td>
</tr>
`;
}
showToast(data.message, "bg-success");
} else {
showToast(data.error || "No se pudo eliminar el favorito.", "bg-danger");
}
})
.catch(error => {
console.error("Error:", error);
showToast("Error al eliminar favorito.", "bg-danger");
});
});
});
}); });

View File

@ -390,7 +390,7 @@
<th>Actions</th> <th>Actions</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody id="favorites-body">
{% for fav in favorite_pastes %} {% for fav in favorite_pastes %}
<tr id="favorite-{{ fav.id }}"> <tr id="favorite-{{ fav.id }}">
<td>{{ fav.id }}</td> <td>{{ fav.id }}</td>
@ -412,6 +412,7 @@
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
<!-- Paginación Favorites --> <!-- Paginación Favorites -->
{% if favorite_pagination.pages > 1 %} {% if favorite_pagination.pages > 1 %}