In Hábito., a habit is the core tracking unit. Each habit is stored in theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/BrandonCVale/SISTEMA-HABITOS/llms.txt
Use this file to discover all available pages before exploring further.
habitos table and belongs to exactly one user. The Habito model captures the habit’s name, an optional description, the days of the week it should be performed (dias_semana), an activo boolean flag to suspend a habit without deleting it, and two streak counters: racha_actual (current consecutive-day streak) and mejor_racha (all-time best streak). The model also defines an optional horario column (a time-of-day label such as 'Mañana' or 'Tarde'), but this field is not read by the creation route — it is reserved for future use and is always NULL for newly created habits. Every time a user ticks off a habit for the day, a RegistroHabito record is written to a companion table that drives streaks, the heat-map calendar, and the progress charts.
Creating a Habit
Route:POST /habitos/crear
The creation form lives on the dashboard. It submits nombre, an optional descripcion, and one or more dias checkboxes to this route. Validation ensures that both the habit name and at least one day are provided before any database write is attempted.
Submit the form
The route extracts form values and converts the list of day letters into a comma-separated string, because SQLite stores
dias_semana as plain text:| Field | Type | Required | Notes |
|---|---|---|---|
nombre | Text | ✅ | Max 100 characters |
descripcion | Textarea | ❌ | Optional; stored as TEXT |
dias | Checkboxes | ✅ At least one | Multi-select; see day letters below |
Days of the week are stored as a comma-separated string in the
dias_semana column — for example, "L,M,X,J,V" for a weekday-only habit. The letter codes are:| Letter | Day |
|---|---|
L | Lunes (Monday) |
M | Martes (Tuesday) |
X | Miércoles (Wednesday) |
J | Jueves (Thursday) |
V | Viernes (Friday) |
S | Sábado (Saturday) |
D | Domingo (Sunday) |
Listing Habits
Route:GET /habitos/mis_habitos?estado={activos|inactivos|todos}
The habit management screen supports three filter states controlled by the estado query parameter. When estado is omitted it defaults to 'activos'.
HabitoRepository.obtener_habitos_por_usuario applies the filter at the query level:
estado value | SQL condition | Use case |
|---|---|---|
activos | activo = TRUE | Default — habits currently being tracked |
inactivos | activo = FALSE | Habits that have been paused |
todos | (no filter) | Full history of all habits |
Editing a Habit
- Load the form (GET)
- Save changes (POST)
Route: If the
GET /habitos/editar/<id>The route fetches the habit by ID and verifies ownership before rendering the edit form:usuario_id on the habit does not match current_user.id, the request is rejected and the user is sent back to their habit list — no data is returned.Deleting a Habit
Route:POST /habitos/eliminar/<id>
Before deletion, the route performs the same ownership check as the edit flow. Only after the check passes does it call the repository:
Daily Completion Toggle
Route:POST /habitos/completar/<id>
Each habit card on the dashboard includes a completion button. Tapping it calls this route, which toggles the habit’s completion state for today.
HabitoRepository.marcar_completado_hoy checks whether a RegistroHabito already exists for today’s date and either creates or deletes it accordingly:
Marking complete ✅
racha_actual is incremented by 1. If the new value exceeds mejor_racha, mejor_racha is updated to match — preserving the all-time record.Unchecking ↩️
The
RegistroHabito row for today is deleted and racha_actual is decremented by 1, with a floor of 0 so it never goes negative: max(0, racha_actual - 1).The
RegistroHabito table enforces a UniqueConstraint('habito_id', 'fecha', name='uq_habito_fecha_diaria'). This constraint is the database-level guarantee that a habit cannot be marked complete twice on the same day, even if two requests arrive simultaneously.Active vs Inactive Habits
Every habit has anactivo boolean column (default True). When a user marks a habit inactive — either via the edit form or by toggling the activo checkbox — the habit disappears from the dashboard’s daily view but remains in the database with its full completion history intact.
Active habits
Shown on the dashboard (
/pagina_principal/inicio) and in mis_habitos?estado=activos. These are habits the user is currently tracking.Inactive habits
Hidden from the daily dashboard. Accessible at
mis_habitos?estado=inactivos. Can be reactivated at any time through the edit form — all historical RegistroHabito records are preserved.