Overview
TheUser model represents registered users in the SWL Library Management System, including both regular clients and premium users. It extends Flask-Login’s UserMixin for authentication support and includes password hashing capabilities.
Fields
Primary key identifier for the user
User’s email address. Unique constraint applied. Nullable to support users without email.
User’s document/ID number. Must be unique and is required for all users.
User’s full name. Required field.
User’s phone number. Optional field.
User’s role in the system (e.g., ‘cliente’, ‘premium’, ‘admin’). Required field.
Name of the academic program or affiliation. Optional field.
Hashed password for authentication. Never store plain text passwords.
Methods
set_password(password)
Sets the user’s password by generating a secure hash. Parameters:password(str): Plain text password to hash and store
check_password(password)
Verifies a password against the stored hash. Parameters:password(str): Plain text password to verify
bool: True if password matches, False otherwise
Relationships
loans
Back reference to all loans associated with this user.- Type: Dynamic relationship to
Loanmodel - Access:
user.loans.all()oruser.loans.filter_by(status='activo')
Usage Examples
Creating a New User
User Authentication
Querying User Loans
Flask-Login Integration
The User model extendsUserMixin which provides:
is_authenticated: Property that returns True if user is authenticatedis_active: Property that returns True if user account is activeis_anonymous: Property that returns False for regular usersget_id(): Method that returns the user ID as a string
Security Notes
- Passwords are hashed using Werkzeug’s
generate_password_hash - Never store or log plain text passwords
- The
password_hashfield should never be exposed in API responses - Email addresses have unique constraint to prevent duplicates