import json from django.contrib.auth.decorators import login_required from django.http import HttpResponse from django.shortcuts import get_object_or_404, render from .models import Task def task_response(data, status=200): """Helper function to return a task response""" return HttpResponse(json.dumps(data, default=str), status=status, content_type='application/json') @login_required def task_status(request): """API endpoint to check task status""" task_uuid = request.GET.get('task_uuid') if not task_uuid: return task_response({ 'status': 'error', 'message': 'task_uuid parameter required' }, status=400) try: task = Task.objects.get(uuid=task_uuid) # Get latest entries latest_entries = list(task.entries.order_by('-created_at')[:10].values( 'message', 'created_at' )) return task_response({ 'status': 'success', 'task': { 'uuid': str(task.uuid), 'proxmox_upid': task.proxmox_upid, 'status': task.status, 'created_at': task.created_at.isoformat(), 'entries': latest_entries } }, status=200) except Task.DoesNotExist: return task_response({ 'status': 'error', 'message': f'Task {task_uuid} not found' }, status=404) @login_required def task_detail(request, task_uuid): """View to show task details and log entries""" task = get_object_or_404(Task, uuid=task_uuid) entries = task.entries.order_by('created_at') return render(request, 'tasklogger/task_detail.html', { 'task': task, 'entries': entries }) @login_required def task_list(request): """View to list recent tasks""" tasks = Task.objects.order_by('-created_at')[:50] return render(request, 'tasklogger/task_list.html', { 'tasks': tasks })