Sistema MRP is organized around a strict three-layer pattern that keeps UI rendering, business logic, and data access cleanly separated. Pages drive the Streamlit interface and own the lifecycle of every database session. Services hold all business logic but are stateless with respect to sessions. Models are pure SQLAlchemy ORM classes with no embedded logic.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/ElthonJohan/Sistema-MRP/llms.txt
Use this file to discover all available pages before exploring further.
Three-layer pattern
Data flows in one direction through the stack:| Layer | Location | Responsibility |
|---|---|---|
| Pages | pages/ | Render UI, call guards, open and close db sessions |
| Services | services/ | Receive db: Session, execute all business logic |
| Models | models/ | Pure SQLAlchemy ORM declarations, no logic |
| DB | SQLite / PostgreSQL | Persistent storage |
Database sessions are opened and closed exclusively by pages. A page calls
db = SessionLocal() before invoking any service and calls db.close() (or uses a try/finally block) when finished. Services receive the session as a parameter and must never open or close one themselves.Entry points
The project has three meaningful entry points:pages/login.py— The real user-facing entry point. All unauthenticated users land here.app.py— A legacy redirect shell. It renders a brief hero page on startup but immediately redirects users to login. It is not the active UI.init_db.py— One-time (and idempotent) setup script. RunsBase.metadata.create_all(), executesrun_migrations(), and seeds thesuperadminaccount if it does not already exist.
Directory tree
Key utilities
utils/auth.py — Access control guards used at the top of every page:
| Function | Behavior |
|---|---|
require_login() | Redirects to pages/login.py if no active session exists |
require_cliente() | Calls require_login(), then redirects superadmin to /admin unless impersonation is active |
require_superadmin() | Calls require_login(), then redirects non-superadmin users to /dashboard |
get_current_user_id() | Returns the impersonated user’s ID when impersonation is active, otherwise the real user’s ID |
utils/session_manager.py — Provides init_session(), which reads the token from st.query_params["token"] and validates it against the user_sessions table, and logout_session(), which invalidates the session record and clears st.session_state.
utils/pdf_generator.py — generate_dispatch_pdf(dispatch) builds a “Guía de Remisión” PDF using fpdf2 and saves it to storage/guides/<guia_number>.pdf. The guide number format is GR-YYYYMMDD-{dispatch_id:05d}. The storage/guides/ directory is created on demand.