def delete(cls, email: str): customer = CustomerModel.find_by_email(email) if customer: customer.delete_from_db() return {"message": gettext("CUSTOMER_DELETED")} return {"message": gettext("CUSTOMER_NOT_FOUND")}, 404
def post(cls, _id: str): if CustomerModel.find_by_id(_id): return {"message": ERROR_ID_EXISTS}, 400 data = request.get_json() if CustomerModel.find_by_email(data["email"]): return {"message": ERROR_EMAIL_EXISTS}, 400 customer = customer_schema.load(data) try: customer.save_to_db() except: return {"message": ERROR_SAVING_USER}, 500 return {"message": SUCCESS_SAVING_USER}, 200
def post(self): data = self.parser.parse_args() user = CustomerModel.find_by_email(data["email"]) if user: return {'message': "The email {} already exists.".format(data["email"])}, 400 valid_data = {key: val for key, val in data.items() if val is not None} user = CustomerModel(**valid_data) try: user.save_to_db() except: return {"message": "An error occurred while saving to the database."}, 500 return user.json(), 201
def post(self): data = Customer.parser.parse_args() customer = CustomerModel.find_by_email(data["email"]) if customer: return ( { "message": f"a member with the same email already exists" }, 400, ) new_customer = CustomerModel(**data) try: new_customer.save_to_db() except: return ( { "message": "An error occured adding a member" }, 500, ) return new_customer.json(), 201
def post(cls): data = request.get_json() user_data = {"email": data['email'], "password": data['password']} user = CustomerModel.find_by_email(user_data['email']) if user and safe_str_cmp( user.password, user_data['password']): # confirm password matching is_confirmed = user.most_recent_confirmation.confirmed # true for confirmed email, else false if is_confirmed: 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 # successful login return { "message": ERROR_USER_CONFIRMATION }, 400 # user not confirmed his email return { "message": IDENTIFICATION_ERROR }, 400 # user password or email not matched
def post(cls, email: str): if CustomerModel.find_by_email(email): return { "message": gettext("CUSTOMER_EMAIL_ALREADY_EXISTS").format("email") }, 400, customer_json = request.get_json() print(f"customer_json={customer_json}") customer_json[ "email"] = email # we must add this to json since we receive email from endpoint and not from # request body like the lname and fname customer = customer_schema.load(customer_json) # customer = CustomerModel(email=email) try: customer.save_to_db() except: return {"message": gettext("CUSTOMER_ERROR_INSERTING")}, 500 return customer_schema.dump(customer), 201
def post(cls): data = request.get_json() if CustomerModel.find_by_email(data["email"]): return {"message": ERROR_EMAIL_EXISTS}, 400 address_data = data['address'] data.pop('address', None) user = customer_schema.load(data) try: user.save_to_db() confirmation = ConfirmationModel(user.id) address = AddressModel(**address_data, customer_id=user.id) address.save_to_db() confirmation.save_to_db() link = request.url_root[:-1] + url_for( "customerconfirmation", confirmation_id=user.most_recent_confirmation.id ) # support for emails foreign to Mailgun except: traceback.print_exc() return {"message": ERROR_SAVING_USER}, 500 user.send_confirmation_email() return {"message": USER_REGISTERED, "confirmation_link": link}, 200
def delete(self): data = Customer.parser.parse_args() customer = CustomerModel.find_by_email(data["email"]) if customer: customer.delete_from_db() return {"message": "member deleted"}
def get(cls, email: str): customer = CustomerModel.find_by_email(email) if customer: return customer_schema.dump(customer), 200 return {"message": gettext("CUSTOMER_NOT_FOUND")}, 404