def create_client(): try: session = current_app.db.session body = request.get_json() error_list = [] if not Client.query.filter_by(email=body["email"]).first(): email = body["email"] else: error_list.append("email") if not Client.query.filter_by(phone_number=body["phone_number"]).first(): phone_number = body["phone_number"] else: error_list.append("phone_number") if error_list: raise NameError(error_list) name = body["name"] password = body["password"] user_type = "client" new_client = Client( name=name, email=email, phone_number=phone_number, user_type=user_type, ) new_client.password = password session.add(new_client) session.commit() return { "id": new_client.id, "name": new_client.name, "email": new_client.email, }, HTTPStatus.CREATED except KeyError: return {"msg": "Verify BODY content"}, HTTPStatus.BAD_REQUEST except DataError: return {"msg": "Verify BODY content"}, HTTPStatus.BAD_REQUEST except NameError as e: unique = e.args[0] errors = "" for error in unique: errors = errors + f"{error}, " return {"msg": f"{errors}already registered"}, HTTPStatus.BAD_REQUEST
def addClient(userInformation): client = Client() client.first_name = userInformation["first-name"] client.last_name = userInformation["last-name"] client.role = userInformation["role"] client.belonging = userInformation["belonging"] client.finance = userInformation["finance"] client.work_code = userInformation["work-code"] client.e_mail = userInformation["e-mail"] client.password = myhash(userInformation["password"]) client.reference_list = userInformation["reference-list"] client.admin = userInformation["admin"] client.first_name_reading = userInformation["first-name-reading"] client.last_name_reading = userInformation["last-name-reading"] client.gender = userInformation["gender"] client.nationality = userInformation["nationality"] client.birthday = userInformation["birthday"] db.session.add(client) db.session.commit()