Skip to content

Commit 2ca410d

Browse files
authored
Improvements: Move json data variable to a json object file (#38)
* todo: load resume json object file * resume.json data + load_data function * improv: load_data.json + respective test * +comments & code improvements to improve linter score * fix: update app.py to fix tests * fix: linting improvements * improve lint score * final linting improvements , i hope * +fix * fix: this should work fix * include project to reflect recent merged changes * 😔
1 parent 0aaeac0 commit 2ca410d

File tree

4 files changed

+199
-112
lines changed

4 files changed

+199
-112
lines changed

app.py

+60-108
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,13 @@
11
"""
22
Flask Application
33
"""
4-
54
from flask import Flask, jsonify, request
6-
from models import Experience, Education, Project, Skill, User
7-
from utils import get_suggestion, check_phone_number, correct_spelling
8-
5+
from models import Experience, Education, Skill, Project
6+
from utils import check_phone_number, correct_spelling, get_suggestion, load_data
97

108
app = Flask(__name__)
119

12-
data = {
13-
"user": [User("Jackie Stewart", "+4478322678", "[email protected]")],
14-
"experience": [
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-
),
31-
],
32-
"education": [
33-
Education(
34-
"Computer Science",
35-
"University of Tech",
36-
"September 2019",
37-
"July 2022",
38-
"80%",
39-
"example-logo.png",
40-
)
41-
],
42-
"skill": [
43-
Skill("Python",
44-
"1-2 Years",
45-
"example-logo.png")
46-
],
47-
"project": [
48-
Project(
49-
title="Sample Project",
50-
description="A sample project",
51-
technologies=["Python", "Flask"],
52-
link="https://github.com/username/sample-project"
53-
)
54-
]
55-
}
56-
10+
data = load_data('data/resume.json')
5711

5812
@app.route("/test")
5913
def hello_world():
@@ -64,59 +18,56 @@ def hello_world():
6418

6519

6620
@app.route("/resume/user", methods=["GET", "POST", "PUT"])
67-
def user():
21+
def user_route():
6822
"""
69-
Handles User information
23+
Handle GET, POST, and PUT requests for user data.
24+
GET: Retrieve all users
25+
POST: Create a new user
26+
PUT: Update an existing user
7027
"""
28+
if request.method == 'GET':
29+
return jsonify(data['user']), 200
7130

72-
# defining sub function to reduce number of returns
73-
def get_users():
74-
return jsonify([user.__dict__ for user in data["user"]]), 200
75-
76-
def add_user(body):
77-
# retrieve user's information.
78-
name = body["name"]
79-
phone_number = body["phone_number"]
80-
email = body["email_address"]
81-
# store the new user information.
82-
if not check_phone_number(phone_number):
83-
return jsonify({"error": "Incorrect phone number !"}), 400
84-
new_user = User(name, phone_number, email)
85-
data["user"].append(new_user)
86-
return jsonify(new_user.__dict__), 201
87-
88-
# edit the user information.
89-
def edit_user(body):
90-
name = body["name"]
91-
phone_number = body["phone_number"]
92-
email = body["email_address"]
93-
for i, user_ in enumerate(data["user"]):
94-
if user_.email_address == email:
95-
if not check_phone_number(phone_number):
96-
return jsonify({"error": "Incorrect phone number !"}), 400
97-
data["user"][i] = User(name, phone_number, email)
98-
return jsonify(data["user"][i].__dict__), 200
99-
return jsonify({"error": "User not found !"}), 404
31+
body = request.get_json()
32+
if not body or not all(key in body for key in ['name', 'phone_number', 'email_address']):
33+
return jsonify({"error": "Missing required fields"}), 400
10034

101-
if request.method == "GET":
102-
return get_users()
103-
if request.method == "POST":
104-
body = request.get_json()
105-
return add_user(body)
106-
if request.method == "PUT":
107-
body = request.get_json()
108-
return edit_user(body)
109-
return jsonify({"error": "Unsupported request method !"}), 405
35+
name = body['name']
36+
phone_number = body['phone_number']
37+
email = body['email_address']
38+
if not check_phone_number(phone_number):
39+
return jsonify({"error": "Incorrect phone number"}), 400
40+
41+
if request.method == 'POST':
42+
# Create a new user and add it to the data
43+
new_user = {
44+
'name': name,
45+
'phone_number': phone_number,
46+
'email_address': email
47+
}
48+
data['user'].append(new_user)
49+
return jsonify(new_user), 201
50+
51+
# Handle PUT request
52+
for i, current_user in enumerate(data['user']):
53+
if current_user['email_address'] == email:
54+
data['user'][i] = {
55+
'name': name,
56+
'phone_number': phone_number,
57+
'email_address': email
58+
}
59+
return jsonify(data['user'][i]), 200
60+
61+
return jsonify({"error": "User not found !"}), 404
11062

11163

11264
@app.route("/resume/experience", methods=["GET", "POST", 'PUT'])
11365
def experience():
11466
"""
11567
Handle experience requests
11668
"""
117-
if request.method == 'GET':
118-
return jsonify(
119-
{"experience": [exp.__dict__ for exp in data["experience"]]})
69+
if request.method == "GET":
70+
return jsonify({"experience": list(data["experience"])}), 200
12071

12172
if request.method == "POST":
12273
new_experience = request.json
@@ -130,7 +81,7 @@ def experience():
13081
new_exp["logo"],
13182
)
13283
data["experience"].append(experience_instance)
133-
return jsonify({"id": len(data["experience"]) - 1})
84+
return jsonify({"id": len(data["experience"]) - 1}), 201
13485

