Skip to main content

Overview

The Clients API manages customer records with support for SMS marketing campaigns via Twilio integration.

Cliente Model (Client)

integer
Unique client identifier (auto-generated)
string
Client name (max 150 characters, required)
string
Phone number (max 20 characters, optional)
string
ID/document number (max 20 characters, optional)
boolean
Active status (default: true)
datetime
Creation timestamp (auto-generated)
integer
Foreign key to User who created the client (PROTECT constraint)

PlantillaSMS Model (SMS Template)

integer
Template identifier
string
Template name (max 100 characters)
text
Message template (use {nombre} for personalization)
boolean
Active status (default: true)
datetime
Creation timestamp
integer
Foreign key to User

CampanaSMS Model (SMS Campaign)

integer
Campaign identifier
string
Campaign name (max 200 characters)
text
Message content
integer
Total recipients (default: 0)
integer
Successfully sent count (default: 0)
integer
Failed send count (default: 0)
string
Campaign status: pendiente, procesando, completado, or error
datetime
Creation timestamp
datetime
Completion timestamp (optional)
integer
Foreign key to User

SMSLog Model (SMS Log)

integer
Log entry identifier
integer
Foreign key to CampanaSMS
integer
Foreign key to Cliente
text
Message content sent
string
Send status: enviado or fallido
string
Twilio message SID (max 100 characters, optional)
text
Error message if failed (optional)
datetime
Log timestamp

List Clients

Requires authentication. Returns clients owned by the authenticated user.

Response

Returns rendered HTML with list of active clients.

Create Client

Requires authentication and admin role.

Request Parameters

string
required
Client name (max 150 characters)
string
Phone number (max 20 characters)
string
ID/document number (max 20 characters)
boolean
default:"true"
Active status

Response

Success (HTTP 302): Redirects to /clientes/ with flash message Validation Error (HTTP 200): Re-renders form with errors

Create Client (AJAX)

Response

Error:

Update Client

Requires authentication and admin role.

URL Parameters

integer
required
Client ID to update

Response

Success: Redirects to clients list Not Found (HTTP 404): Client doesn’t exist or doesn’t belong to user

Delete Client

Requires authentication and admin role. Cannot delete clients referenced in sales (PROTECT constraint).

URL Parameters

integer
required
Client ID to delete

Response

Success: Client deleted, redirects to list Protected Error: Cannot delete due to foreign key constraint (client has sales)

SMS Templates

List SMS Templates

Get Templates (AJAX)

Response

Create SMS Template

Requires authentication and admin role.
string
required
Template name (max 100 characters)
text
required
Message template. Use {nombre} placeholder for personalization.
boolean
default:"true"
Active status

Update SMS Template

Delete SMS Template

SMS Campaigns

Send SMS to Clients

Requires Twilio credentials configured in environment variables.
array
required
Array of client IDs to send SMS to
string
required
Message content. Use {nombre} for personalization.
string
Campaign name (optional, auto-generated if not provided)

Response

Message Personalization

The {nombre} placeholder is replaced with each client’s name:

Business Logic

Client Protection

Clients cannot be deleted if they have associated sales:
Error: ProtectedError exception raised

Multi-Tenancy

Clients are isolated per user:
  • Admin users see only their own clients
  • Vendedor users see clients owned by their creado_por admin
  • Enforced via: Cliente.objects.filter(usuario=request.user)

SMS Campaign Tracking

Each SMS sent creates an SMSLog entry:
  • Links to campaign and client
  • Stores Twilio SID for tracking
  • Records success/failure status
  • Logs error messages
Campaign aggregates:
  • total_clientes - Recipients count
  • enviados_exitosos - Success count
  • enviados_fallidos - Failure count
  • estado - Overall campaign status

Example Workflow

  • Sales API - Client sales transactions
  • See source: applications/clientes/models.py and applications/clientes/views.py