Added stuffies to reports, like images and coordinate based geolocation

This commit is contained in:
2026-04-06 23:48:19 -06:00
parent 4395a81815
commit f812d4a664
10 changed files with 147 additions and 14 deletions

View File

@@ -3,6 +3,8 @@ import sys
import os
import logging
from datetime import datetime
from pathlib import Path
from shutil import move
# Add src to path to import modules
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
@@ -11,7 +13,9 @@ from infrastructure.adapters.rabbitmq.consumer import RabbitMQConsumer
from infrastructure.adapters.rabbitmq.messages import ReportMessage, ReportEventType
from infrastructure.adapters.persistence.report_repository_mongo import ReportRepositoryMongo
from infrastructure.adapters.persistence.user_repository_sql import UserRepositorySQL
from infrastructure.adapters.file_storage import image_storage
from domain.reports import Report
from core.config import ConfSettings
# Set up logging
logging.basicConfig(
@@ -64,6 +68,25 @@ class ReportConsumer:
# Parse datetime string
fecha_creacion = datetime.fromisoformat(message.fecha_creacion)
# Renombrar imagen temporal si existe
final_image_filename = None
if message.image_filename:
try:
# Renombrar de temp_userid_type a report_id
temp_path = image_storage.get_image_path(message.image_filename)
final_filename = f"{message.id_reporte}.webp"
final_path = image_storage.get_image_path(final_filename)
if temp_path.exists():
move(str(temp_path), str(final_path))
final_image_filename = final_filename
logger.info(f"Image renamed from {message.image_filename} to {final_filename}")
else:
logger.warning(f"Temporary image not found: {message.image_filename}")
except Exception as img_error:
logger.error(f"Error renaming image: {img_error}", exc_info=True)
# Continuar sin imagen si falla el renombramiento
# Create Report domain object
report = Report(
id_reporte=message.id_reporte,
@@ -71,6 +94,9 @@ class ReportConsumer:
tipo_reporte=message.tipo_reporte,
descripcion=message.descripcion,
ubicacion=message.ubicacion,
lat=message.lat,
lng=message.lng,
image_filename=final_image_filename,
visibilidad=message.visibilidad,
fecha_creacion=fecha_creacion
)
@@ -146,9 +172,21 @@ class ReportConsumer:
try:
logger.info(f"Deleting report: {message.id_reporte}")
# Obtener reportepara acceder a image_filename antes de eliminarlo
report = self.repo.find_by_id(message.id_reporte)
# Eliminar del MongoDB
success = self.repo.delete(message.id_reporte)
if success:
logger.info(f"Report deleted successfully: {message.id_reporte}")
logger.info(f"Report deleted successfully from MongoDB: {message.id_reporte}")
# Eliminar imagen del almacenamiento
if report and report.image_filename:
deleted = image_storage.delete_image(report.image_filename)
if deleted:
logger.info(f"Image deleted: {report.image_filename}")
else:
logger.warning(f"Could not delete image: {report.image_filename}")
else:
logger.warning(f"Failed to delete report: {message.id_reporte}")