How to call bussines logic from seperate class in view in Django?

I am using django framework. And I have some functionality for uploading files. And I have a textarea where the extracted content of the uploaded file will be shown.

So for example if you upload a pdf file within a image that contains text. In the textarea the text will be shown.

But the logic for extracting the text from the image of course doesn't belong too the method in the view.

So I made a seperate class for it, with name: extractingTextFromFile.py

that looks like this:

import io
from tracemalloc import start
from PIL import Image
import pytesseract
from wand.image import Image as wi
import re


class ExtractingTextFromFile:

    def extractingtextfromimage():      
        text_factuur_verdi = []

        #image = pdfFile.convert('jpeg')
        imageBlobs = []

        for img in image.sequence:
                imgPage = wi(image=img)
                imageBlobs.append(imgPage.make_blob('jpeg'))

        for imgBlob in imageBlobs:
            image = Image.open(io.BytesIO(imgBlob))
            text = pytesseract.image_to_string(image, lang='eng')
            text_factuur_verdi.append(text)

           

and I have the views.py:

from django.shortcuts import render
from django.views import View
from django.http import HttpResponseRedirect
from .forms import ProfileForm
from .models import UploadFile
import extractingTextFromFile as textFromFile
from wand.image import Image as wi
from PIL import Image
import pytesseract
from django.conf import settings
import io
import os


class ReadingFile(View):
    def get(self, request):
        form = ProfileForm()
        return render(request, "main/create_profile.html", {
            "form": form
        })

    def extractingtextfromimage():

        pass

    def post(self, request):
        submitted_form = ProfileForm(request.POST, request.FILES)
        content = ''

        if submitted_form.is_valid():
            uploadfile = UploadFile(image=request.FILES["upload_file"])

            name_of_file = str(request.FILES['upload_file'])
            uploadfile.save()
            print('path of the file is:::', uploadfile.image.name)
            #path_to_file = uploadfile.upload_file.path
            #request.session['text'] = name_of_file(filetosave.file.path)

            with open(os.path.join(settings.MEDIA_ROOT,
                                   f"{uploadfile.image}"), 'r') as f:

                print("Now its type is ", type(name_of_file))
                print(uploadfile.image.path)

                # reading PDF file
                if name_of_file.endswith('.pdf'):
                    textFromFile.extractingtextfromimage()

                    content = text_factuur_verdi
                    print(text_factuur_verdi)

                # ENDING Reading pdf file

                else:
                    content = f.read()
                    print(content)

            return render(request, "main/create_profile.html", {
                'form': ProfileForm(),
                "content": content
            })

        return render(request, "main/create_profile.html", {
            "form": submitted_form,
        })


so on this line:

  textFromFile.extractingtextfromimage()

I try to call the seperate method.

But of course there is missing some properties, like: text_factuur_verdi.

So my question is: how to call this seperate method from the view?

Thank you

So,this works in the views.py:

  # reading PDF file
                if name_of_file.endswith('.pdf'):
                    pdfFile = wi(filename= uploadfile.image.path , resolution=300)
                    text_factuur_verdi = []

                    image = pdfFile.convert('jpeg')
                    imageBlobs = []

                    for img in image.sequence:
                        imgPage = wi(image=img)
                        imageBlobs.append(imgPage.make_blob('jpeg'))

                    for imgBlob in imageBlobs:
                        image = Image.open(io.BytesIO(imgBlob))
                        text = pytesseract.image_to_string(image, lang='eng')
                        text_factuur_verdi.append(text)

                    content = text_factuur_verdi
                    print(text_factuur_verdi)
                    
                # ENDING Reading pdf file

But of course this has to be in a seperate file.

And then from the view you can inject the method.

Back to Top