Overview
Theviewtrabajadores function is a Django view that handles all team member (encargado/trabajadores) operations including creating, editing, deleting, and listing team members. This view is protected by the @login_required decorator and supports both GET and POST requests with action-based routing.
Location: CTP/view_trabajadores.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 team member operations
Action Parameters
Specifies the operation to perform. Supported values:
agregar- Add new team membereditar- Edit existing team membereliminar- Delete team member (soft delete)pdflistado- Generate PDF listingconsultar- Query team member details (AJAX)
Team member ID for edit, delete, and consultar actions
Actions
agregar (Add Team Member)
Creates a new team member with transaction management and duplicate validation. GET Request:Duplicate Prevention: The view checks if a team member with the same
nombres already exists before creating a new record to prevent duplicates.Form instance for rendering in template (GET)
Success or error messages added to Django message framework
editar (Edit Team Member)
Updates an existing team member’s information. GET Request:ID of the team member to edit
Pre-populated form instance with existing team member data
eliminar (Delete Team Member)
Performs a soft delete by setting thestatus field to False.
GET Request:
Soft Delete: Team members are not permanently removed from the database. The
status field is set to False to hide them from active listings while preserving historical data.ID of the team member to delete
pdflistado (PDF Listing)
Generates a PDF-ready listing of all active team members. GET Request:Django QuerySet containing all active team members (status=True) ordered by name
Rendered HTML template (POST request returns JSON)
consultar (Query Team Member Details)
Returns team member details as JSON for AJAX requests. GET Request:ID of the team member to query
Operation success status
Rendered HTML template containing team member details (on success)
Error message (on failure)
Default View (Listing)
When no action is specified, the view returns a listing of all active team members:Django QuerySet containing all active team members (status=True)
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:
- “Registro Guardado Correctamente” (after creating team member)
- “El registro se editó correctamente” (after editing team member)
- “registro eliminado” (after deleting team member)
Displays error messages when operations failExamples:
- “Registro ya existe” (duplicate team member name)
- “error” (form validation failure)
- Exception messages from try/except blocks
Form Integration
The view integrates with trabajadoresForm for data validation and rendering:Custom Save Method
The team member model uses a custom save method that accepts the request object:The
encargado model has a custom save() method that accepts a request parameter, allowing it to capture additional context such as user information during creation.Dependencies
URL Routing
Base Path:/trabajadores/
Action URLs:
/trabajadores/?action=agregar- Add new team member/trabajadores/?action=editar&id=1- Edit team member with ID 1/trabajadores/?action=eliminar&id=1- Delete team member with ID 1/trabajadores/?action=pdflistado- Generate PDF listing/trabajadores/?action=consultar&id=1- Query team member details
Template Integration
The view renders different templates based on the action:Form template for adding and editing team members
Confirmation template for team member deletion
Main listing template for displaying all active team members
PDF-ready template for team member listings
AJAX template fragment for team member details