11import jwt
2+ import datetime
23from flask import request , jsonify , make_response
34from flask_restful import Resource , abort
45from flask_bcrypt import check_password_hash
@@ -126,6 +127,7 @@ def post(self):
126127 first_name = first_name ,
127128 last_name = last_name ,
128129 email = email_id ,
130+ registered_on = datetime .datetime .now (),
129131 )
130132
131133 # insert the user
@@ -439,33 +441,7 @@ def put(self, username):
439441 abort (500 , message = "INTERNAL ERROR" )
440442
441443 # TODO: Check if the user has the right privileges
442- if verified :
443- # Check if the user payload contain fields that are either non-updatabale or
444- # are not present in the user db. If any key in the payload does not match
445- # with the column name we will abort the update request.
446- non_updatable = list (
447- set (post_data .keys ()) - set (User .__table__ .columns .keys ())
448- )
449- if non_updatable :
450- self .logger .warning (
451- "User trying to update fields that are not present in the user database. Fields: {}" ,
452- non_updatable ,
453- )
454- abort (400 , message = "Invalid fields in update request payload" )
455- try :
456- # We will update the user object with the keys and values provided in the request payload.
457- # THe keys need to match with the column names in the user model.
458- user .update (** post_data )
459- except ValueError :
460- self .logger .warning (
461- "Either provided values to update the user does not adhere to the user model "
462- "datatype or user attempted to update the protected properties."
463- )
464- abort (400 , message = "Invalid fields in update request payload" )
465- except Exception :
466- self .logger .exception ("Exception occurred during updating user object" )
467- abort (500 , message = "INTERNAL ERROR" )
468- else :
444+ if not verified :
469445 self .logger .warning (
470446 "Username retrieved from the auth token {} is different from the username provided" ,
471447 self .auth .token_auth .current_user ().id ,
@@ -474,6 +450,41 @@ def put(self, username):
474450 403 , message = "Authentication token does not belong to the current user"
475451 )
476452
453+ # Check if the user payload contain fields that are either non-updatabale or
454+ # are not present in the user db. If any key in the payload does not match
455+ # with the column name we will abort the update request.
456+ non_existent = list (set (post_data .keys ()) - set (User .__table__ .columns .keys ()))
457+ non_updatable = list (
458+ set (post_data .keys ()).intersection (set (User .get_protected ()))
459+ )
460+ if non_updatable :
461+ for field in non_updatable :
462+ if getattr (user , field ) != post_data [f"{ field } " ]:
463+ self .logger .warning (
464+ "Either provided values to update the user does not adhere to the user model "
465+ "datatype or user attempted to update the protected properties."
466+ )
467+ abort (400 , message = "Invalid data in update request payload" )
468+ if non_existent :
469+ self .logger .warning (
470+ "User trying to update fields that are not present in the user database. Fields: {}" ,
471+ non_existent ,
472+ )
473+ abort (400 , message = "Invalid fields in update request payload" )
474+ try :
475+ # We will update the user object with the keys and values provided in the request payload.
476+ # THe keys need to match with the column names in the user model.
477+ user .update (** post_data )
478+ except ValueError :
479+ self .logger .warning (
480+ "Either provided values to update the user does not adhere to the user model "
481+ "datatype or user attempted to update the protected properties."
482+ )
483+ abort (400 , message = "Invalid fields in update request payload" )
484+ except Exception :
485+ self .logger .exception ("Exception occurred during updating user object" )
486+ abort (500 , message = "INTERNAL ERROR" )
487+
477488 return_data = user .get_json ()
478489 response_object = {
479490 "status" : "success" ,
0 commit comments