|
1 |
| -''' |
| 1 | +""" |
2 | 2 | Flask Application
|
3 |
| -''' |
| 3 | +""" |
| 4 | + |
4 | 5 | from flask import Flask, jsonify, request
|
5 | 6 | from models import Experience, Education, Skill, User
|
6 |
| -from utils import check_phone_number, correct_spelling |
| 7 | +from utils import get_suggestion, check_phone_number, correct_spelling |
| 8 | + |
| 9 | + |
7 | 10 | app = Flask(__name__)
|
8 | 11 |
|
9 | 12 | data = {
|
10 |
| - "user": [ |
11 |
| - User("Jackie Stewart", |
12 |
| - "+4478322678", |
13 |
| - |
14 |
| - ], |
| 13 | + "user": [ User( "Jackie Stewart", "+4478322678", "[email protected]")], |
15 | 14 | "experience": [
|
16 |
| - Experience("Software Developer", |
17 |
| - "A Cool Company", |
18 |
| - "October 2022", |
19 |
| - "Present", |
20 |
| - "Writing Python Code", |
21 |
| - "example-logo.png"), |
22 |
| - Experience("Intern", |
23 |
| - "A Nice Company", |
24 |
| - "October 2021", |
25 |
| - "December 2021", |
26 |
| - "Writing Scripts", |
27 |
| - "example-logo.png") |
| 15 | + Experience( |
| 16 | + "Software Developer", |
| 17 | + "A Cool Company", |
| 18 | + "October 2022", |
| 19 | + "Present", |
| 20 | + "Writing Python Code", |
| 21 | + "example-logo.png", |
| 22 | + ), |
| 23 | + Experience( |
| 24 | + "Intern", |
| 25 | + "A Nice Company", |
| 26 | + "October 2021", |
| 27 | + "December 2021", |
| 28 | + "Writing Scripts", |
| 29 | + "example-logo.png", |
| 30 | + ), |
28 | 31 | ],
|
29 | 32 | "education": [
|
30 |
| - Education("Computer Science", |
31 |
| - "University of Tech", |
32 |
| - "September 2019", |
33 |
| - "July 2022", |
34 |
| - "80%", |
35 |
| - "example-logo.png") |
| 33 | + Education( |
| 34 | + "Computer Science", |
| 35 | + "University of Tech", |
| 36 | + "September 2019", |
| 37 | + "July 2022", |
| 38 | + "80%", |
| 39 | + "example-logo.png", |
| 40 | + ) |
36 | 41 | ],
|
37 |
| - "skill": [ |
38 |
| - Skill("Python", |
39 |
| - "1-2 Years", |
40 |
| - "example-logo.png") |
41 |
| - ] |
| 42 | + "skill": [Skill("Python", "1-2 Years", "example-logo.png")], |
42 | 43 | }
|
43 | 44 |
|
44 | 45 |
|
45 |
| -@app.route('/test') |
| 46 | +@app.route("/test") |
46 | 47 | def hello_world():
|
47 |
| - ''' |
| 48 | + """ |
48 | 49 | Returns a JSON test message
|
49 |
| - ''' |
| 50 | + """ |
50 | 51 | return jsonify({"message": "Hello, World!"})
|
51 | 52 |
|
52 |
| -@app.route('/resume/user', methods=['GET', 'POST', 'PUT']) |
| 53 | + |
| 54 | +@app.route("/resume/user", methods=["GET", "POST", "PUT"]) |
53 | 55 | def user():
|
54 |
| - ''' |
| 56 | + """ |
55 | 57 | Handles User information
|
56 |
| - ''' |
| 58 | + """ |
| 59 | + |
57 | 60 | # defining sub function to reduce number of returns
|
58 | 61 | def get_users():
|
59 |
| - return jsonify([user.__dict__ for user in data['user']]), 200 |
| 62 | + return jsonify([user.__dict__ for user in data["user"]]), 200 |
60 | 63 |
|
61 | 64 | def add_user(body):
|
62 | 65 | # retrieve user's information.
|
63 |
| - name = body['name'] |
64 |
| - phone_number = body['phone_number'] |
65 |
| - email = body['email_address'] |
| 66 | + name = body["name"] |
| 67 | + phone_number = body["phone_number"] |
| 68 | + email = body["email_address"] |
66 | 69 | # store the new user information.
|
67 | 70 | if not check_phone_number(phone_number):
|
68 | 71 | return jsonify({"error": "Incorrect phone number !"}), 400
|
69 | 72 | new_user = User(name, phone_number, email)
|
70 |
| - data['user'].append(new_user) |
| 73 | + data["user"].append(new_user) |
71 | 74 | return jsonify(new_user.__dict__), 201
|
72 | 75 |
|
73 | 76 | # edit the user information.
|
74 | 77 | def edit_user(body):
|
75 |
| - name = body['name'] |
76 |
| - phone_number = body['phone_number'] |
77 |
| - email = body['email_address'] |
78 |
| - for i, user_ in enumerate(data['user']): |
| 78 | + name = body["name"] |
| 79 | + phone_number = body["phone_number"] |
| 80 | + email = body["email_address"] |
| 81 | + for i, user_ in enumerate(data["user"]): |
79 | 82 | if user_.email_address == email:
|
80 | 83 | if not check_phone_number(phone_number):
|
81 | 84 | return jsonify({"error": "Incorrect phone number !"}), 400
|
82 |
| - data['user'][i] = User(name, phone_number, email) |
83 |
| - return jsonify(data['user'][i].__dict__), 200 |
| 85 | + data["user"][i] = User(name, phone_number, email) |
| 86 | + return jsonify(data["user"][i].__dict__), 200 |
84 | 87 | return jsonify({"error": "User not found !"}), 404
|
85 | 88 |
|
86 |
| - if request.method == 'GET': |
| 89 | + if request.method == "GET": |
87 | 90 | return get_users()
|
88 |
| - if request.method == 'POST': |
| 91 | + if request.method == "POST": |
89 | 92 | body = request.get_json()
|
90 | 93 | return add_user(body)
|
91 |
| - if request.method == 'PUT': |
| 94 | + if request.method == "PUT": |
92 | 95 | body = request.get_json()
|
93 | 96 | return edit_user(body)
|
94 | 97 | return jsonify({"error": "Unsupported request method !"}), 405
|
95 | 98 |
|
96 |
| -@app.route('/resume/experience', methods=['GET', 'POST']) |
| 99 | + |
| 100 | +@app.route("/resume/experience", methods=["GET", "POST"]) |
97 | 101 | def experience():
|
98 |
| - ''' |
| 102 | + """ |
99 | 103 | Handle experience requests
|
100 |
| - ''' |
101 |
| - if request.method == 'GET': |
102 |
| - return jsonify({"experience": [exp.__dict__ for exp in data["experience"]]}) |
| 104 | + """ |
103 | 105 |
|
104 |
| - if request.method == 'POST': |
| 106 | + if request.method == "GET": |
| 107 | + return jsonify( |
| 108 | + {"experience": [exp.__dict__ for exp in data["experience"]]}) |
| 109 | + |
| 110 | + if request.method == "POST": |
105 | 111 | new_experience = request.json
|
106 | 112 | experience_instance = Experience(
|
107 | 113 | new_experience["title"],
|
108 | 114 | new_experience["company"],
|
109 | 115 | new_experience["start_date"],
|
110 | 116 | new_experience["end_date"],
|
111 | 117 | new_experience["description"],
|
112 |
| - new_experience["logo"] |
| 118 | + new_experience["logo"], |
113 | 119 | )
|
114 | 120 | data["experience"].append(experience_instance)
|
115 | 121 | return jsonify({"id": len(data["experience"]) - 1})
|
116 | 122 |
|
117 | 123 | return jsonify({})
|
118 | 124 |
|
119 |
| -@app.route('/resume/education', methods=['GET', 'POST']) |
| 125 | + |
| 126 | +@app.route("/resume/education", methods=["GET", "POST"]) |
120 | 127 | def education():
|
121 |
| - ''' |
| 128 | + """ |
122 | 129 | Handles education requests
|
123 |
| - ''' |
124 |
| - if request.method == 'GET': |
125 |
| - return jsonify({"education": [edu.__dict__ for edu in data["education"]]}) |
| 130 | + """ |
| 131 | + if request.method == "GET": |
| 132 | + return jsonify( |
| 133 | + {"education": [edu.__dict__ for edu in data["education"]]}) |
126 | 134 |
|
127 |
| - if request.method == 'POST': |
| 135 | + if request.method == "POST": |
128 | 136 | new_education = request.json
|
129 | 137 | education_instance = Education(
|
130 | 138 | new_education["course"],
|
131 | 139 | new_education["school"],
|
132 | 140 | new_education["start_date"],
|
133 | 141 | new_education["end_date"],
|
134 | 142 | new_education["grade"],
|
135 |
| - new_education["logo"] |
| 143 | + new_education["logo"], |
136 | 144 | )
|
137 | 145 | data["education"].append(education_instance)
|
138 | 146 | return jsonify({"id": len(data["education"]) - 1})
|
139 | 147 |
|
140 | 148 | return jsonify({})
|
141 | 149 |
|
142 | 150 |
|
143 |
| -@app.route('/resume/skill', methods=['GET', 'POST']) |
| 151 | +@app.route("/resume/skill", methods=["GET", "POST"]) |
144 | 152 | def skill():
|
145 |
| - ''' |
| 153 | + """ |
146 | 154 | Handles Skill requests
|
147 |
| - ''' |
148 |
| - if request.method == 'GET': |
| 155 | + """ |
| 156 | + |
| 157 | + if request.method == "GET": |
149 | 158 | return jsonify({"skills": [skill.__dict__ for skill in data["skill"]]})
|
150 | 159 |
|
151 |
| - if request.method == 'POST': |
| 160 | + if request.method == "POST": |
152 | 161 | new_skill = request.json
|
153 |
| - skill_instance = Skill(new_skill["name"], new_skill["proficiency"], new_skill["logo"]) |
| 162 | + skill_instance = Skill( |
| 163 | + new_skill["name"], new_skill["proficiency"], new_skill["logo"] |
| 164 | + ) |
154 | 165 | data["skill"].append(skill_instance)
|
155 | 166 | return jsonify({"id": len(data["skill"]) - 1})
|
156 | 167 |
|
157 | 168 | return jsonify({})
|
158 | 169 |
|
159 |
| -@app.route('/resume/spellcheck', methods=['POST']) |
| 170 | + |
| 171 | +@app.route("/resume/spellcheck", methods=["POST"]) |
160 | 172 | def spellcheck():
|
161 |
| - ''' |
| 173 | + """ |
162 | 174 | Corrects the spelling of a text
|
163 |
| - ''' |
| 175 | + """ |
164 | 176 | body = request.get_json()
|
165 | 177 | try:
|
166 |
| - text = body['text'] |
| 178 | + text = body["text"] |
167 | 179 | corrected_text = correct_spelling(text)
|
168 | 180 |
|
169 | 181 | return jsonify({"before": text, "after": corrected_text}), 200
|
170 | 182 | except KeyError:
|
171 | 183 | return jsonify({"error": "Missing text parameter"}), 400
|
| 184 | + |
| 185 | + |
| 186 | +@app.route("/suggestion", methods=["POST"]) |
| 187 | +def get_description_suggestion(): |
| 188 | + """ |
| 189 | + Handles suggestion requests |
| 190 | + """ |
| 191 | + description = request.json.get("description") |
| 192 | + description_type = request.json.get("type") |
| 193 | + if not description or not description_type: |
| 194 | + return jsonify({"error": "Description and type are required"}), 400 |
| 195 | + suggestion = get_suggestion(description, description_type) |
| 196 | + return jsonify({"suggestion": suggestion}) |
0 commit comments