diff --git a/Templates/Details/details_base.html b/Templates/Details/details_base.html index 7415639..8eefb20 100644 --- a/Templates/Details/details_base.html +++ b/Templates/Details/details_base.html @@ -156,8 +156,7 @@

{% trans "Deja tu opinión" %}

if (favoriteBtn) { favoriteBtn.addEventListener('click', function() { const targetUrl = this.getAttribute('data-url'); - - const csrftoken = getCookie('csrftoken'); // Obtenemos el token real + const csrftoken = getCookie('csrftoken'); fetch(targetUrl, { method: 'POST', @@ -175,41 +174,50 @@

{% trans "Deja tu opinión" %}

}); } - const platformButtons = document.querySelectorAll('.track-platform-btn'); - const toast = document.getElementById('history-toast'); - - platformButtons.forEach(btn => { - btn.addEventListener('click', function() { - const contingutId = this.getAttribute('data-contingut-id'); - const apiId = this.getAttribute('data-api-id'); - - const csrftoken = getCookie('csrftoken'); // Obtenemos el token real - - fetch("{% url 'register_platform_click' %}", { - method: 'POST', - headers: { - 'X-CSRFToken': csrftoken, - 'Content-Type': 'application/json', - 'X-Requested-With': 'XMLHttpRequest' - }, - body: JSON.stringify({ contingut_id: contingutId, api_id: apiId }) - }) - .then(response => { - if (!response.ok) throw new Error('Error en el registro del clic'); - return response.json(); - }) - .then(data => { - if (data.status === 'success') { - // Solo disparamos el Toast verde de confirmación - toast.classList.add('show'); - setTimeout(() => { - toast.classList.remove('show'); - }, 3000); - } - }) - .catch(error => console.error('Error tracking click:', error)); + // 2. Lógica de plataformas según si el usuario está logueado o no + {% if user.is_authenticated %} + // Si ESTÁ logueado: Rastreamos el clic y mostramos el Toast + const platformButtons = document.querySelectorAll('.track-platform-btn'); + const toast = document.getElementById('history-toast'); + + platformButtons.forEach(btn => { + btn.addEventListener('click', function() { + const contingutId = this.getAttribute('data-contingut-id'); + const apiId = this.getAttribute('data-api-id'); + const csrftoken = getCookie('csrftoken'); + + fetch("{% url 'register_platform_click' %}", { + method: 'POST', + headers: { + 'X-CSRFToken': csrftoken, + 'Content-Type': 'application/json', + 'X-Requested-With': 'XMLHttpRequest' + }, + body: JSON.stringify({ contingut_id: contingutId, api_id: apiId }) + }) + .then(response => { + if (!response.ok) throw new Error('Error en el registro del clic'); + return response.json(); + }) + .then(data => { + if (data.status === 'success') { + toast.classList.add('show'); + setTimeout(() => { + toast.classList.remove('show'); + }, 3000); + } + }) + .catch(error => console.error('Error tracking click:', error)); + }); + }); + {% else %} + const platformButtons = document.querySelectorAll('.track-platform-btn'); + platformButtons.forEach(btn => { + btn.addEventListener('click', function() { + window.location.href = "{% url 'login' %}"; + }); }); - }); + {% endif %} }); {% endblock %} \ No newline at end of file diff --git a/Templates/paginator.html b/Templates/paginator.html new file mode 100644 index 0000000..3cb8853 --- /dev/null +++ b/Templates/paginator.html @@ -0,0 +1,44 @@ +{% load i18n %} + + + + \ No newline at end of file diff --git a/users/middleware.py b/users/middleware.py index 7109573..3ee5712 100644 --- a/users/middleware.py +++ b/users/middleware.py @@ -22,6 +22,7 @@ def __call__(self, request): 'update_user_role', 'gestion_cartelleres', 'editar_cartellera', + 'system_logs', ] if current_url_name not in exempt_url_names and not request.path.startswith('/static/'): diff --git a/users/templates/users/parts/system_logs.html b/users/templates/users/parts/system_logs.html new file mode 100644 index 0000000..e7e2100 --- /dev/null +++ b/users/templates/users/parts/system_logs.html @@ -0,0 +1,78 @@ +{% extends 'users/types/staff_admin.html' %} +{% load i18n %} + +{% block staff_admin_content %} +
+
+
+

{% trans "Logs de Sincronización" %}

+

{% trans "Registro histórico del Scheduler con las APIs externas." %}

+
+
+ +
+
+ + + + + + + + + + + {% for log in logs %} + + + + + + + {% empty %} + + + + {% endfor %} + +
{% trans "Fecha de Inicio" %}{% trans "Fecha Fin" %}{% trans "Estado" %}{% trans "Resumen" %}
+ {{ log.start_time|date:"d M Y, H:i:s" }} + + {% if log.end_time %} + {{ log.end_time|date:"d M Y, H:i:s" }} + {% else %} + {% trans "En curso..." %} + {% endif %} + + {% if log.status == 'SUCCESS' %} + + Éxito + + {% elif log.status == 'ERROR' %} + + Error + + {% else %} + + {{ log.status }} + + {% endif %} + + {% if log.summary %} + {{ log.summary|truncatechars:80 }} + {% else %} + - + {% endif %} +
+ {% trans "Todavía no hay ningún registro de sincronización." %} +
+
+
+ + {% if logs.has_other_pages %} +
+ {% include 'paginator.html' with items=logs %} +
+ {% endif %} +
+{% endblock %} \ No newline at end of file diff --git a/users/templates/users/types/staff_admin.html b/users/templates/users/types/staff_admin.html index e6f2aa1..6136a70 100644 --- a/users/templates/users/types/staff_admin.html +++ b/users/templates/users/types/staff_admin.html @@ -12,10 +12,14 @@ {% trans 'Carteleras' %} + -
+
{% block staff_admin_content %}{% endblock %}
-{% endblock %} +{% endblock %} \ No newline at end of file diff --git a/users/urls.py b/users/urls.py index 6c7af8c..d435512 100644 --- a/users/urls.py +++ b/users/urls.py @@ -1,5 +1,6 @@ from django.urls import path from . import views +from web_app.views import system_logs urlpatterns = [ # Staff Admin URLs @@ -10,7 +11,7 @@ path('api/update-role/', views.update_user_role, name='update_user_role'), path('staff-admin/cartelleres/', views.gestion_cartelleres, name='gestion_cartelleres'), path('staff-admin/cartelleres/editar//', views.editar_cartellera, name='editar_cartellera'), - + path('staff-admin/logs/', system_logs, name='system_logs'), # User URLs path('profile/', views.user_profile, name='user_profile'), path('history/', views.history, name='history'), diff --git a/users/views.py b/users/views.py index ff42e15..849f821 100644 --- a/users/views.py +++ b/users/views.py @@ -480,3 +480,4 @@ def editar_cartellera(request, contingut_id): return render(request, 'users/parts/editar_cartellera.html', { 'contingut': contingut, }) + diff --git a/web_app/views.py b/web_app/views.py index 91a4bac..b4a32b8 100644 --- a/web_app/views.py +++ b/web_app/views.py @@ -4,7 +4,7 @@ from django.shortcuts import render, redirect, get_object_or_404 from web_app.forms import CustomUserAdminCreationForm, CustomUserCreationForm from web_app.models import AgeRating, API, CustomUser, Director, Genre, Movie, UserProfile, Series, Contingut, \ - AgeRating, Valoracio, Visualitzacio + AgeRating, Valoracio, Visualitzacio, SyncLog from web_app import utils import json from itertools import chain @@ -266,3 +266,16 @@ def register_platform_click(request): return JsonResponse({'status': 'error', 'message': 'Método no permitido'}, status=405) + +@user_passes_test(lambda u: u.is_superuser) +def system_logs(request): + if not request.user.is_authenticated: + return redirect('login') + + logs = SyncLog.objects.all().order_by('-start_time') + paginator = Paginator(logs, 20) + page_obj = paginator.get_page(request.GET.get('page', 1)) + + return render(request, 'users/parts/system_logs.html', { + 'logs': page_obj + }) \ No newline at end of file