Cambios a las 3 bases Model mapper repo para que funcionen a partir de las clases heredando todos los metodos comunes
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from sqlalchemy import Column, String, Table, MetaData
|
||||
from pgvector.sqlalchemy import Vector
|
||||
from sqlalchemy.orm import registry, Session
|
||||
from src.TextManager.nota import Nota
|
||||
from src.ConexionSql.Base_conexion import ConexionBase
|
||||
from typing import Tuple
|
||||
import re
|
||||
|
||||
|
||||
from src.ArquitectureLayer.Mapper import Mapper_base
|
||||
from src.ArquitectureLayer.Model import Model_base
|
||||
from src.ArquitectureLayer.Repo import Repo_base
|
||||
|
||||
# ----------------------
|
||||
# Cargar .env
|
||||
# ----------------------
|
||||
from entrypoint import ENV_PATH
|
||||
load_dotenv(ENV_PATH)
|
||||
|
||||
# ----------------------
|
||||
# REGISTRO DINÁMICO PARA TABLAS
|
||||
# ----------------------
|
||||
mapper_registry = registry()
|
||||
|
||||
# ----------------------
|
||||
# FUNCIONES AUXILIARES
|
||||
# ----------------------
|
||||
|
||||
def generar_tabla_nota_para_biblioteca(biblioteca_nombre: str, vector_dim: int, metadata: MetaData) -> Tuple[Table, type]:
|
||||
"""
|
||||
Genera una tabla dinámica y modelo ORM para una biblioteca dada, con campos vectoriales.
|
||||
"""
|
||||
# Normalización robusta del nombre de la tabla
|
||||
nombre_tabla = re.sub(r"[^a-zA-Z0-9_]", "_", biblioteca_nombre.strip().lower())
|
||||
|
||||
tabla = Table(
|
||||
nombre_tabla,
|
||||
metadata,
|
||||
Column("id", String, primary_key=True),
|
||||
Column("titulo", String, nullable=False),
|
||||
Column("tags", String),
|
||||
Column("conexiones", String),
|
||||
Column("texto", String),
|
||||
Column("resumen", String),
|
||||
Column("vector", Vector(vector_dim), nullable=True),
|
||||
Column("vector_resumen", Vector(vector_dim), nullable=True),
|
||||
*Model_base.__table__.columns # Añade columnas del sistema si Model_base es declarativo
|
||||
)
|
||||
|
||||
class NotaModel(Model_base):
|
||||
"""
|
||||
Modelo ORM generado dinámicamente para notas de una biblioteca.
|
||||
"""
|
||||
def __init__(self, nota):
|
||||
self.id = nota.id
|
||||
self.titulo = nota.titulo
|
||||
self.tags = ",".join(nota.tags)
|
||||
self.conexiones = ",".join(nota.conexiones)
|
||||
self.texto = nota.texto
|
||||
self.resumen = nota.resumen
|
||||
self.vector = nota.vector
|
||||
self.vector_resumen = getattr(nota, "vector_resumen", None)
|
||||
|
||||
# Registrar solo si aún no se ha hecho
|
||||
if NotaModel.__name__ not in mapper_registry._class_registry:
|
||||
mapper_registry.map_imperatively(NotaModel, tabla)
|
||||
|
||||
return tabla, NotaModel
|
||||
|
||||
|
||||
# ----------------------
|
||||
# MAPPER
|
||||
# ----------------------
|
||||
|
||||
class NotaMapper(Mapper_base[Nota, object]): # Usa `object` si el modelo es dinámico
|
||||
@staticmethod
|
||||
def to_model(nota: Nota):
|
||||
return {
|
||||
"id": nota.id,
|
||||
"titulo": nota.titulo,
|
||||
"tags": ",".join(nota.tags),
|
||||
"conexiones": ",".join(nota.conexiones),
|
||||
"texto": nota.texto,
|
||||
"resumen": nota.resumen,
|
||||
"vector": nota.vector,
|
||||
"vector_resumen": nota.vector_resumen
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def from_model(model) -> Nota:
|
||||
return Nota(
|
||||
id=model.id,
|
||||
titulo=model.titulo,
|
||||
tags=model.tags.split(",") if model.tags else [],
|
||||
conexiones=model.conexiones.split(",") if model.conexiones else [],
|
||||
texto=model.texto,
|
||||
resumen=model.resumen,
|
||||
vector=list(model.vector) if model.vector is not None else None,
|
||||
vector_resumen=list(model.vector_resumen) if model.vector_resumen is not None else None
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def to_dict(nota: Nota) -> dict:
|
||||
return {
|
||||
"id": nota.id,
|
||||
"titulo": nota.titulo,
|
||||
"tags": ",".join(nota.tags),
|
||||
"conexiones": ",".join(nota.conexiones),
|
||||
"texto": nota.texto,
|
||||
"resumen": nota.resumen,
|
||||
"vector": nota.vector,
|
||||
"vector_resumen": nota.vector_resumen
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def from_dict(data: dict) -> Nota:
|
||||
return Nota(
|
||||
id=data["id"],
|
||||
titulo=data["titulo"],
|
||||
tags=data["tags"].split(",") if data.get("tags") else [],
|
||||
conexiones=data["conexiones"].split(",") if data.get("conexiones") else [],
|
||||
texto=data["texto"],
|
||||
resumen=data["resumen"],
|
||||
vector=data.get("vector"),
|
||||
vector_resumen=data.get("vector_resumen")
|
||||
)
|
||||
|
||||
# ----------------------
|
||||
# REPO
|
||||
# ----------------------
|
||||
|
||||
class NotaRepo(Repo_base):
|
||||
def __init__(self, session: Session, modelo_nota: type):
|
||||
if modelo_nota is None:
|
||||
raise ValueError("No se puede instanciar NotaRepo sin un modelo válido de nota.")
|
||||
super().__init__(session=session, modelo=modelo_nota, mapper=NotaMapper)
|
||||
|
||||
# ------------------------
|
||||
# Métodos personalizados
|
||||
# ------------------------
|
||||
|
||||
def get_by_tag(self, tag: str) -> list[Nota]:
|
||||
query = self.session.query(self.Modelo).filter(self.Modelo.tags.contains([tag]))
|
||||
models = query.all()
|
||||
return self.Mapper.from_model_list(models)
|
||||
|
||||
def search_by_text(self, texto: str) -> list[Nota]:
|
||||
query = self.session.query(self.Modelo).filter(self.Modelo.texto.ilike(f'%{texto}%'))
|
||||
models = query.all()
|
||||
return self.Mapper.from_model_list(models)
|
||||
|
||||
def get_paginated(self, offset: int = 0, limit: int = 10) -> list[Nota]:
|
||||
models = self.session.query(self.Modelo).offset(offset).limit(limit).all()
|
||||
return self.Mapper.from_model_list(models)
|
||||
|
||||
def update(self, id_: str, nota_actualizada: Nota) -> bool:
|
||||
model = self.session.get(self.Modelo, id_)
|
||||
if not model:
|
||||
return False
|
||||
|
||||
# Campos de dominio
|
||||
model.titulo = nota_actualizada.titulo
|
||||
model.texto = nota_actualizada.texto
|
||||
model.tags = nota_actualizada.tags
|
||||
model.conexiones = nota_actualizada.conexiones
|
||||
model.resumen = nota_actualizada.resumen
|
||||
|
||||
# Actualización de campos de sistema
|
||||
model.sys_version = (model.sys_version or 1) + 1
|
||||
|
||||
self.session.commit()
|
||||
return True
|
||||
Reference in New Issue
Block a user