From 559baca38c2d711f87566836e6e5ac23f2876bac Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Thu, 28 Nov 2024 13:41:58 +0800 Subject: [PATCH] Fix files not being read properly in watcher --- .../config/management/commands/start_watcher.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docmanager_backend/config/management/commands/start_watcher.py b/docmanager_backend/config/management/commands/start_watcher.py index a6b30cf..2a8883f 100644 --- a/docmanager_backend/config/management/commands/start_watcher.py +++ b/docmanager_backend/config/management/commands/start_watcher.py @@ -10,6 +10,7 @@ from config.settings import MEDIA_ROOT from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler from documents.models import Document +from django.core.files import File import logging import time @@ -67,16 +68,19 @@ class PDFHandler(FileSystemEventHandler): metadata += text - document, created = Document.objects.get_or_create( + # Open the file for instance creation + DOCUMENT, created = Document.objects.get_or_create( name=filename, defaults={ 'number_pages': num_pages, 'ocr_metadata': metadata, 'document_type': document_type - } + }, ) if created: + DOCUMENT.file.save( + name=filename, content=File(open(file_path, 'rb'))) self.logger.info(f"Document '{filename}' created successfully with type '{ document_type}'.")