13586
if request.method == 'PUT':
13687
body = request.get_json()
@@ -145,10 +96,9 @@ def experience():
14596

14697
new_experience_order.append(
14798
Experience(title, company, start_date, end_date, description, logo)
148-
)
99+
)
149100
data['experience'] = new_experience_order
150-
151-
return_data = [item.__dict__ for item in data['experience']]
101+
return_data = list(data['experience'])
152102
return jsonify(return_data), 200
153103

154104
return jsonify({"error": "Unsupported request method !"}), 405
@@ -158,9 +108,9 @@ def education():
158108
"""
159109
Handles education requests
160110
"""
161-
if request.method == 'GET':
162-
return jsonify(
163-
{"education": [edu.__dict__ for edu in data["education"]]})
111+
112+
if request.method == "GET":
113+
return jsonify({"education": list(data["education"])}), 200
164114

165115
if request.method == "POST":
166116
new_education = request.json
@@ -174,7 +124,7 @@ def education():
174124
new_edu["logo"],
175125
)
176126
data["education"].append(education_instance)
177-
return jsonify({"id": len(data["education"]) - 1})
127+
return jsonify({"id": len(data["education"]) - 1}), 201
178128

179129
if request.method == 'PUT':
180130
body = request.get_json()
@@ -189,9 +139,10 @@ def education():
189139
new_education_order.append(Education(course, school, start_date, end_date, grade, logo))
190140
data['education'] = new_education_order
191141

192-
return_data = [item.__dict__ for item in data['education']]
142+
return_data = list(data['education'])
193143
return jsonify(return_data), 200
194-
return jsonify({})
144+
return jsonify({}), 405
145+
195146

196147

197148
@app.route("/resume/skill", methods=["GET", "POST", 'PUT'])
@@ -201,7 +152,7 @@ def skill():
201152
"""
202153

203154
if request.method == "GET":
204-
return jsonify({"skills": [skill.__dict__ for skill in data["skill"]]})
155+
return jsonify({"skills": list(data["skill"])}), 200
205156

206157
if request.method == "POST":
207158
new_skill = request.json
@@ -212,7 +163,7 @@ def skill():
212163
skill_data["logo"]
213164
)
214165
data["skill"].append(skill_instance)
215-
return jsonify({"id": len(data["skill"]) - 1})
166+
return jsonify({"id": len(data["skill"]) - 1}), 201
216167

217168
if request.method == 'PUT':
218169
body = request.get_json()
@@ -227,7 +178,7 @@ def skill():
227178
return_data = [item.__dict__ for item in data['skill']]
228179
return jsonify(return_data), 200
229180

230-
return jsonify({})
181+
return jsonify({}), 405
231182

232183

233184
@app.route('/resume/project', methods=['GET', 'POST', 'PUT', 'DELETE'])
@@ -245,7 +196,7 @@ def validate_id(project_id):
245196
if not project_id.isdigit():
246197
raise ValueError("Invalid id")
247198

248-
# check if the id is within the range of the project list
199+
# Check if the id is within the range of the project list
249200
int_id = int(project_id)
250201
if int_id < 0 or int_id >= len(data['project']):
251202
raise ValueError("Project not found")
@@ -264,7 +215,7 @@ def get_project(project_id):
264215
return jsonify({"error": str(error)}), 400
265216

266217
return jsonify([
267-
{**project.__dict__, "id": str(index)}
218+
{**project, "id": str(index)}
268219
for index, project in enumerate(data['project'])
269220
]), 200
270221

@@ -301,7 +252,7 @@ def edit_project(project_id, body):
301252
if hasattr(data['project'][project_id], key):
302253
setattr(data['project'][project_id], key, value)
303254
else:
304-
return jsonify({"error": f"invalid field: {key}"}), 400
255+
return jsonify({"error": f"Invalid field: {key}"}), 400
305256

306257
return jsonify({**data['project'][project_id].__dict__, "id": str(project_id)}), 200
307258

@@ -338,6 +289,7 @@ def delete_project(project_id):
338289

339290
return jsonify({"error": "Unsupported request method"}), 405
340291

292+
341293
@app.route("/resume/spellcheck", methods=["POST"])
342294
def spellcheck():
343295
"""
@@ -363,4 +315,4 @@ def get_description_suggestion():
363315
if not description or not description_type:
364316
return jsonify({"error": "Description and type are required"}), 400
365317
suggestion = get_suggestion(description, description_type)
366-
return jsonify({"suggestion": suggestion})
318+
return jsonify({"suggestion": suggestion}), 200

data/resume.json

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"user": [
3+
{
4+
"name": "Jackie Stewart",
5+
"phone_number": "+4478322678",
6+
"email_address": "[email protected]"
7+
}
8+
],
9+
"experience": [
10+
{
11+
"title": "Software Developer",
12+
"company": "A Cool Company",
13+
"start_date": "October 2022",
14+
"end_date": "Present",
15+
"description": "Writing Python Code",
16+
"logo": "example-logo.png"
17+
}
18+
],
19+
"education": [
20+
{
21+
"degree": "Computer Science",
22+
"institution": "University of Tech",
23+
"start_date": "September 2019",
24+
"end_date": "July 2022",
25+
"grade": "80%",
26+
"logo": "example-logo.png"
27+
}
28+
],
29+
"skill": [
30+
{
31+
"name": "Python",
32+
"experience": "1-2 Years",
33+
"logo": "example-logo.png"
34+
}
35+
],
36+
"project": [
37+
{
38+
"title": "My Title",
39+
"description": "A sample project",
40+
"technologies": ["Python", "Flask"],
41+
"link": "https://github.com/username/sample-project"
42+
}
43+
]
44+
}

0 commit comments

Comments
 (0)