Vista de los archivos adjuntados

Ahora al leer un correo se puede ver los archivos adjuntados
solamente los titulos, pero en la siguente version se podran
descargar
This commit is contained in:
Kevin William Olarte Braun 2025-02-14 00:15:09 +01:00
parent 32abb88fae
commit ace8b70419
2 changed files with 49 additions and 10 deletions

View File

@ -34,12 +34,19 @@ class CorreoModelo:
def correo_existe(self, remitente, asunto, fecha):
return self.collection.find_one({"remitente": remitente, "asunto": asunto, "fecha": fecha}) is not None
def guardar_correo(self, remitente, asunto, fecha, cuerpo):
def guardar_correo(self, remitente, asunto, fecha, cuerpo, archivos_adjuntos=[]):
if self.correo_existe(remitente, asunto, fecha):
return
correo = {"remitente": remitente, "asunto": asunto, "fecha": fecha, "cuerpo": cuerpo}
correo = {
"remitente": remitente,
"asunto": asunto,
"fecha": fecha,
"cuerpo": cuerpo,
"archivos_adjuntos": archivos_adjuntos # Guardar archivos adjuntos
}
self.collection.insert_one(correo)
def descargar_correos(self):
try:
mail = poplib.POP3(self.POP3_SERVER, self.POP3_PORT)
@ -65,16 +72,21 @@ class CorreoModelo:
pass
cuerpo = ""
archivos_adjuntos = []
if msg.is_multipart():
for part in msg.walk():
if part.get_content_maintype() == "multipart":
continue
if part.get_content_type() == "text/plain":
cuerpo = part.get_payload(decode=True).decode(errors="ignore")
break
else:
cuerpo = msg.get_payload(decode=True).decode(errors="ignore")
if part.get("Content-Disposition") is not None:
filename = part.get_filename()
if filename:
archivos_adjuntos.append(filename)
if not self.correo_existe(remitente, asunto, fecha):
self.guardar_correo(remitente, asunto, fecha, cuerpo.strip())
self.guardar_correo(remitente, asunto, fecha, cuerpo.strip(), archivos_adjuntos)
mensajes_nuevos = True
mail.quit()
@ -82,8 +94,16 @@ class CorreoModelo:
except Exception as e:
return str(e)
def obtener_correos(self):
return list(self.collection.find())
correos = list(self.collection.find())
for correo in correos:
correo["_id"] = str(correo["_id"])
if "archivos_adjuntos" not in correo:
correo["archivos_adjuntos"] = []
return correos
def hay_mensajes_nuevos(self):
try:

View File

@ -165,15 +165,34 @@ class CorreoVista:
if not seleccionado:
messagebox.showwarning("⚠️ Advertencia", "Seleccione un correo")
return
correo_id = seleccionado[0]
correo = self.tree.item(correo_id, "values")
# Obtener el correo desde la base de datos
correo_db = self.controlador.modelo.collection.find_one(
{"remitente": correo[0], "asunto": correo[1], "fecha": correo[2]}
)
if correo_db is None:
messagebox.showerror("Error", "No se encontró el correo en la base de datos")
return
archivos_adjuntos = correo_db.get("archivos_adjuntos", []) # Asegurar que siempre exista la clave
self.detalle_text.delete("1.0", tk.END)
self.detalle_text.insert(tk.END, f"📧 Remitente: {correo[0]}\n")
self.detalle_text.insert(tk.END, f"📌 Asunto: {correo[1]}\n")
self.detalle_text.insert(tk.END, f"📅 Fecha: {correo[2]}\n\n")
self.detalle_text.insert(tk.END, f"📝 Mensaje:\n{correo[3]}")
self.detalle_text.insert(tk.END, f"📝 Mensaje:\n{correo[3]}\n\n")
if archivos_adjuntos:
self.detalle_text.insert(tk.END, "📎 Archivos Adjuntos:\n")
for archivo in archivos_adjuntos:
self.detalle_text.insert(tk.END, f" - {archivo}\n")
else:
self.detalle_text.insert(tk.END, "📎 No hay archivos adjuntos.\n")
def actualizar_lista(self):
self.tree.delete(*self.tree.get_children())