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,44 +1,80 @@
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) {
favoriteButton.classList.add("text-light");
// Ajuste para visibilidad en tema oscuro favoriteButton.addEventListener("click", function () {
favoriteButton.classList.add("text-light"); const pasteId = document.body.dataset.pasteId;
if (!pasteId) {
console.error("Error: pasteId no definido en el <body>.");
return;
}
favoriteButton.addEventListener("click", function () { const isFavorited = favoriteButton.classList.contains("text-danger");
const pasteId = document.body.dataset.pasteId; const action = isFavorited ? "unfavorite" : "favorite";
if (!pasteId) {
console.error("Error: pasteId no definido en el <body>.");
return;
}
const isFavorited = favoriteButton.classList.contains("text-danger"); fetch(`/paste/${pasteId}/${action}`, {
const action = isFavorited ? "unfavorite" : "favorite"; method: "POST",
headers: { "Content-Type": "application/json" },
credentials: "include"
})
.then(response => response.json())
.then(data => {
if (data.message.includes("Added")) {
favoriteButton.classList.add("text-danger");
favoriteButton.classList.remove("text-secondary");
favoriteButton.setAttribute("title", "Remove from Favorites");
} else {
favoriteButton.classList.add("text-secondary");
favoriteButton.classList.remove("text-danger");
favoriteButton.setAttribute("title", "Add to Favorites");
}
showToast(data.message, "bg-success");
})
.catch(error => {
console.error("Error updating favorites:", error);
showToast("Error updating favorites.", "bg-danger");
});
});
}
fetch(`/paste/${pasteId}/${action}`, { document.querySelectorAll(".unfavorite-btn").forEach(button => {
button.addEventListener("click", function () {
const pasteId = this.dataset.id;
fetch(`/paste/${pasteId}/unfavorite`, {
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())
.then(data => { .then(data => {
if (data.message.includes("Added")) { if (data.success) {
favoriteButton.classList.add("text-danger"); const row = this.closest("tr");
favoriteButton.classList.remove("text-secondary"); if (row) row.remove();
favoriteButton.setAttribute("title", "Remove from Favorites");
// 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 { } else {
favoriteButton.classList.add("text-secondary"); showToast(data.error || "No se pudo eliminar el favorito.", "bg-danger");
favoriteButton.classList.remove("text-danger");
favoriteButton.setAttribute("title", "Add to Favorites");
} }
showToast(data.message, "bg-success");
}) })
.catch(error => { .catch(error => {
console.error("Error updating favorites:", error); console.error("Error:", error);
showToast("Error updating favorites.", "bg-danger"); showToast("Error al eliminar favorito.", "bg-danger");
}); });
}); });
}); });
});

View File

@ -390,28 +390,29 @@
<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>
<td>{{ fav.filename if fav.filename else "Paste " ~ fav.id }}</td> <td>{{ fav.filename if fav.filename else "Paste " ~ fav.id }}</td>
<td>{{ fav.get_type() }}</td> <td>{{ fav.get_type() }}</td>
<td>{{ fav.size }}</td> <td>{{ fav.size }}</td>
<td>{{ fav.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</td> <td>{{ fav.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</td>
<td> <td>
<a href="{{ url_for('get_paste', id=fav.id) }}" <a href="{{ url_for('get_paste', id=fav.id) }}"
class="btn btn-sm btn-secondary view-btn" class="btn btn-sm btn-secondary view-btn"
data-url="{{ url_for('serve_media', id=fav.id) }}" data-url="{{ url_for('serve_media', id=fav.id) }}"
data-type="{{ fav.content_type }}"> data-type="{{ fav.content_type }}">
View View
</a> </a>
<button class="btn btn-sm btn-danger unfavorite-btn" data-id="{{ fav.id }}"> <button class="btn btn-sm btn-danger unfavorite-btn" data-id="{{ fav.id }}">
Unfavorite Unfavorite
</button> </button>
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
<!-- Paginación Favorites --> <!-- Paginación Favorites -->
{% if favorite_pagination.pages > 1 %} {% if favorite_pagination.pages > 1 %}