django_ocr_server/models.py
def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
        """
        This function save the instance of the model, or create it
        :param force_insert:
        :param force_update:
        :param using:
        :param update_fields:
        :return: None
        """
        if self.is_saved(raise_exception=False):
            return
        if not self.file_type:
            self.file_type = self.file.file.content_type
        OCRedFile.is_valid_file_type(file_type=self.file_type, raise_exception=True)
        content = self.file.file.read()  # read content of the 'file' field
        self.file.file.seek(0)  # return the reading pointer of the 'file' file to start position
        # calculate md5 of 'file' field if if does not exist
        if not self.md5:
            self.md5 = md5(content)
        OCRedFile.is_valid_ocr_md5(md5_value=self.md5, raise_exception=True)
        # extract of ocr a content of the 'file' field if 'text' does not exist
        if not self.text:
            print('OCRedFile->save start OCR ' + self.md5)
            ocr_started_datetime = datetime.now()
            if 'image' in self.file_type:
                pdf_content = ocr_img2pdf(content)
                self.text = pdf2text(pdf_content)
                if len(self.text):
                    # create ocred_pdf only for an image that contains a text
                    self.ocred_pdf_md5 = md5(pdf_content)
                    if getattr(settings, 'OCR_STORE_PDF', ocr_default_settings.STORE_PDF):
                        self.ocred_pdf.save(set_pdffile_name(self), BytesIO(pdf_content), False)
                    else:
                        self.ocred_pdf.name = set_pdffile_name(self)
                self.ocred = timezone.now()
            elif 'pdf' in self.file_type:
                info = pdf_info(content)
                self.pdf_num_pages = info['numPages']
                self.pdf_author = info['Author']
                if info['CreationDate']:
                    self.pdf_creation_date = info['CreationDate']
                self.pdf_creator = info['Creator']
                if info['ModDate']:
                    self.pdf_mod_date = info['ModDate']
                self.pdf_producer = info['Producer']
                self.pdf_title = info['Title']
                pdf_text = pdf2text(content)
                # check that loaded PDF file contains text
                if pdf_need_ocr(pdf_text):
                    print('OCRedFile PDF OCR processing via OCRmyPDF ' + self.md5)
                    filename = set_pdffile_name(self)
                    self.text = ocr_pdf(content, filename)
                    self.ocred = timezone.now()  # save datetime when uploaded PDF was ocred
                    if len(self.text):
                        # create ocred_pdf only for a pdf file that contains images with text
                        self.ocred_pdf.name = filename
                        self.ocred_pdf_md5 = md5(read_binary_file(filename))
                        if not getattr(settings, 'OCR_STORE_PDF', ocr_default_settings.STORE_PDF):
                            if os.path.isfile(filename):
                                os.remove(filename)
                    else:
                        # remove created by ocr_pdf(content, filename) pdf file
                        if os.path.isfile(filename):
                            os.remove(filename)
                else:
                    print('OCRedFile->save use text from loaded pdf ' + self.md5)
                    self.text = pdf_text
            ocr_finished_datetime=datetime.now()
            print(f"OCRedFile->save finished OCR '{str(ocr_finished_datetime-ocr_started_datetime)}' {self.md5}")
        if not connection.is_usable():
            try:
                connection.connect()
            except Exception as e:
                print(f"database reconnection exception {self.md5}")
        super(OCRedFile, self).save(force_insert=False, force_update=False, using=None, update_fields=None)
        if not getattr(settings, 'OCR_STORE_FILES', ocr_default_settings.STORE_FILES):
            os.remove(self.file.path)
        OCRedFile.Counters.num_created_instances += 1

        This function save the instance of the model, or create it
        :param force_insert:
        :param force_update:
        :param using:
        :param update_fields:
        :return: None
        
This function save the instance of the model, or create it
This function save the instance of the model or create it
def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
        if self.is_saved(raise_exception=False):
            return
        if not self.file_type:
            self.file_type = self.file.file.content_type
        OCRedFile.is_valid_file_type(file_type=self.file_type, raise_exception=True)
        content = self.file.file.read()  
        self.file.file.seek(0)  
        if not self.md5:
            self.md5 = md5(content)
        OCRedFile.is_valid_ocr_md5(md5_value=self.md5, raise_exception=True)
        if not self.text:
            print('OCRedFile->save start OCR ' + self.md5)
            ocr_started_datetime = datetime.now()
            if 'image' in self.file_type:
                pdf_content = ocr_img2pdf(content)
                self.text = pdf2text(pdf_content)
                if len(self.text):
                    self.ocred_pdf_md5 = md5(pdf_content)
                    if getattr(settings, 'OCR_STORE_PDF', ocr_default_settings.STORE_PDF):
                        self.ocred_pdf.save(set_pdffile_name(self), BytesIO(pdf_content), False)
                    else:
                        self.ocred_pdf.name = set_pdffile_name(self)
                self.ocred = timezone.now()
            elif 'pdf' in self.file_type:
                info = pdf_info(content)
                self.pdf_num_pages = info['numPages']
                self.pdf_author = info['Author']
                if info['CreationDate']:
                    self.pdf_creation_date = info['CreationDate']
                self.pdf_creator = info['Creator']
                if info['ModDate']:
                    self.pdf_mod_date = info['ModDate']
                self.pdf_producer = info['Producer']
                self.pdf_title = info['Title']
                pdf_text = pdf2text(content)
                if pdf_need_ocr(pdf_text):
                    print('OCRedFile PDF OCR processing via OCRmyPDF ' + self.md5)
                    filename = set_pdffile_name(self)
                    self.text = ocr_pdf(content, filename)
                    self.ocred = timezone.now()  
                    if len(self.text):
                        self.ocred_pdf.name = filename
                        self.ocred_pdf_md5 = md5(read_binary_file(filename))
                        if not getattr(settings, 'OCR_STORE_PDF', ocr_default_settings.STORE_PDF):
                            if os.path.isfile(filename):
                                os.remove(filename)
                    else:
                        if os.path.isfile(filename):
                            os.remove(filename)
                else:
                    print('OCRedFile->save use text from loaded pdf ' + self.md5)
                    self.text = pdf_text
            ocr_finished_datetime=datetime.now()
            print(f"OCRedFile->save finished OCR '{str(ocr_finished_datetime-ocr_started_datetime)}' {self.md5}")
        if not connection.is_usable():
            try:
                connection.connect()
            except Exception as e:
                print(f"database reconnection exception {self.md5}")
        super(OCRedFile, self).save(force_insert=False, force_update=False, using=None, update_fields=None)
        if not getattr(settings, 'OCR_STORE_FILES', ocr_default_settings.STORE_FILES):
            os.remove(self.file.path)
        OCRedFile.Counters.num_created_instances += 1
