import os import sys # Ajusta la ruta al directorio raíz del proyecto sys.path.insert(0, '/home/kevinarm/Documents/EFC_V2/backend') os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') import django django.setup() # Ahora puedes importar tus modelos from api.customs.models import Pedimento from api.record.models import Fuente """ id nombre descripcion 1 Pedimento Partida Tag para saber que el archivo guarda una partida 2 Pedimento Completo Tag para saber que el archivo guarda un pedimento completo 3 Pedimento Remesas Tag para saber que el documento almacena remesas 5 Pedimento EDocument Tag para saber que el documento es un EDocument 6 Estado Pedimento Tag para saber que el archivo almacena el Estado del pedimeto 4 Pedimento Acuse Tag para saber que el documento es un Acuse 7 Acuse Cove Tag para saber que el archivo guarda un acuse de cove 8 Cove Tag para saber que el archivo guarda un cove 9 Documento Digitalizacion Tag para saber que es un documento logistico " """ FUENTE = Fuente.objects.filter(pk=1).first() # Asumiendo que la fuente es siempre 1, ajusta según sea necesario TIPOS_DE_DOCUMENTOS = { 'PT': '1', 'AC': '4', 'PC': '2', 'EDC': '5', 'AC_COVE': '7', 'COVE': '8', 'RM': '3', 'LOGISTICO': '9', } class DocumentFile: def __init__(self, document): self.document_name = document self.document_type = self.get_pedimento_tipo() self.fuente = self.get_fuente() self.extension = self.get_extension() self.aduana = self.get_aduana() self.patente = self.get_patente() self.pedimento = self.get_pedimento_numero() self.numero_documento = self.get_numero_documento() self.identificador = self.get_identificacor() def get_fuente(self): return self.document_name.split('_')[0] if '_' in self.document_name else None def get_extension(self): return self.document_name.split('.')[-1] if '.' in self.document_name else None def get_aduana(self): if self.document_type != 'AC_COVE': return self.document_name.split('_')[3] if '_' in self.document_name else None return self.document_name.split('_')[4] if '_' in self.document_name else None def get_patente(self): if self.document_type != 'AC_COVE': return self.document_name.split('_')[4] if '_' in self.document_name else None return self.document_name.split('_')[5] if '_' in self.document_name else None def get_pedimento_numero(self): if self.document_type != 'AC_COVE': return self.document_name.split('_')[5] if '_' in self.document_name else None return self.document_name.split('_')[6] if '_' in self.document_name else None def get_identificacor(self): if self.document_type != 'AC_COVE': return self.document_name.split('_')[2] if '_' in self.document_name else None return self.document_name.split('_')[3] if '_' in self.document_name else None def get_numero_documento(self): if self.document_type != 'AC_COVE' and self.document_type in ('PT', 'EDC', 'AC', 'COVE'): return self.document_name.split('_')[6] if '_' in self.document_name else None elif self.document_type in ('RM', 'PC'): return 0 return self.document_name.split('_')[7] if '_' in self.document_name else None def is_duplicated(self): if self.document_type != 'AC_COVE' and self.document_type != 'RM' and self.document_type != 'PC': return False if len(self.document_name.split('_')) <= 7 else True elif self.document_type == 'RM' or self.document_type == 'PC': return False if len(self.document_name.split('_')) <= 6 else True elif self.document_type == 'AC_COVE': return False if len(self.document_name.split('_')) <= 8 else True else: return False return self.document_name.split('_')[8] if '_' in self.document_name else None def get_pedimento_tipo(self): if 'PT' in self.document_name: return 'PT' elif 'AC' in self.document_name and not 'COVE' in self.document_name: return 'AC' elif 'PC' in self.document_name: return 'PC' elif 'EDC' in self.document_name: return 'EDC' elif 'AC_COVE' in self.document_name: return 'AC_COVE' elif 'COVE' in self.document_name and not 'AC_COVE' in self.document_name: return 'COVE' elif 'LOGISTICO' in self.document_name: return 'LOGISTICO' elif 'RM' in self.document_name: return 'RM' else: return None def get_pedimento(self): from api.customs.models import Pedimento return Pedimento.objects.filter(pedimento=self.pedimento).first() def document_exists(self, pedimento): from api.record.models import Document if self.document_type in ('PT', 'EDC', 'AC', 'COVE', 'AC_COVE'): doc_name = f"{self.fuente}_{self.document_type}_{self.aduana}_{self.patente}_{self.pedimento}_{self.numero_documento}.{self.extension}" elif self.document_type in ('PC', 'RM'): doc_name = f"{self.fuente}_{self.document_type}_{self.aduana}_{self.patente}_{self.pedimento}.{self.extension}" return Document.objects.filter(archivo=doc_name , pedimento=pedimento).exists() def __str__(self): return f"{self.pedimento}" def get_document_type(document_type): from api.record.models import DocumentType return DocumentType.objects.filter(pk=TIPOS_DE_DOCUMENTOS[document_type]).first() import concurrent.futures def process_document(document, path): from api.record.models import Document doc = DocumentFile(document) print(document) pedimento = doc.get_pedimento() if pedimento: document_exists = doc.document_exists(pedimento) if not document_exists and not doc.is_duplicated(): Document.objects.create( organizacion=pedimento.organizacion, pedimento=pedimento, archivo=document, document_type=get_document_type(doc.document_type), fuente=FUENTE, # Asumiendo que la fuente es siempre 1, ajusta según sea necesario extension=doc.extension, size=os.path.getsize(os.path.join(path, document)), ) print(f"El documento {doc.document_name} pertenece al pedimento {doc.pedimento} y se ha creado en la base de datos.") else: print(f"El documento {doc.document_name} pertenece al pedimento {doc.pedimento} y ya existe en la base de datos.") else: print(f"El documento {doc.document_name} pertenece al pedimento {doc.pedimento} y NO existe en la base de datos.") def main(): path = '/app/media/documents' documents = os.listdir(path) max_workers = os.cpu_count() or 1 with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor: executor.map(lambda doc: process_document(doc, path), documents) if __name__ == "__main__": main()