def put(self, _id): data = self.parser.parse_args() user = CustomerModel.find_by_id(_id) if user: user.first_name = data["first_name"] # if data["first_name"] else user.first_name user.middle_name = data["middle_name"] user.last_name = data["last_name"] user.date_of_birth = date(*data["date_of_birth"]) user.gender = data["gender"] user.email = data["email"] user.phone_No_1 = data["phone_No_1"] user.phone_No_2 = data["phone_No_2"] user.phone_No_3 = data["phone_No_3"] user.address = data["address"] user.social_status = data["social_status"] user.national_id = data["national_id"] user.has_smartphone = data["has_smartphone"] user.has_computer = data["has_computer"] user.has_internet = data["has_internet"] else: 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()
def post(self, related_user_id): data = Customer.parser.parse_args() theCustomer = CustomerModel( data['credit_limit_balance'], EducationType(data['education']), MaritalStatusType(data['marriage']), data['age'], data['repayment_status_month_1'], data['repayment_status_month_2'], data['repayment_status_month_3'], data['repayment_status_month_4'], data['repayment_status_month_5'], data['repayment_status_month_6'], data['bill_amount_month_1'], data['bill_amount_month_2'], data['bill_amount_month_3'], data['bill_amount_month_4'], data['bill_amount_month_5'], data['bill_amount_month_6'], data['payment_amount_month_1'], data['payment_amount_month_2'], data['payment_amount_month_3'], data['payment_amount_month_4'], data['payment_amount_month_5'], data['payment_amount_month_6'], related_user_id, data['customer_result_id']) # OR USE: theCustomer = CustomerModel(related_user_id, **data) # **kwargs is a dictionary (key word args) try: theCustomer.save_to_db() except: return {"message": "An error occurred inserting the item"}, 500 return theCustomer.json(), 201
def post(self): data = CustomerRegister.parser.parse_args() print('data', data['address']) if UserModel.find_by_username(data['username']): return {'message': 'username already exists'}, 400 if AgentModel.find_by_agent_id(data['agent_id']) is None: return {'message': 'Invalid agent Id'}, 400 user = UserModel(data['username'], data['password'], usertype='customer') user.save_to_db() agent = AgentModel.find_by_agent_id(data['agent_id']) d, m, y = data['dob'].split('-') converted_dob = datetime(int(y), int(m), int(d)) customer = CustomerModel(customer_id=user.id, agent=agent, name=data['name'], dob=converted_dob, email=data['email'], address=data['address'], contact_num=data['contact_num'], alternate_num=data['alternate_num']) print('got it', customer.json()) customer.save_to_db() return {'message': 'customer Registered Successfully'}, 201
def put(self): data = Customer.parser.parse_args() customer = CustomerModel.find_by_full_name(data["f_name"], data["l_name"]) if customer: customer.email = data["email"] customer.active = data["active"] customer.created_at = data["created_at"] else: customer = CustomerModel(**data) customer.save_to_db() return customer.json()
def put(self, tax_id): data = Customer.parser.parse_args() customer = CustomerModel.find_by_taxid(tax_id) if customer: customer.debtor_value = data['debtor_value'] customer.customer_defaulter = data['customer_defaulter'] else: customer = CustomerModel(tax_id, data['debtor_value']) customer.save_to_db() return customer.json()
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 put(self, related_user_id): data = Customer.parser.parse_args() theCustomer = CustomerModel.find_recent_by_related_user_id( related_user_id) if theCustomer: # update if exists theCustomer.update_customer( data['credit_limit_balance'], EducationType(data['education']), MaritalStatusType(data['marriage']), data['age'], data['repayment_status_month_1'], data['repayment_status_month_2'], data['repayment_status_month_3'], data['repayment_status_month_4'], data['repayment_status_month_5'], data['repayment_status_month_6'], data['bill_amount_month_1'], data['bill_amount_month_2'], data['bill_amount_month_3'], data['bill_amount_month_4'], data['bill_amount_month_5'], data['bill_amount_month_6'], data['payment_amount_month_1'], data['payment_amount_month_2'], data['payment_amount_month_3'], data['payment_amount_month_4'], data['payment_amount_month_5'], data['payment_amount_month_6'], related_user_id, data['customer_result_id']) else: # create if dne theCustomer = CustomerModel( data['credit_limit_balance'], EducationType(data['education']), MaritalStatusType(data['marriage']), data['age'], data['repayment_status_month_1'], data['repayment_status_month_2'], data['repayment_status_month_3'], data['repayment_status_month_4'], data['repayment_status_month_5'], data['repayment_status_month_6'], data['bill_amount_month_1'], data['bill_amount_month_2'], data['bill_amount_month_3'], data['bill_amount_month_4'], data['bill_amount_month_5'], data['bill_amount_month_6'], data['payment_amount_month_1'], data['payment_amount_month_2'], data['payment_amount_month_3'], data['payment_amount_month_4'], data['payment_amount_month_5'], data['payment_amount_month_6'], related_user_id, data['customer_result_id']) # OR USE: theCustomer = CustomerModel(related_user_id, **data) theCustomer.save_to_db() return theCustomer.json()
def create_customer(): request_data = request.get_json() name = request_data['name'] description = request_data['description'] if CustomerModel.get_by_name(name): return {'message': f"customer with name '{name}' already exists"}, 400 customer = CustomerModel(name, description) try: customer.save() except Exception: return {"message", "Error creating the customer"}, 500 return customer.json()
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(self, tax_id): if CustomerModel.find_by_taxid(tax_id): return { 'message': "An customer with cpf '{}' already exists.".format( tax_id)}, 400 data = Customer.parser.parse_args() customer = CustomerModel( tax_id, data['debtor_value'], data['score_id'], data['name'], data['customer_defaulter']) try: customer.save_to_db() except: return { "message": "An error occurred inserting the customer."}, 500 return customer.json(), 201