Overview
TheviewTareas function is a Django view that handles all task-related operations including creating, editing, deleting, and listing tasks. This view is protected by the @login_required decorator and supports both GET and POST requests with action-based routing.
Location: CTP/view_registros.py:13
Function Signature
Decorator
Requires user authentication before accessing this view. Unauthenticated users are redirected to the login page.
Request Parameters
Context Data
The view initializes a context dictionary with default values:Company/organization name displayed in templates
Display name for the current section
Base URL path for task operations
Action Parameters
Specifies the operation to perform. Supported values:
agregar- Add new taskeditar- Edit existing taskeliminar- Delete task (soft delete)pdflistado- Generate PDF listingconsultar- Query task details (AJAX)
Task ID for edit, delete, and consultar actions
Actions
agregar (Add Task)
Creates a new task with transaction management and duplicate validation. GET Request:Duplicate Prevention: The view checks if a task with the same
nombre_tarea already exists before creating a new record.Form instance for rendering in template (GET)
Success or error messages added to Django message framework
editar (Edit Task)
Updates an existing task’s information. GET Request:ID of the task to edit
Pre-populated form instance with existing task data
eliminar (Delete Task)
Performs a soft delete by setting thestatus field to False.
GET Request:
Soft Delete: Tasks are not permanently removed from the database. The
status field is set to False to hide them from active listings.ID of the task to delete
pdflistado (PDF Listing)
Generates a PDF-ready listing of all tasks. GET Request:Django QuerySet containing all tasks ordered by
nombre_tareaRendered HTML template (POST request returns JSON)
consultar (Query Task Details)
Returns task details as JSON for AJAX requests. GET Request:ID of the task to query
Operation success status
Rendered HTML template containing task details (on success)
Error message (on failure)
Default View (Listing)
When no action is specified, the view returns a listing of all active tasks:Django QuerySet containing all active tasks (status=True) ordered by task name and assigned person
Transaction Management
All write operations (agregar, editar, eliminar) use Django’stransaction.atomic() context manager to ensure database consistency:
Atomicity: If any error occurs during a transaction, all database changes are rolled back automatically to maintain data integrity.
Message Framework
The view uses Django’s message framework for user feedback:Displays success messages after successful operationsExamples:
- “Tarea Guardada Correctamente” (after creating task)
- “Tarea guardada exitosamente!” (after editing task)
- “Tareas eliminado” (after deleting task)
Displays error messages when operations failExamples:
- “El nombre está repetido” (duplicate task name)
- Exception messages from try/except blocks
Form Integration
The view integrates with TareasForm for data validation and rendering:Custom Save Method
The task model uses a custom save method that accepts the request object:The
Tareas model has a custom save() method that accepts a request parameter, allowing it to capture additional context during creation.Dependencies
URL Routing
Base Path:/Tareas/
Action URLs:
/Tareas/?action=agregar- Add new task/Tareas/?action=editar&id=1- Edit task with ID 1/Tareas/?action=eliminar&id=1- Delete task with ID 1/Tareas/?action=pdflistado- Generate PDF listing/Tareas/?action=consultar&id=1- Query task details
Template Integration
The view renders different templates based on the action:Form template for adding and editing tasks
Confirmation template for task deletion
Main listing template for displaying all active tasks
PDF-ready template for task listings
AJAX template fragment for task details