1
1
"""
2
2
Flask Application for Resume Management
3
3
"""
4
+
4
5
import os
5
6
import logging
6
7
10
11
from werkzeug .utils import secure_filename
11
12
from flask import Flask , jsonify , request , send_from_directory
12
13
from models import Experience , Education , Skill , UserInformation
13
- from helpers import validate_fields , validate_phone_number , load_data , save_data , generate_id
14
+ from helpers import (
15
+ validate_fields ,
16
+ validate_phone_number ,
17
+ load_data ,
18
+ save_data ,
19
+ generate_id ,
20
+ )
14
21
15
22
16
23
spell = SpellChecker ()
26
33
CORS (app )
27
34
app .config ["UPLOAD_FOLDER" ] = UPLOAD_FOLDER
28
35
29
- data = load_data ('data/data.json' )
36
+ data = load_data ("data/data.json" )
37
+
30
38
31
39
def reset_data ():
32
40
"""
@@ -61,7 +69,7 @@ def reset_data():
61
69
]
62
70
63
71
64
- #reset_data()
72
+ # reset_data()
65
73
66
74
67
75
def allowed_file (filename ):
@@ -157,7 +165,7 @@ def experience():
157
165
logo_filename = filename
158
166
159
167
# Create new experience
160
- new_id = generate_id (data , ' experience' )
168
+ new_id = generate_id (data , " experience" )
161
169
162
170
# Create new experience
163
171
new_experience = Experience (
@@ -170,9 +178,12 @@ def experience():
170
178
new_id ,
171
179
)
172
180
data ["experience" ].append (new_experience )
173
- save_data (' data/data.json' , data )
181
+ save_data (" data/data.json" , data )
174
182
new_experience_index = len (data ["experience" ]) - 1
175
- return jsonify ({"message" : "New experience created" , "id" : new_experience_index }), 201
183
+ return (
184
+ jsonify ({"message" : "New experience created" , "id" : new_experience_index }),
185
+ 201 ,
186
+ )
176
187
177
188
return 400
178
189
@@ -191,15 +202,20 @@ def delete_experience(index):
191
202
@app .route ("/resume/education" , methods = ["GET" , "POST" ])
192
203
def education ():
193
204
"""
194
- Handle education requests for GET and POST methods
205
+ Handle education requests for GET and POST methods.
206
+ POST: Add a new education entry with optional file upload.
195
207
"""
196
208
if request .method == "GET" :
197
209
return jsonify ([edu .__dict__ for edu in data ["education" ]]), 200
198
210
199
211
if request .method == "POST" :
200
- request_body = request .get_json ()
212
+ if request .content_type .startswith ('multipart/form-data' ):
213
+ request_body = request .form
214
+ else :
215
+ request_body = request .get_json ()
216
+
201
217
if not request_body :
202
- return jsonify ({"error" : "Request must be JSON" }), 400
218
+ return jsonify ({"error" : "Request must be JSON or include form data " }), 400
203
219
204
220
required_fields = {
205
221
"course" : str ,
@@ -219,21 +235,28 @@ def education():
219
235
"invalid_fields" : invalid_fields
220
236
}), 400
221
237
238
+ logo_filename = DEFAULT_LOGO
239
+ if "logo" in request .files :
240
+ logo_file = request .files ["logo" ]
241
+ if logo_file and allowed_file (logo_file .filename ):
242
+ filename = secure_filename (logo_file .filename )
243
+ logo_file .save (os .path .join (app .config ["UPLOAD_FOLDER" ], filename ))
244
+ logo_filename = filename
245
+
222
246
new_id = generate_id (data , 'education' )
223
247
224
- # Create new education entry
225
248
new_education = Education (
226
249
request_body ["course" ],
227
250
request_body ["school" ],
228
251
request_body ["start_date" ],
229
252
request_body ["end_date" ],
230
253
request_body ["grade" ],
231
- DEFAULT_LOGO ,
254
+ logo_filename ,
232
255
new_id
233
256
)
234
257
data ["education" ].append (new_education )
235
258
save_data ('data/data.json' , data )
236
- new_education_index = new_id - 1
259
+ new_education_index = len ( data [ "education" ]) - 1
237
260
return jsonify ({"message" : "New education created" , "id" : new_education_index }), 201
238
261
239
262
return 400
@@ -270,6 +293,42 @@ def delete_education(index):
270
293
return jsonify ({"error" : "Education entry not found" }), 404
271
294
272
295
296
+ @app .route ("/resume/education/<int:index>" , methods = ["PUT" ])
297
+ def update_education (index ):
298
+ """
299
+ Update education entry by index.
300
+ Supports updating both text fields and file upload for logo.
301
+ """
302
+ if 0 <= index < len (data ["education" ]):
303
+ if request .content_type .startswith ('multipart/form-data' ):
304
+ request_body = request .form
305
+ else :
306
+ request_body = request .get_json ()
307
+
308
+ if not request_body :
309
+ return jsonify ({"error" : "Request must be JSON or include form data" }), 400
310
+
311
+ edu = data ["education" ][index ]
312
+
313
+ edu .course = request_body .get ("course" , edu .course )
314
+ edu .school = request_body .get ("school" , edu .school )
315
+ edu .start_date = request_body .get ("start_date" , edu .start_date )
316
+ edu .end_date = request_body .get ("end_date" , edu .end_date )
317
+ edu .grade = request_body .get ("grade" , edu .grade )
318
+
319
+ if "logo" in request .files :
320
+ logo_file = request .files ["logo" ]
321
+ if logo_file and allowed_file (logo_file .filename ):
322
+ filename = secure_filename (logo_file .filename )
323
+ logo_file .save (os .path .join (app .config ["UPLOAD_FOLDER" ], filename ))
324
+ edu .logo = filename
325
+
326
+ save_data ('data/data.json' , data )
327
+ return jsonify ({"message" : "Education entry updated" , "id" : index }), 200
328
+
329
+ return jsonify ({"error" : "Education entry not found" }), 404
330
+
331
+
273
332
@app .route ("/resume/skill" , methods = ["GET" , "POST" ])
274
333
def skill ():
275
334
"""
@@ -319,8 +378,11 @@ def skill():
319
378
)
320
379
data ["skill" ].append (new_skill )
321
380
322
- save_data ('data/data.json' , data )
323
- return jsonify ({"message" : "New skill created" , "id" : len (data ["skill" ]) - 1 }), 201
381
+ save_data ("data/data.json" , data )
382
+ return (
383
+ jsonify ({"message" : "New skill created" , "id" : len (data ["skill" ]) - 1 }),
384
+ 201 ,
385
+ )
324
386
325
387
return 400
326
388
@@ -356,7 +418,7 @@ def delete_skill(index):
356
418
if 0 <= index < len (data ["skill" ]):
357
419
removed_skill = data ["skill" ].pop (index )
358
420
logging .info ("Skill deleted: %s" , removed_skill .name )
359
- save_data (' data/data.json' , data )
421
+ save_data (" data/data.json" , data )
360
422
return jsonify ({"message" : "Skill successfully deleted" }), 200
361
423
return jsonify ({"error" : "Skill not found" }), 404
362
424
0 commit comments