How alejandro11102003/bibliotecapy works
Project Type and Purpose
• This is a full-stack web application (“Sistema de Gestión de Biblioteca”) built with Flask. Its goal is to manage book inventories, user registrations, loan transactions, and reporting for a library.
File Structure Analysis
• Top-level files:
• run.py – application entry point
• init_db.py – database initialization script
• config.py – environment & Flask configuration
• requirements.txt – dependency list
• app/ module:
• __init__.py – Flask app & SQLAlchemy setup
• models.py – ORM entities (Book, User, Loan)
• forms.py – WTForms definitions
• routes.py – HTTP controllers (endpoints)
• repositories/ – data access layer (one repository per entity + report)
• factories/ – ModelFactory to convert forms into domain models
• services/ – auxiliary services (e.g., FileService for image uploads)
• static/ & templates/ – frontend assets (CSS, JS, HTML with Bootstrap)
• grafico_numpy.py, pandas.py – data-analysis utilities for charts
• Data folder: app/data holds JSON snapshots but primary storage is MySQL via SQLAlchemy.
Architectural Patterns & Principles
• Repository Pattern: Separates data access logic.
• Factory Pattern: Centralizes object creation from forms.
• Service Layer: Encapsulates cross-cutting concerns (file handling).
• MVC-like: routes.py act as controllers, templates are views, models.py are domain models.
• SOLID principles: SRP, OCP, ISP, DIP are applied across layers.
Main Components
a. Frontend Layer
• Browser – user interface rendered via templates + Bootstrap + JS
• Static server – serves CSS/JS/images from app/static
b. Backend Layer
• Flask Application – handles HTTP requests in routes.py
• Controller Layer – routes call Services and Repositories
• Service Layer – FileService, potential other business services
• Repository Layer – LibroRepository, UsuarioRepository, PrestamoRepository, ReportRepository
• Factory Layer – ModelFactory converting form data to SQLAlchemy models
c. Database
• MySQL (configured via SQLAlchemy) – persistent storage for books, users, loans
d. Data-Analysis & Reporting
• Utility modules (grafico_numpy.py, pandas.py) + Matplotlib/NumPy for chart generation
e. External/Environment
• .env file – environment variables (SECRET_KEY, DATABASE_URL)
• SMTP or other services (if added later for notifications—optional)
Relationships & Data Flow
• User → Browser → HTTP Request → Flask routes
• routes → validate forms → ModelFactory → domain model
• routes → calls Repository to CRUD → DB
• Repository → SQLAlchemy → MySQL
• routes → call reporting Repository → uses pandas/grafico_numpy → Matplotlib → chart rendered in template
• Browser ← rendered HTML templates + chart images
• Static assets served in parallel
Architectural Patterns Highlight
• Clear layering: Presentation (templates/static) → Controllers (routes) → Services/Factories → Data Access (repositories) → Database