From ace8b704196bd159aa620683569b193535d5ef54 Mon Sep 17 00:00:00 2001 From: kevin Date: Fri, 14 Feb 2025 00:15:09 +0100 Subject: [PATCH] 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 --- modelo.py | 34 +++++++++++++++++++++++++++------- vista.py | 25 ++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/modelo.py b/modelo.py index 79b8e29..843bc88 100644 --- a/modelo.py +++ b/modelo.py @@ -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: diff --git a/vista.py b/vista.py index 60daeb4..e813726 100644 --- a/vista.py +++ b/vista.py @@ -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())