WebashalarForML commited on
Commit
49341e5
·
verified ·
1 Parent(s): f0525d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -20
app.py CHANGED
@@ -88,32 +88,50 @@ def text_preview():
88
  return redirect(url_for('index'))
89
 
90
  # API for uploading Resume files
91
- @app.route('/upload',methods=['GET', 'POST'])
92
  def upload_file():
93
  try:
94
- if 'file' not in request.files:
95
- flash('No file part', 'error')
96
- return redirect(request.url)
97
-
98
- file = request.files['file']
99
- if file.filename == '':
100
- flash('No selected file', 'error')
101
- return redirect(request.url)
102
-
103
- if file and allowed_file(file.filename):
104
- filename = secure_filename(file.filename)
105
- file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
106
- file.save(file_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
- # Handle text extraction for non-JSON files
109
- if not filename.lower().endswith('.json'):
110
- return process_other_files(file_path, filename)
111
 
112
- flash('File type not allowed', 'error')
113
  except Exception as e:
 
114
  flash(f"Error: {str(e)}", 'error')
115
-
116
- return redirect(request.url)
117
 
118
  # Process non-JSON files, extract text and save to 'resume_text.txt'
119
  def process_other_files(file_path, filename):
 
88
  return redirect(url_for('index'))
89
 
90
  # API for uploading Resume files
91
+ @app.route('/upload', methods=['GET', 'POST'])
92
  def upload_file():
93
  try:
94
+ # Check if the request is a POST method, meaning a file is being uploaded
95
+ if request.method == 'POST':
96
+ # Check if the 'file' part is in the request (no file selected case)
97
+ if 'file' not in request.files:
98
+ # Flash an error message and render the same page without redirecting (prevents redirect loop)
99
+ flash('No file part', 'error')
100
+ return render_template('upload.html') # Render the upload page directly
101
+
102
+ file = request.files['file']
103
+ # Check if a file was selected by the user
104
+ if file.filename == '':
105
+ # Flash an error message and render the same page without redirecting (prevents redirect loop)
106
+ flash('No selected file', 'error')
107
+ return render_template('upload.html') # Render the upload page directly
108
+
109
+ # Check if the uploaded file has an allowed extension
110
+ if file and allowed_file(file.filename):
111
+ filename = secure_filename(file.filename)
112
+ file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
113
+ file.save(file_path)
114
+
115
+ # Handle non-JSON files and process accordingly
116
+ if not filename.lower().endswith('.json'):
117
+ return process_other_files(file_path, filename) # Return after processing non-JSON files
118
+ else:
119
+ # Flash success message for JSON files and render the upload page
120
+ flash('JSON file uploaded successfully', 'success')
121
+ return render_template('upload.html') # Return the same page with success
122
+
123
+ # Flash an error if the file type is not allowed
124
+ flash('File type not allowed', 'error')
125
+ return render_template('upload.html') # Return the same page with the error message
126
 
127
+ # Render the upload page for GET requests (when the page is loaded initially)
128
+ return render_template('upload.html')
 
129
 
 
130
  except Exception as e:
131
+ # Handle any exceptions that occur, flash the error message, and render the same page
132
  flash(f"Error: {str(e)}", 'error')
133
+ return render_template('upload.html') # Return the same page with the error
134
+
135
 
136
  # Process non-JSON files, extract text and save to 'resume_text.txt'
137
  def process_other_files(file_path, filename):