3
3
'''
4
4
5
5
from flask import Flask , jsonify , request
6
-
7
- from models import Education , Experience , Skill
6
+ from models import Experience , Education , Skill
7
+ from gpt_connection import get_improvement
8
+ from validation import validate_experience , validate_education , validate_skill
9
+ from spell_check import spell_check
8
10
9
11
app = Flask (__name__ )
10
12
20
22
)
21
23
],
22
24
"education" : [
23
- Education (
24
- "Computer Science" ,
25
- "University of Tech" ,
26
- "September 2019" ,
27
- "July 2022" ,
28
- "80%" ,
29
- "example-logo.png" ,
30
- )
25
+ Education ("Computer Science" ,
26
+ "University of Tech" ,
27
+ "September 2019" ,
28
+ "July 2022" ,
29
+ "80%" ,
30
+ "example-logo.png" ,
31
+ "I was head of the debate team at university" )
31
32
],
32
33
"skill" : [Skill ("Python" , "1-2 Years" , "example-logo.png" )],
33
34
}
@@ -42,7 +43,7 @@ def hello_world():
42
43
43
44
44
45
@app .route ("/resume/experience" , methods = ["GET" , "POST" ])
45
- @app .route ("/resume/experience/<int:index>" , methods = ["GET" ])
46
+ @app .route ("/resume/experience/<int:index>" , methods = ["GET" , "DELETE" ])
46
47
def experience (index = None ):
47
48
'''
48
49
Handle experience requests
@@ -57,34 +58,31 @@ def experience(index=None):
57
58
return jsonify ({"error" : "Experience not found" }), 404
58
59
return jsonify (data ["experience" ]), 200
59
60
60
- if request .method == "POST" :
61
+ if request .method == 'POST' :
62
+ json_data = request .json
61
63
try :
62
- new_experience = request .get_json ()
63
- if not new_experience :
64
- return jsonify ({"error" : "No data provided" }), 400
65
- # validate required fields
66
- required_fields = [
67
- "title" ,
68
- "company" ,
69
- "start_date" ,
70
- "end_date" ,
71
- "description" ,
72
- "logo" ,
73
- ]
74
- if not all (field in new_experience for field in required_fields ):
75
- return jsonify ({"error" : "Missing required fields" }), 400
76
-
77
- experience_obj = Experience (** new_experience )
78
- data ["experience" ].append (experience_obj )
64
+ validated_data = validate_experience (json_data )
65
+
66
+ data ["experience" ].append (validated_data )
79
67
return jsonify ({"id" : len (data ["experience" ]) - 1 }), 201
80
68
except TypeError as e :
81
69
return jsonify ({"error" : f"Invalid data format: { str (e )} " }), 400
82
70
except Exception as e :
83
71
return jsonify ({"error" : f"Internal error: { str (e )} " }), 500
84
72
73
+ if request .method == "DELETE" :
74
+ try :
75
+ if index is None or index < 0 or index >= len (data ["experience" ]):
76
+ return jsonify ({"message" : "Resource doesn't exist" }), 404
77
+ else :
78
+ data ['experience' ].pop (index )
79
+ return jsonify ({"message" : "Experience Successfully Deleted" }), 200
80
+ except Exception as e :
81
+ return jsonify ({"error" : f"An error occurred: { str (e )} " }), 500
85
82
86
83
return jsonify ({"error" : "Method not allowed" }), 405
87
84
85
+
88
86
@app .route ('/resume/experience/<int:exp_id>' , methods = ['DELETE' ])
89
87
def delete_experience (exp_id ):
90
88
try :
@@ -97,19 +95,64 @@ def delete_experience(exp_id):
97
95
return jsonify ({"error" : f"An error occurred: { str (e )} " }), 500
98
96
99
97
98
+
99
+ @app .route ('/resume/spell_check' , methods = ['POST' ])
100
+ def spell_check ():
101
+ json_data = request .json
102
+ if json_data .get ('description' ) and isinstance (json_data .get ('description' ), str ):
103
+ json_data ['description' ] = spell_check (json_data ['description' ])
104
+ return jsonify ({
105
+ "before" : request .json ,
106
+ "after" : json_data
107
+ })
108
+
109
+
100
110
@app .route ("/resume/education" , methods = ["GET" , "POST" ])
101
- def education ():
111
+ @app .route ("/resume/education/<int:edu_id>" , methods = ["GET" , "DELETE" ])
112
+ def education (edu_id = None ):
102
113
'''
103
114
Handles education requests
115
+ GET: Returns all educations (unimplemented here)
116
+ POST: Creates a new education (unimplemented here)
117
+ DELETE: Deletes an education by index
104
118
'''
105
119
if request .method == "GET" :
106
120
return jsonify ({})
107
121
108
122
if request .method == "POST" :
109
123
return jsonify ({})
110
124
125
+ if request .method == "DELETE" :
126
+ try :
127
+ if edu_id is None or edu_id < 0 or edu_id >= len (data ["education" ]):
128
+ return jsonify ({"message" : "Resource doesn't exist" }), 404
129
+ else :
130
+ del data ["education" ][edu_id ]
131
+ return jsonify ({"message" : "Education Successfully Deleted" }), 200
132
+ except Exception as e :
133
+ return jsonify ({"error" : f"An error occurred: { str (e )} " }), 500
134
+
111
135
return jsonify ({})
112
136
137
+ @app .route ('/resume/reword_description' , methods = ['GET' ])
138
+ def reword_description ():
139
+ '''
140
+ Rewords the description using GPT
141
+ '''
142
+ model = None
143
+ try :
144
+ model = Experience (** request .json )
145
+ except :
146
+ model = Education (** request .json )
147
+
148
+ if model is None :
149
+ return jsonify ({"error" : "Invalid request" }), 400
150
+
151
+ response = get_improvement (model )
152
+ if response is None :
153
+ return jsonify ({"error" : "Failed to get improvement" }), 500
154
+
155
+ return jsonify ({"response" : response })
113
156
114
157
@app .route ("/resume/skill" , methods = ["GET" , "POST" ])
115
158
@app .route ('/resume/skill/<int:skill_id>' , methods = ['DELETE' ])
@@ -123,20 +166,14 @@ def skill(skill_id = None):
123
166
if request .method == 'POST' :
124
167
json_data = request .json
125
168
try :
126
- # extract the data from the request
127
- name = json_data ["name" ]
128
- proficiency = json_data ["proficiency" ]
129
- logo = json_data ["logo" ]
169
+ validated_data = validate_skill (json_data )
130
170
131
- new_skill = Skill (name , proficiency , logo )
132
-
133
- data ["skill" ].append (new_skill )
171
+ data ["skill" ].append (validated_data )
134
172
135
173
# return ID of new skill
136
174
return jsonify (
137
175
{"id" : len (data ["skill" ]) - 1 }
138
176
), 201
139
-
140
177
except KeyError :
141
178
return jsonify ({"error" : "Invalid request" }), 400
142
179
@@ -149,7 +186,8 @@ def skill(skill_id = None):
149
186
else :
150
187
del data ['skill' ][skill_id ]
151
188
return jsonify ({"message" : "Skill Successfully Deleted" }), 200
152
-
189
+ except (ValueError , TypeError , KeyError ) as e :
190
+ return jsonify ({"error" : f"Invalid request: { str (e )} " }), 400
153
191
except Exception as e :
154
192
return jsonify ({"error" : f"An error occurred: { str (e )} " }), 500
155
193
0 commit comments