Click here to Skip to main content
15,878,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The overall concept is user uploads the resume and selects either auto-corrections, show corrections, and no corrections. If the user selects auto-corrections it will automatically correct the misspelled words but when the user selects show corrections, will show the user list of misspelled words and allow the user to select specific words to correct with the predefined words or with their own words.



Python
# called from HTML form action when user selected show corrections option and uploads the resume .docx file.
def uploading_resume(request):
user = request.user
if user.is_authenticated:
    if request.method == 'POST':
        if corrections == 'show':
             file = request.FILES['file']
             global u_file
             u_file = request.FILES['file']
             resume_text = show_corrections(request=request, file=file)

# gets the list of incorrect words and returns the list.
def show_corrections(request, file):
  user = request.user
  resume_text = []
  document = Document(file.file)
  parser = GingerIt()
  text = ''
  for line in document.paragraphs:
      text += line.text
      matches = parser.parse(line.text)
      for obj in matches['corrections']:
          word_obj = {'word': obj['text'], 'correct_word': obj['correct']}
          resume_text.append(word_obj)
 return resume_text

# when user selected specific words from the above list.
def correct_selected(request):
  user = request.user
   if user.is_authenticated:
      if request.method == 'POST':
         if request.POST['selected_words'] != '':
            selected_words = demjson.decode(request.POST['selected_words'])
            file_name = request.POST['selected_file_name']
            global u_file
            file = u_file
            file = corrections_fn(request=request, file=file, selected_words=selected_words, corrections="show")
            save_resume(request=request, file_name=file_name, file=file)
return redirect('upload')

# replaces the wrong words with the defined words.
def corrections_fn(request, file, selected_words, corrections):
  parser = GingerIt()
  user = request.user
  document = Document(file.file)
  text = ''
  for line in document.paragraphs:
      matches = parser.parse(line.text)
      for obj in matches['corrections']:
          misspelled_word = obj['text']
          if misspelled_word in selected_words:
             correction = selected_words[misspelled_word]
             line.text = line.text.replace(str(misspelled_word), str(correction))
          else:
             if corrections == 'auto':
                line.text = line.text.replace(str(obj['text']), str(obj['correct']))
   document.save(file.file)
return file


What I have tried:

I have tried setting the file to the session variable and when trying to accessing the file it is showing "InMemoryUploadedFile is not JSON serializable" Error and when I tried to use the global variable it is showing "I/O operation on closed file." this error. can anyone suggest to me a better way of storing the file temporarily in a variable? so that I can access the file in another view function.

Thank You
Posted
Updated 1-Jun-21 5:23am

1 solution

You cannot store the file itself in the session.

Instead, generate a random name[^] for the file. Save the file[^] in a folder on your server which is not exposed to the public, using that random name. Store the random name in the session, and use it to read the file each time you want to process it.

You'll probably want to add some code to clean up old files after they haven't been used for a while. Otherwise you risk having your server's disk fill up with old files.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900