Skip to main content

Overview

The Role model manages user roles and permission profiles within the system (e.g., Admin, Editor, Viewer). It provides the foundation for role-based access control (RBAC). Table Name: roles

Fields

Primary Key

id_role
Integer
required
Unique identifier for the role. Auto-incremented primary key.

Core Fields

name
String(50)
required
Name of the role (e.g., “Admin”, “Editor”, “Viewer”). Must be unique.Constraints:
  • Maximum length: 50 characters
  • Unique: true
  • Nullable: false
active
Boolean
default:"true"
required
Indicates whether the role is active in the system.Default: true

Audit Trail Fields

created_at
TIMESTAMP
required
Timestamp when the role was created.Default: Current timestamp (server-side)
updated_at
TIMESTAMP
required
Timestamp when the role was last updated. Automatically updated on record modification.Default: Current timestamp (server-side)On Update: Automatically set to current timestamp
deleted_at
TIMESTAMP
Timestamp when the role was soft-deleted. NULL if the record is not deleted.
created_by
String(100)
Username or identifier of the user who created the role.
updated_by
String(100)
Username or identifier of the user who last updated the role.
deleted_by
String(100)
Username or identifier of the user who soft-deleted the role.

Methods

to_dict()

Serializes the Role model instance to a dictionary format suitable for JSON responses. Returns: dict
id_role
integer
The role’s unique identifier
name
string
The role name
active
boolean
Active status of the role
created_at
string
ISO 8601 formatted creation timestamp
updated_at
string
ISO 8601 formatted last update timestamp
Example Response:
{
  "id_role": 1,
  "name": "Admin",
  "active": true,
  "created_at": "2024-01-15T10:30:00",
  "updated_at": "2024-01-15T10:30:00"
}

Model Source Code

from sqlalchemy.sql import func
from ..extensions import db

class Role(db.Model):
    """
    Modelo de Rol para la gestión de permisos y perfiles de usuario.

    Attributes:
          id_role: Identificador único del rol.
          name: Nombre del rol (ej. 'Admin', 'Editor', 'Viewer').
          active: Indica si el rol está activo o no.

          created_at: Fecha de creación del rol.
          updated_at: Fecha de última actualización del rol.
          deleted_at: Fecha de eliminación lógica del rol.

          created_by: Usuario que creó el rol.
          updated_by: Usuario que actualizó el rol.
          deleted_by: Usuario que eliminó el rol.
    """

    __tablename__ = 'roles'

    id_role = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False, unique=True)
    active = db.Column(db.Boolean, nullable=False, default=True)

    created_at = db.Column(
        db.TIMESTAMP,
        nullable=False,
        server_default=func.current_timestamp()
    )
    updated_at = db.Column(
        db.TIMESTAMP,
        nullable=False,
        server_default=func.current_timestamp(),
        server_onupdate=func.current_timestamp()
    )
    deleted_at = db.Column(db.TIMESTAMP, nullable=True)

    created_by = db.Column(db.String(100), nullable=True)
    updated_by = db.Column(db.String(100), nullable=True)
    deleted_by = db.Column(db.String(100), nullable=True)
    
    def to_dict(self) -> dict:
        """
        Serializa el modelo a diccionario.

        Returns:
            dict: Representación del rol en formato diccionario
        """
        return {
            "id_role": self.id_role,
            "name": self.name,
            "active": self.active,
            "created_at": self.created_at.isoformat() if self.created_at else None,
            "updated_at": self.updated_at.isoformat() if self.updated_at else None,
        }

Notes

  • Foundation for implementing role-based access control (RBAC)
  • The model implements soft delete functionality via the deleted_at field
  • Timestamps are managed automatically by the database
  • The to_dict() method excludes soft-delete audit fields from the response
  • Typical roles include: Admin, Editor, Viewer, Manager

Build docs developers (and LLMs) love