def save ( self , force_insert = False , force_update = False , using = None , update_fields = None ) : if self . is_saved ( raise_exception = False ) : return if not self . file_type : self . file_type = self . file . file . content_type OCRedFile . is_valid_file_type ( file_type = self . file_type , raise_exception = True ) content = self . file . file . read ( ) self . file . file . seek ( 0 ) if not self . md5 : self . md5 = md5 ( content ) OCRedFile . is_valid_ocr_md5 ( md5_value = self . md5 , raise_exception = True ) if not self . text : print ( 'OCRedFile->save start OCR ' + self . md5 ) ocr_started_datetime = datetime . now ( ) if 'image' in self . file_type : pdf_content = ocr_img2pdf ( content ) self . text = pdf2text ( pdf_content ) if len ( self . text ) : self . ocred_pdf_md5 = md5 ( pdf_content ) if getattr ( settings , 'OCR_STORE_PDF' , ocr_default_settings . STORE_PDF ) : self . ocred_pdf . save ( set_pdffile_name ( self ) , BytesIO ( pdf_content ) , False ) else : self . ocred_pdf . name = set_pdffile_name ( self ) self . ocred = timezone . now ( ) elif 'pdf' in self . file_type : info = pdf_info ( content ) self . pdf_num_pages = info [ 'numPages' ] self . pdf_author = info [ 'Author' ] if info [ 'CreationDate' ] : self . pdf_creation_date = info [ 'CreationDate' ] self . pdf_creator = info [ 'Creator' ] if info [ 'ModDate' ] : self . pdf_mod_date = info [ 'ModDate' ] self . pdf_producer = info [ 'Producer' ] self . pdf_title = info [ 'Title' ] pdf_text = pdf2text ( content ) if pdf_need_ocr ( pdf_text ) : print ( 'OCRedFile PDF OCR processing via OCRmyPDF ' + self . md5 ) filename = set_pdffile_name ( self ) self . text = ocr_pdf ( content , filename ) self . ocred = timezone . now ( ) if len ( self . text ) : self . ocred_pdf . name = filename self . ocred_pdf_md5 = md5 ( read_binary_file ( filename ) ) if not getattr ( settings , 'OCR_STORE_PDF' , ocr_default_settings . STORE_PDF ) : if os . path . isfile ( filename ) : os . remove ( filename ) else : if os . path . isfile ( filename ) : os . remove ( filename ) else : print ( 'OCRedFile->save use text from loaded pdf ' + self . md5 ) self . text = pdf_text ocr_finished_datetime = datetime . now ( ) print ( f"OCRedFile->save finished OCR '{str(ocr_finished_datetime-ocr_started_datetime)}' {self.md5}" ) if not connection . is_usable ( ) : try : connection . connect ( ) except Exception as e : print ( f"database reconnection exception {self.md5}" ) super ( OCRedFile , self ) . save ( force_insert = False , force_update = False , using = None , update_fields = None ) if not getattr ( settings , 'OCR_STORE_FILES' , ocr_default_settings . STORE_FILES ) : os . remove ( self . file . path ) OCRedFile . Counters . num_created_instances += 1
This function save the instance of the model, or create it
This function save the instance of the model or create it
"""
        This function save the instance of the model, or create it
        :param force_insert:
        :param force_update:
        :param using:
        :param update_fields:
        :return: None
        """ # read content of the 'file' field # return the reading pointer of the 'file' file to start position # calculate md5 of 'file' field if if does not exist # extract of ocr a content of the 'file' field if 'text' does not exist # create ocred_pdf only for an image that contains a text # check that loaded PDF file contains text # save datetime when uploaded PDF was ocred # create ocred_pdf only for a pdf file that contains images with text # remove created by ocr_pdf(content, filename) pdf file
None
self
force_insert
None
force_update
None
using
None
update_fields
None