def post(cls): user_json = request.get_json() if 'password' in user_json: user_json['password'] = encrypt_password(user_json['password']) user = user_schema.load(user_json, instance=UserModel()) # if UserModel.find_by_username(user.username): # return {"message": gettext("user_username_exists")}, 400 if UserModel.find_by_id(user.id): return {"message": gettext("user_id_exists")}, 400 if UserModel.find_by_email(user.email): return {"message": gettext("user_email_exists")}, 400 if UserModel.find_by_phone(user.phone) and user.phone: return {"message": gettext("user_phone_exists")}, 400 try: user.save_to_db() confirmation = ConfirmationModel(user.id) confirmation.save_to_db() user.send_confirmation_email() if user.phone: user.send_sms() return {"message": gettext("user_registered")}, 201 except MailGunException as e: user.delete_from_db() # rollback return {"message": str(e)}, 500 except TwilioException as e: user.delete_from_db() # rollback return {"message": str(e)}, 500 except: # failed to save user to db traceback.print_exc() user.delete_from_db() # rollback return {"message": gettext("user_error_creating")}, 500
def post(cls): user_json = request.get_json() user = user_schema.load(user_json) if UserModel.find_by_username(user.username): print('oi') return {"message": gettext("user_username_exists")}, 400 if UserModel.find_by_email(user.email): return {"message": gettext("user_email_exists")}, 400 try: pw_hash = bcrypt.generate_password_hash(user.password, 10).decode('utf-8') user.password = pw_hash user.save_to_db() confirmation = ConfirmationModel(user.id) confirmation.save_to_db() # user.send_confirmation_email() return {"message": gettext("user_registered")}, 201 except MailGunException as e: user.delete_from_db() # rollback return {"message": str(e)}, 500 except: # failed to save user to db traceback.print_exc() user.delete_from_db() # rollback return {"message": gettext("user_error_creating")}, 500
def post(cls): json = request.get_json() user = user_schema.load(json) if UserModel.find_by_username(user.username): return {"message": ALREADY_EXISTS}, 400 if UserModel.find_by_email(user.email): return {"message": EMAIL_ALREADY_EXISTS}, 400 try: user.save_to_db() confirmation = ConfirmationModel(user.id) confirmation.save_to_db() # user.send_confirmation_email() return {"message": USER_CREATED}, 201 except MailGunException as e: user.delete_from_db() # rollback return {"message": str(e)}, 500 except: # failed to save user to db user.delete_from_db() # rollback traceback.print_exc() return {"message": FAILED_TO_CREATE}, 500
def post(self): user = user_schema.load(request.get_json()) if UserModel.find_by_username(user.username): return ( { "message": gettext("user_USER_EXISTS_ERROR").format(user.username) }, 400, ) if UserModel.find_by_email(user.email): return ( { "message": gettext("user_EMAIL_EXISTS_ERROR").format(user.email) }, 400, ) user.create_user() confirmation = ConfirmationModel(user.id) confirmation.save_to_db() user.send_confirmation_email() return {"message": gettext("user_USER_CREATED_MSG")}, 201
def post(cls, user_id: int): user = UserModel.find_by_id(user_id) if not user: return {"message": gettext("user_not_found")}, 404 try: confirmation = user.most_recent_confirmation if confirmation: if confirmation.confirmed: return { "message", gettext("confirmation_already_confirmed") }, 499 confirmation.force_to_expire() new_confirmation = ConfirmationModel(user_id) new_confirmation.save_to_db() user.send_confirmation_email() return {"message": gettext("confirmation_resend_success")}, 201 except MailgunException as e: return {"message", str(e)}, 500 except: traceback.print_exc() return {"message": gettext("confirmation_resend_error")}, 500
def post(cls): user_json = request.get_json() user = user_schema.load(user_json) if UserModel.find_by_username(user.username): return {'message': gettext('user_username_exists')}, 400 if UserModel.find_by_email(user.email): return {'message': gettext('user_email_exists')}, 400 try: user.save_to_db() confirmation = ConfirmationModel(user.id) confirmation.save_to_db() user.send_confirmation_email() return {'message': gettext('user_registered')}, 201 except MailGunException as e: user.delete_from_db() return {'message': str(e)}, 500 except: traceback.print_exc() user.delete_from_db() return {'message': gettext('user_error_creating')}, 500
def post(cls, user_id: int): """Resend confirmation email.""" user = UserModel.find_by_id(user_id) if not user: return {'message': getext("user_not_found")}, 404 try: confirmation = user.most_recent_confirmation if confirmation: if confirmation.confirmed: return { 'message': getext("confirmation_already_confirmed") }, 400 confirmation.force_to_expire() new_confirmation = ConfirmationModel(user_id) new_confirmation.save_to_db() user.send_confirmation_email() return {'message': getext("confirmation_resend_successful")} except MailgunException as e: return {'message': str(e)}, 500 except: traceback.print_exc() return {'message': getext("confirmation_resend_fail")}, 500
def post(cls, user_id: int): """Resend confirmation""" user = UserModel.fetch_by_id(user_id) if not user: return {"message": "user not found"}, 404 try: confirmation = user.most_recent_confirmation if confirmation: if confirmation.confirmed: return {"message": "already confirmed"}, 400 else: confirmation.force_to_expire() else: new_confirmation = ConfirmationModel(user_id) new_confirmation.save_to_db() user.send_confirmation_email() return {"message": "Resend successful"}, 201 except MailgunException as e: return {"message": str(e)} except: traceback.print_exc() return {"message": "Resend Failed"}, 500
def post(cls): """Register user and pass to confirmation page""" user_data = request.get_json() user = user_schema.load(request.get_json()) if UserModel.find_by_email(user.email): return {'message': gettext("user_email_exists"), 'status': 400} if "@" not in user.email: return {'message': gettext("user_email_incorrect"), 'status': 400} if UserModel.find_by_username(user.username): return {'message': gettext("user_username_exists"), 'status': 400} if user.password != user_data['confirm']: return {'message': gettext("user_password_mismatch"), 'status': 400} if len(user.password) < 6: return {'message': gettext("user_password_too_short"), 'status': 400} hashed_password = generate_password_hash(user.password, method='sha256') # password get hashed for security purposes new_user = UserModel(email=user.email, username=user.username, password=hashed_password) try: new_user.save_to_db() confirmation = ConfirmationModel(new_user.id) confirmation.save_to_db() confirmation_id = confirmation.id return {'confirmation': confirmation_id, 'status': 200} except: new_user.delete_from_db() return {"message": gettext("user_error_creating"), 'status': 500}
def post(cls): user = user_schema.load(request.get_json()) logging.info(f"USER REGISTER: Passed user= {user}") if UserModel.find_by_username(user["username"]): return {"message": msgs.USER_EXISTS}, 400 if UserModel.find_by_email(user["email"]): return {"message": msgs.EMAIL_EXISTS}, 400 pw_salt, pw_hash = hash_new_password(password=user["password"]) this_user = UserModel(id=None, username=user["username"], email=user["email"], pw_salt=pw_salt, pw_hash=pw_hash, displayname=user["displayname"]) try: this_user.save_to_db() confirmation = ConfirmationModel(this_user.id) confirmation.save_to_db() resp = this_user.send_confirmation_email() if resp: logging.info( f"USER: Confirmation email sent: {json.loads(resp.text)['id']}" ) return {"message": msgs.CREATED.format(this_user.username)}, 201 except MailGunException as e: logging.error(f"USER: Mailgun exception caught: {str(e)}") this_user.delete_from_db() return {"error": msgs.FAILED_TO_MAIL.format(this_user.email)}, 500 except Exception as e: logging.error(f"USER REGISTER: Other exception: {str(e)}") traceback.print_exc() this_user.delete_from_db() return {"error": msgs.FAILED_TO_CREATE.format(str(e))}, 500
def post(cls): user_json = request.get_json() user = user_schema.load(user_json) if UserModel.find_by_username(user.username): return {"message": gettext("user_name_already_exists")}, 400 if UserModel.find_by_email(user.email): return {"message": gettext("user_email_already_exists")}, 400 try: user.save_to_db() confirmation = ConfirmationModel(user.id) confirmation.save_to_db() user.send_confirmation_email() return {"message": gettext("user_creation_success")}, 201 except MailGunException as error: user.delete_from_db() return {"message": str(error)}, 500 except: # failed to save user to db traceback.print_exc() user.delete_from_db() return {"message": gettext("user_creation_failure")}, 500
def post(cls, user_id: int): """ This endpoint resend the confirmation email with a new confirmation model. It will force the current confirmation model to expire so that there is only one valid link at once. """ user = UserModel.find_by_id(user_id) if not user: return {"message": msgs.USER_NONEXISTANT.format(user_id)}, 404 logging.debug(f"Processing confirmation resend for {user.username}") try: # find the most current confirmation for the user confirmation = user.most_recent_confirmation # using property decorator if confirmation: if confirmation.confirmed: return {"message": msgs.ALREADY_CONFIRMED}, 400 confirmation.force_to_expire() new_confirmation = ConfirmationModel( user_id) # create a new confirmation new_confirmation.save_to_db() # Does `user` object know the new confirmation by now? Yes. # An excellent example where lazy='dynamic' comes into use. user.send_confirmation_email() # re-send the confirmation email return {"message": msgs.RESEND_SUCCESSFUL}, 201 except MailGunException as e: logging.error(f"CONFIRMATION: Mailgun error {str(e)}") return {"message": str(e)}, 500 except Exception as e: traceback.print_exc() return {"message": msgs.RESEND_FAILED}, 500
def post(cls): json = request.get_json() # user_schema creates a UserModel object with attribute values being request variables user = user_schema.load(json) if UserModel.find_by_username(user.username): return {"message": USERNAME_EXISTS}, 400 if UserModel.find_by_email(user.email): return {"message": EMAIL_EXISTS}, 400 try: user.save_to_db() confirmation = ConfirmationModel(user.id) confirmation.save_to_db() user.send_confirmation_email() return {"message": SUCCESS_REGISTER_MESSAGE}, 201 except MailGunException as e: user.delete_from_db() return {"message": str(e)}, 500 except: traceback.print_exc() user.delete_from_db() return {"message": FAILED_TO_CREATE}
def put(cls): user_json = request.get_json() user = UserModel.find_by_id(user_json['user_id']) if not user: return {"message": gettext("user_not_found")}, 404 if user.password: return {"message": gettext("user_id_exists")}, 400 user.temporary_password = encrypt_password( user_json['temporary_password']) try: user.save_to_db() #print(user.most_recent_confirmation) if not user.most_recent_confirmation: confirmation = ConfirmationModel(user.id) confirmation.save_to_db() user.send_confirmation_email() user.send_sms() elif user.most_recent_confirmation.expired: confirmation = ConfirmationModel(user.id) confirmation.save_to_db() user.send_confirmation_email() user.send_sms() elif not user.most_recent_confirmation.expired: return {"message": gettext("confirmation_already_sent")}, 409 return {"message": gettext("user_registered")}, 201 except MailGunException as e: user.delete_from_db() # rollback return {"message": str(e)}, 500 except TwilioException as e: user.delete_from_db() # rollback return {"message": str(e)}, 500 except: # failed to save user to db traceback.print_exc() return {"message": gettext("user_error_creating")}, 500
def post(cls): try: user = user_schema.load(request.get_json()) except ValidationError as err: return err.messages, 400 if UserModel.find_by_username(user.username): return {"message": gettext("user_exists")}, 400 if UserModel.find_by_email(user.username): return {"message": gettext("user_email_exists")}, 400 try: user.save_to_db() confirmation = ConfirmationModel(user.id) confirmation.save_to_db() user.send_confirmation_email() return {"message": gettext("user_success_register")}, 201 except MailgunException as e: user.delete_from_db() # rollback return {'message': str(e)}, 500 except: traceback.print_exc() user.delete_from_db() return {'message': gettext("user_failed_to_create")}, 500
def get(cls): resp = github.authorized_response() if resp is None or resp.get("access_token") is None: error_response = { "error": request.args["error"], "error_description": request.args["error_description"] } return error_response g.access_token = resp['access_token'] github_user = github.get('user') github_username = github_user.data['login'] user = UserModel.find_by_username(github_username) if not user: user = UserModel(username=github_username, password=None) user.save_to_db() confirmation = ConfirmationModel(user.id) confirmation.confirmed = True confirmation.expire_at = 0 confirmation.save_to_db() access_token = create_access_token(identity=user.id, fresh=True) refresh_token = create_refresh_token(user.id) return { "access_token": access_token, "refresh_token": refresh_token }, 200
def post(cls): try: user = user_schema.load(request.get_json()) except ValidationError: username = request.form["username"] password = request.form["password"] email = request.form["email"] user = user_schema.load({ "username": username, "password": password, "email": email }) if UserModel.find_by_username(user.username): return {"message": gettext("user_already_exists")}, 400 if UserModel.find_by_email(user.email): return {"message": gettext("user_email_already_exists")}, 400 try: user.save_to_db() confirmation = ConfirmationModel(user.id) confirmation.save_to_db() user.send_confirmation_email() except MailgunException as e: user.delete_from_db() return {"message": str(e)}, 500 except: user.delete_from_db() traceback.print_exc() return {"message": gettext("user_failed_to_create")}, 500 return {"message": gettext("user_created_successfully")}, 201
def post(cls, user_id): """ This endpoint resend the confirmation email with a new confirmation model. It will force the current confirmation model to expire so that there is only one valid link at once. """ user = UserModel.find_by_id(user_id) if not user: return {"message": gettext("user_not_found")}, 404 try: # find the most current confirmation for the user confirmation = user.most_recent_confirmation # using property decorator if confirmation: #this render old confirmation link invalid if confirmation.confirmed: # if the confirmation is already confirmed, return already confirmed return {"message": gettext("confirmation_already_confirmed")}, 400 confirmation.force_to_expire() #else, force the confirmation to expire with the force_to_expire function new_confirmation = ConfirmationModel(user_id) # create a new confirmation for that user_id new_confirmation.save_to_db() # Does `user` object know the new confirmation by now? Yes. # An excellent example where lazy='dynamic' comes into use. user.send_confirmation_email() # re-send the confirmation email return {"message": gettext("confirmation_resend_successful")}, 201 except MailGunException as e: return {"message": str(e)}, 500 except: traceback.print_exc() return {"message": gettext("confirmation_resend_fail")}, 500
def post(cls, user_id: int): """ Resend confirmation email""" user = UserModel.find_by_id(user_id) if not user: return {"message": gettext("User not found.")}, 404 try: confirmation = user.most_recent_confirmation if confirmation: if confirmation.is_confirmed: return { "message": gettext("confirmation_already_confirmed") }, 400 confirmation.force_to_expire() new_confirmation = ConfirmationModel(user_id) new_confirmation.save_to_db() user.send_confirmation_email() return {"message": gettext("confirmation_resent")}, 201 except MailGunException as error: return {"message": str(error)}, 500 except: traceback.print_exc() return {"message": gettext("confirmation_resend_failure")}, 500
def post(cls, user_id: int): """Resend the confirmation.""" user = UserModel.find_by_id(user_id) if not user: return {"message": USER_NOT_FOUND} try: confirmation = user.most_recent_confirmation if confirmation: if confirmation.confirmed: return { "message": gettext("confirmation_already_confirmed") } confirmation.force_to_expire() new_confirmation = ConfirmationModel(user_id) new_confirmation.save_to_db() user.send_confirmation_email() return {"message": gettext("confirmation_resend_successful")}, 201 except MailGunException as ex: return {"message": str(ex)}, 500 except: traceback.print_exc() return {"message": gettext("confirmation_resend_fail")}
def post(cls, user_id): """ This endpoint resend the confirmation email with a new confirmation model. It will force the current confirmation model to expire so that there is only one valid link at once. """ user = UserModel.find_by_id(user_id) if not user: return {"message": USER_NOT_FOUND}, 404 try: # find the most current confirmation for the user confirmation = user.most_recent_confirmation # using property decorator if confirmation: if confirmation.confirmed: return {"message": ALREADY_CONFIRMED}, 400 confirmation.force_to_expire() new_confirmation = ConfirmationModel( user_id) # create a new confirmation new_confirmation.save_to_db() # Does `user` object know the new confirmation by now? Yes. # An excellent example where lazy='dynamic' comes into use. user.send_confirmation_email() # re-send the confirmation email return {"message": RESEND_SUCCESSFUL}, 201 except MailGunException as e: return {"message": str(e)}, 500 except: traceback.print_exc() return {"message": RESEND_FAIL}, 500
def post(self): """create a new user """ data = api.payload email = data["email"] if UserModel.fetch_by_email(email): return {"message": "That user already exists"}, 400 else: user = UserModel(**data) try: user.save_to_db() confirmation = ConfirmationModel(user.id) confirmation.save_to_db() user.send_confirmation_email() return { "message": "user created successfuly, we have sent an activation link to your email" } except MailgunException as e: user.delete_from_db() return {"message": str(e)}, 500 except: traceback.print_exc() user.delete_from_db() return ( { "message": "user not created successfully. internal server error" }, 500, )
def get(cls): resp = github.authorized_response() if resp is None or resp.get("access_token") is None: return { "error": request.args["error"], "error_description": request.args["error_description"] }, 500 g.access_token = resp["access_token"] github_user = github.get("user") github_username = github_user.data["login"] user = UserModel.find_by_username(github_username) if not user: user = UserModel(username=github_username, password=None) user.save_to_db() confirmation = ConfirmationModel(user_id=user.id) confirmation.confirmed = True confirmation.save_to_db() access_token = create_access_token(identity=user.id, fresh=True) refresh_token = create_refresh_token(user.id) return { 'access_token': access_token, 'refresh_token': refresh_token }, 200
def post(cls): user_json = request.get_json() user = user_schema.load(user_json) if UserModel.find_by_username(user.username): return {"message": USER_ALREADY_EXISTS}, 400 if UserModel.find_by_email(user.email): return {"message": EMAIL_ALREADY_EXISTS}, 400 hashed_pwd = security.encrypt_password(user.password) user.password = hashed_pwd try: user.save_to_db() confirmation = ConfirmationModel(user.id) confirmation.save_to_db() user.send_confirmation_email() return {"message": SUCCESS_REGISTER_MESSAGE}, 201 except MailgunException as e: user.delete_from_db() return {"message": str(e)}, 500 except: traceback.print_exc() user.delete_from_db() return {"message": FAILED_TO_CREATE}, 500
def post(cls): userJSON = request.get_json() userJSON["password"] = pwd_context.encrypt(userJSON["password"]) user = userSchema.load(userJSON, partial=("email", )) if UserModel.find_by_username(user.username): return {'message': 'A user with that username already exists'}, 400 if UserModel.find_by_email(user.email): return {'message': 'A user with that email already exists'}, 400 try: user.save_to_db() confirmation = ConfirmationModel(user.id) confirmation.save_to_db() user.send_confirmation_email() except MailGunException as e: user.delete_from_db() return {"message": str(e)}, 500 except Exception as e: print(e) user.delete_from_db() return {"message": "Failed to create user"}, 500 return {'message': 'User created successfully.'}, 201
def post(cls): # try: user = user_schema.load(request.get_json()) # data = _user_parser.parse_args() # except ValidationError as err: # return err.messages, 400 if UserModel.find_by_username(user.username): return {"message": gettext("user_username_exists")}, 400 #in order to enaure that our email is unique if UserModel.find_by_email(user.email): return {"message": gettext("user_email_exists")}, 400 try: # user = UserModel(**user) # we no longer need to create a user model down here user.save_to_db() confirmation = ConfirmationModel( user.id ) # we want to create a confirmation model with the user then save to database before sending confirmation email confirmation.save_to_db() user.send_confirmation_email() return {"message": gettext("user_registered")}, 201 except MailGunException as e: user.delete_from_db( ) # so that if registration is incomplete without email, we want to remove the user as they can't confirm their account return {"message": str(e)}, 500 except: #failed to save user to db by deleting the entered data traceback.print_exc() user.delete_from_db() return {"message": gettext("user_error_creating")}, 500
def post(cls, user_id: int): """Resend confirmation email""" user = UserModel.find_by_id(user_id) if not user: return {'message': 'user not found'}, 404 try: confirmation = user.find_last_confirmation if confirmation: if confirmation.confirmed: return {'message': "already confirmed"}, 400 confirmation.force_to_expire() new_confirmation = ConfirmationModel(user_id) new_confirmation.save_to_db() user.send_confirmation_email() except MailgunException as e: return {'message': str(e)}, 500 except: traceback.print_exc() return {'message': 'resent confirmation email failed'}, 500 return {'message': 'resent confirmation email'}, 200
def post(cls): user = user_schema.load(request.get_json()) if UserModel.find_by_username(user.username): return {"message": "A user with that username already exists"}, 400 try: user.save_to_db() confirmation = ConfirmationModel(user.id) confirmation.save_to_db() # user.send_confirmation_email() return ( { "message": "User created successfully. Please check email for confirmation link" }, 201, ) except MailgunException as e: user.delete_from_db() return {"message": str(e)}, 500 except: traceback.print_exc() user.delete_from_db() return {"message": "Failed to create user"}, 500
def post(cls, user_id): """ This endpoint resend the confirmation email with a new confirmation model. It will force the current confirmation model to expire so that there is only one valid link at once. """ user = UserModel.find_by_id(user_id) if not user: return {"message": gettext("user_not_found")}, 404 try: # find the most current confirmation for the user confirmation = user.most_recent_confirmation # using property decorator if confirmation: if confirmation.confirmed: return {"message": gettext("confirmation_already_confirmed")}, 400 confirmation.force_to_expire() new_confirmation = ConfirmationModel(user_id) # create a new confirmation new_confirmation.save_to_db() user.send_confirmation_email() # re-send the confirmation email return {"message": gettext("confirmation_resend_successful")}, 201 except MailGunException as e: return {"message": str(e)}, 500 except: traceback.print_exc() return {"message": gettext("confirmation_resend_fail")}, 500
def post(self): user = user_schema.load(request.get_json()) # Using flask-marshmallow, we no longer get a dict object. Instead, we # get an UserModel object when load is called. user_schema knows this # info, from the definition of UserSchema # prev : user = UserModel.find_by_name(user['username']) if UserModel.find_by_name(user.username): return {'message': 'A user with this username already exists'}, 400 if UserModel.find_by_email(user.email): return {'message': 'A user with this email already exists'}, 400 # or unpack as: UserModel(**data) # No need to create instance of UserModel # user = UserModel(data['username'], data['password']) try: user.save_to_db() # create new confirmation and save to db confirmation = ConfirmationModel(user.id) confirmation.save_to_db() user.send_confirmation_email() except MailgunException as err: user.delete_from_db() # rollback return {'message': str(err)}, 500 except: user.delete_from_db() # rollback traceback.print_exc() return {'message': 'Failed to create user'}, 500 return {'message': 'We have sent you an email. Please confirm'}, 201