Skip to content

Commit 7c5a31b

Browse files
Fix:updating an user with empty username and name gives an error message
1 parent 99820ef commit 7c5a31b

31 files changed

+219
-101
lines changed

app/api/api_extension.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask_restplus import Api
1+
from flask_restx import Api
22

33
api = Api(
44
title="Mentorship System API",

app/api/dao/mentorship_relation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,22 +182,22 @@ def accept_request(user_id: int, request_id: int):
182182

183183
# verify if request is in pending state
184184
if request.state != MentorshipRelationState.PENDING:
185-
return messages.NOT_PENDING_STATE_RELATION, HTTPStatus.BAD_REQUEST
185+
return messages.NOT_PENDING_STATE_RELATION, HTTPStatus.FORBIDDEN
186186

187187
# verify if I'm the receiver of the request
188188
if request.action_user_id == user_id:
189-
return messages.CANT_ACCEPT_MENTOR_REQ_SENT_BY_USER, HTTPStatus.BAD_REQUEST
189+
return messages.CANT_ACCEPT_MENTOR_REQ_SENT_BY_USER, HTTPStatus.FORBIDDEN
190190

191191
# verify if I'm involved in this relation
192192
if not (request.mentee_id == user_id or request.mentor_id == user_id):
193-
return messages.CANT_ACCEPT_UNINVOLVED_MENTOR_RELATION, HTTPStatus.BAD_REQUEST
193+
return messages.CANT_ACCEPT_UNINVOLVED_MENTOR_RELATION, HTTPStatus.FORBIDDEN
194194

195195
my_requests = user.mentee_relations + user.mentor_relations
196196

197197
# verify if I'm on a current relation
198198
for my_request in my_requests:
199199
if my_request.state == MentorshipRelationState.ACCEPTED:
200-
return messages.USER_IS_INVOLVED_IN_A_MENTORSHIP_RELATION, HTTPStatus.BAD_REQUEST
200+
return messages.USER_IS_INVOLVED_IN_A_MENTORSHIP_RELATION, HTTPStatus.FORBIDDEN
201201

202202
mentee = request.mentee
203203
mentor = request.mentor

app/api/dao/task.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def create_task(user_id: int, mentorship_relation_id: int, data: Dict[str, str])
3737
return messages.MENTORSHIP_RELATION_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND
3838

3939
if relation.state != MentorshipRelationState.ACCEPTED:
40-
return messages.UNACCEPTED_STATE_RELATION, HTTPStatus.BAD_REQUEST
40+
return messages.UNACCEPTED_STATE_RELATION, HTTPStatus.FORBIDDEN
4141

4242
if (relation.mentor_id != user_id) and (relation.mentee_id != user_id):
4343
return messages.USER_NOT_INVOLVED_IN_THIS_MENTOR_RELATION, 403
@@ -142,7 +142,7 @@ def complete_task(user_id: int, mentorship_relation_id: int, task_id: int):
142142
return messages.TASK_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND
143143

144144
if task.get("is_done"):
145-
return messages.TASK_WAS_ALREADY_ACHIEVED, HTTPStatus.BAD_REQUEST
145+
return messages.TASK_WAS_ALREADY_ACHIEVED, HTTPStatus.FORBIDDEN
146146
else:
147147
relation.tasks_list.update_task(
148148
task_id=task_id, is_done=True, completed_at=datetime.now().timestamp()

app/api/dao/task_comment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def delete_comment(user_id, _id, task_id, relation_id):
185185
return messages.TASK_COMMENT_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND
186186

187187
if task_comment.user_id != user_id:
188-
return messages.TASK_COMMENT_WAS_NOT_CREATED_BY_YOU_DELETE, HTTPStatus.BAD_REQUEST
188+
return messages.TASK_COMMENT_WAS_NOT_CREATED_BY_YOU_DELETE, HTTPStatus.FORBIDDEN
189189

190190
if task_comment.task_id != task_id:
191191
return messages.TASK_COMMENT_WITH_GIVEN_TASK_ID_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND

app/api/dao/user.py

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from operator import itemgetter
33
from http import HTTPStatus
44
from typing import Dict
5-
from flask_restplus import marshal
5+
from flask_restx import marshal
66
from sqlalchemy import func
77

88
from app import messages
@@ -34,12 +34,12 @@ class UserDAO:
3434
@staticmethod
3535
def create_user(data: Dict[str, str]):
3636
"""Creates a new user.
37-
37+
3838
Creates a new user with provided data.
39-
39+
4040
Arguments:
4141
data: A list containing the user's name, username, password, and email, as well as recognition that they have read and agree to the Terms and Conditions.
42-
42+
4343
Returns:
4444
A tuple with two elements. The first element is a dictionary containing a key 'message' containing a string which indicates whether or not the user was created successfully. The second is the HTTP response code.
4545
"""
@@ -73,13 +73,13 @@ def create_user(data: Dict[str, str]):
7373
@email_verification_required
7474
def delete_user(user_id: int):
7575
""" Deletes a user.
76-
76+
7777
Deletes the specified user and removes them from the directory, with checks to make sure that the user exists and is not the only administrator.
78-
78+
7979
Arguments:
8080
user_id: The ID of the user to be deleted.
81-
82-
Returns:
81+
82+
Returns:
8383
A tuple with two elements. The first element is a dictionary containing a key 'message' containing a string which indicates whether or not the user was created successfully. The second is the HTTP response code.
8484
"""
8585

@@ -102,65 +102,65 @@ def delete_user(user_id: int):
102102
@email_verification_required
103103
def get_user(user_id: int):
104104
""" Retrieves a user's profile information using a specified ID.
105-
105+
106106
Provides the user profile of the user whose ID matches the one specified.
107-
107+
108108
Arguments:
109109
user_id: The ID of the user to be searched.
110-
110+
111111
Returns:
112112
The UserModel class of the user whose ID was searched, containing the public information of their profile such as bio, location, etc.
113-
113+
114114
"""
115115

116116
return UserModel.find_by_id(user_id)
117117

118118
@staticmethod
119119
def get_user_by_email(email: str):
120120
""" Retrieves a user's profile information using a specified email.
121-
121+
122122
Provides the user profile of the user whose email matches the one specified.
123-
123+
124124
Arguments:
125125
email: The email of the user to be searched.
126-
126+
127127
Returns:
128128
The UserModel class of the user whose email was searched, containing the public information of their profile such as bio, location, etc.
129-
129+
130130
"""
131131

132132
return UserModel.find_by_email(email)
133133

134134
@staticmethod
135135
def get_user_by_username(username: str):
136136
""" Retrieves a user's profile information using a specified username.
137-
137+
138138
Provides the user profile of the user whose username matches the one specified.
139-
139+
140140
Arguments:
141141
username: The ID of the user to be searched.
142-
142+
143143
Returns:
144144
The UserModel class of the user whose username was searched, containing the public information of their profile such as bio, location, etc.
145-
145+
146146
"""
147147

148148
return UserModel.find_by_username(username)
149149

150150
@staticmethod
151151
def list_users(user_id: int, search_query: str = "", page: int = DEFAULT_PAGE, per_page: int = DEFAULT_USERS_PER_PAGE, is_verified = None):
152152
""" Retrieves a list of verified users with the specified ID.
153-
153+
154154
Arguments:
155155
user_id: The ID of the user to be listed.
156156
search_query: The search query for name of the users to be found.
157157
is_verified: Status of the user's verification; None when provided as an argument.
158158
page: The page of users to be returned
159159
per_page: The number of users to return per page
160-
160+
161161
Returns:
162162
A list of users matching conditions and the HTTP response code.
163-
163+
164164
"""
165165

166166
users_list = UserModel.query.filter(
@@ -197,23 +197,24 @@ def list_users(user_id: int, search_query: str = "", page: int = DEFAULT_PAGE, p
197197
@email_verification_required
198198
def update_user_profile(user_id: int, data: Dict[str, str]):
199199
""" Updates the profile of a specified user with new data.
200-
200+
201201
Replaces old data items with new ones in the provided data list, with a check for overlap between users in username and a check that a user with the specified ID exists
202-
202+
203203
Arguments:
204204
user_id: The ID of the user whose data will be updated.
205205
data: A list containing the user's information such as name, bio, location, etc.
206-
206+
207207
Returns:
208208
A message that indicates whether the update was successful or not and a second element which is the HTTP response code.
209-
209+
210210
"""
211211

212212
user = UserModel.find_by_id(user_id)
213213
if not user:
214214
return messages.USER_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND
215215

216216
username = data.get("username", None)
217+
217218
if username:
218219
user_with_same_username = UserModel.find_by_username(username)
219220

@@ -223,8 +224,17 @@ def update_user_profile(user_id: int, data: Dict[str, str]):
223224

224225
user.username = username
225226

226-
if "name" in data and data["name"]:
227-
user.name = data["name"]
227+
if "name" in data:
228+
if data["name"]:
229+
user.name = data["name"]
230+
else:
231+
return messages.NAME_FIELD_IS_MISSING,HTTPStatus.OK
232+
233+
if "username" in data:
234+
if data["username"]:
235+
user.username = data["username"]
236+
else:
237+
return messages.USERNAME_FIELD_IS_MISSING,HTTPStatus.OK
228238

229239
if "bio" in data:
230240
if data["bio"]:
@@ -291,7 +301,7 @@ def update_user_profile(user_id: int, data: Dict[str, str]):
291301

292302
if "available_to_mentor" in data:
293303
user.available_to_mentor = data["available_to_mentor"]
294-
304+
295305
user.save_to_db()
296306

297307
return messages.USER_SUCCESSFULLY_UPDATED, HTTPStatus.OK
@@ -300,16 +310,16 @@ def update_user_profile(user_id: int, data: Dict[str, str]):
300310
@email_verification_required
301311
def change_password(user_id: int, data: Dict[str, str]):
302312
""" Changes the user's password.
303-
313+
304314
Finds the user with the given ID, checks their current password, and then updates to the new one.
305-
315+
306316
Arguments:
307317
user_id: The ID of the user to be searched.
308318
data: The user's current and new password.
309-
319+
310320
Returns:
311321
A message that indicates whether the password change was successful or not and a second element which is the HTTP response code.
312-
322+
313323
"""
314324

315325
current_password = data["current_password"]
@@ -326,15 +336,15 @@ def change_password(user_id: int, data: Dict[str, str]):
326336
@staticmethod
327337
def confirm_registration(token: str):
328338
""" Determines whether a user's email registration has been confirmed.
329-
339+
330340
Determines whether a user's email registration was invalid, previously confirmed, or just confirmed.
331-
341+
332342
Arguments:
333343
token: Serialized and signed email address as a URL safe string.
334-
344+
335345
Returns:
336346
A message that indicates if the confirmation was invalid, already happened, or just happened, and the HTTP response code.
337-
347+
338348
"""
339349

340350
email_from_token = confirm_token(token)
@@ -354,15 +364,15 @@ def confirm_registration(token: str):
354364
@staticmethod
355365
def authenticate(username_or_email: str, password: str):
356366
""" User login process.
357-
367+
358368
The user can login with two options:
359369
-> username + password
360370
-> email + password
361-
371+
362372
Arguments:
363373
username_or_email: The username or email associated with the account being authenticated.
364374
password: The password associated with the account being authenticated.
365-
375+
366376
Returns:
367377
Returns authenticated user if username and password are valid, otherwise returns None.
368378
"""

app/api/models/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask_restplus import fields, Model
1+
from flask_restx import fields, Model
22

33

44
def add_models_to_namespace(api_namespace):

app/api/models/mentorship_relation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask_restplus import fields, Model
1+
from flask_restx import fields, Model
22

33
from app.utils.enum_utils import MentorshipRelationState
44

app/api/models/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask_restplus import fields, Model
1+
from flask_restx import fields, Model
22
from app.api.models.mentorship_relation import (
33
list_tasks_response_body,
44
mentorship_request_response_body_for_user_dashboard_body,

app/api/resources/admin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from flask import request
2-
from flask_restplus import Resource, Namespace, marshal
2+
from flask_restx import Resource, Namespace, marshal
33
from flask_jwt_extended import jwt_required, get_jwt_identity
44
from http import HTTPStatus
55
from app import messages
@@ -108,7 +108,7 @@ def get(cls):
108108
A admin user with valid access token can view the list of all admins. The endpoint
109109
doesn't take any other input. A JSON array having an object for each admin user is
110110
returned. The array contains id, username, name, slack_username, bio,
111-
location, occupation, organization, skills.
111+
location, occupation, organization, skills.
112112
The current admin user's details are not returned.
113113
"""
114114
user_id = get_jwt_identity()

app/api/resources/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask_restplus import reqparse
1+
from flask_restx import reqparse
22

33
auth_header_parser = reqparse.RequestParser()
44
auth_header_parser.add_argument(

0 commit comments

Comments
 (0)