mirror of
https://github.com/marcogll/telegram_new_socias.git
synced 2026-01-13 13:15:16 +00:00
This commit introduces a three-database architecture to the application, as specified in the `db_logic.md` file. The changes include: - A SQL initialization script (`db/init/init.sql`) to create the `USERS_ALMA`, `vanity_hr`, and `vanity_attendance` databases and their respective tables. - SQLAlchemy models for all tables, organized into separate files within the `models` directory. - Refactoring of the database connection logic in `modules/database.py` to support connections to all three databases. - Creation of a `modules/logger.py` to handle request logging to the `USERS_ALMA` database. - Updates to `docker-compose.yml` to mount the initialization script and build the bot image locally. - Updates to `.env.example` to include the new database environment variables. - Restoration of the data dictionary to `db_tasks.md`.
30 lines
899 B
Python
30 lines
899 B
Python
import logging
|
|
from modules.database import SessionUsersAlma
|
|
from models.users_alma_models import RequestLog
|
|
|
|
def log_request(telegram_id, username, command, message):
|
|
if not SessionUsersAlma:
|
|
logging.debug("DB log omitted (DB not configured).")
|
|
return
|
|
|
|
try:
|
|
db_session = SessionUsersAlma()
|
|
except Exception as exc:
|
|
logging.error(f"Could not create DB session, logging is disabled: {exc}")
|
|
return
|
|
try:
|
|
log_entry = RequestLog(
|
|
telegram_id=str(telegram_id),
|
|
username=username,
|
|
command=command,
|
|
message=message
|
|
)
|
|
db_session.add(log_entry)
|
|
db_session.commit()
|
|
logging.info(f"Log saved: {command} from {username}")
|
|
except Exception as e:
|
|
logging.error(f"Error saving log: {e}")
|
|
db_session.rollback()
|
|
finally:
|
|
db_session.close()
|