def add_customer(customer_id, name, lastname, home_address, phone_number, email_address, status, credit_limit): """This function will add a new customer to the sqlite3 database.""" LOGGER.info('Adding a new customer') try: new_customer = Customer.create(customer_id=customer_id, customer_name=name, customer_last_name=lastname, customer_address=home_address, customer_phone=phone_number, customer_email=email_address, customer_status=status, customer_limit=credit_limit) new_customer.save() LOGGER.info('Database add successful') LOGGER.info(f'Customer: {name} ' f'{lastname} saved as' f' {customer_id}') except peewee.IntegrityError: LOGGER.info(f'{customer_id} already exists in database') raise ValueError('Customer already exists in the database') from None except Exception as err: LOGGER.info(f'Error creating = {customer_id} - check all inputs.') LOGGER.info(err)
def add_customer(customer_id: int, name: str, lastname: str, home_address: str, phone_number: str, email_address: str, status: bool, credit_limit: float): """ Add a new customer to the database. :param customer_id: int, Customer ID :param name: str, First name :param lastname: str, Last name :param home_address: str, Home address :param phone_number: str, Phone number :param email_address: str, Email address :param status: bool, True for "active", False for "inactive" :param credit_limit: float, credit limit """ # Create a new customer object with DB.transaction(): try: cust = Customer.create(id=customer_id, name=name, last_name=lastname, address=home_address, phone=phone_number, email=email_address, status=status, credit_limit=credit_limit) except pw.IntegrityError as err: raise ValueError(err) else: cust.save() logging.info("Added customer with ID=%d", customer_id)
def add_customer(customer_id, first_name, last_name, home_address, phone_number, email_address, is_active, credit_limit): """ add new customer to customer database :param customer_id:customer id :param first_name: customer first name :param last_name: customer last name :param home_address: customer address :param phone_number: customer cell phone number :param email_address: customer email address :param is_active: whether the customer is active :param credit_limit: customer credit limit :return: add a new customer to table """ try: LOGGER.info('Successfully connected to the database') with DATABASE.transaction(): new_customer = Customer.create(customer_id=customer_id, first_name=first_name, last_name=last_name, home_address=home_address, phone_number=phone_number, email_address=email_address, is_active=is_active, credit_limit=credit_limit) new_customer.save() LOGGER.info("Customer added successfully") except IntegrityError as error: LOGGER.info(error) LOGGER.info('Error occurred')
def add_customer(customer_id, customer_name, customer_lastname, customer_home_address, customer_phone_number, customer_email_address, customer_status, customer_credit_limit): '''Adds a customer to the database''' try: with database.transaction(): new_customer = Customer.create( customer_id=customer_id, name=customer_name, lastname=customer_lastname, home_address=customer_home_address, phone_number=customer_phone_number, email_address=customer_email_address, status=customer_status, credit_limit=customer_credit_limit) new_customer.save() logger.info('Database add successful') return new_customer except IntegrityError as i_e: logger.error(i_e) logger.info( 'Customer was not saved.' 'A customer with id of %s already exists.', customer_id) raise IntegrityError
def bulk_add_customers(csv_file): try: file_ = csv.DictReader(open(csv_file)) except FileNotFoundError: logging.info(f"{csv_file} doesn't seem to exist") print(f"{csv_file} doesn't seem to exist") raise FileNotFoundError for record in file_: if record['status'] not in [True, False]: logging.info(f"{record['customer_id']} has invalid status:\ {record['status']}") print(f"Status must be True or False for {record['customer_id']}") pass else: cust = Customer.create(customer_id=record['customer_id'], first_name=record['first_name'], last_name=record['last_name'], home_address=record['home_address'], phone_number=record['phone_number'], email_address=record['email_address'], status=record['status'], credit_limit=record['credit_limit']) cust.save() logging.info( f'Customer {record["customer_id"]} added successfully!')
def add_customer(customer_id, name, lastname, home_address, phone_number, email_address, status, credit_limit): '''add new customer function.''' try: with DB.atomic(): Customer.create( customer_id=customer_id, name=name, lastname=lastname, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit ) except IntegrityError: LOGGER.warning("Customer ID %s is already taken.", customer_id)
def test_create_dup(self): """Tests creating a customer with same ID.""" customer = { 'id': 1, 'name': 'Bob', 'last_name': 'Xavi', 'address': '123', 'phone': '412-569-8467', 'email': '*****@*****.**', 'status': True, 'credit_limit': 100 } created = Customer.create(**customer) with self.assertRaises(pw.IntegrityError): created = Customer.create(**customer)
def add_customer(customer_id, name, lastname, home_address, phone_number, email_address, status, credit_limit): """ Add a new customer to the database """ try: logging.debug(f"Adding new customer: {name} {lastname}") with DATABASE.transaction(): Customer.create(customer_id=customer_id, name=name, lastname=lastname, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit) logging.debug("Customer added successfully") except IntegrityError: logging.error(f"Error adding customer: {name} {lastname}") raise IntegrityError
def add_customer(customer_id, name, last_name, home_address, phone_number, email_address, status, credit_limit): new_customer = Customer.create( customer_id=customer_id, name=name, last_name=last_name, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit ) new_customer.save()
def add_customer(customer_id, name, last_name, home_address, phone_number, email_address, status, credit_limit): logging.debug('Adding customer') new_customer = Customer.create(customer_id=customer_id, name=name, last_name=last_name, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit) new_customer.save() logging.info('Added customer %s to database', customer_id)
def add_customer(**kwargs): """ This takes the specified dictionary and attempts to create a new customer record. Returns True on successful creation; otherwise, returns False. """ with CUSTOMER_DB.transaction(): try: new_customer = Customer.create(**kwargs) except IntegrityError: return False else: new_customer.save() return True
def add_customer(customer_id, name, lastname, home_address, phone_number, email_address, status, credit_limit): """Adding the new customer""" customer = Customer.create(customer_id=customer_id, name=name, last_name=lastname, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit) try: customer.save() except IntegrityError: print("Duplicate primary keys no allowed")
def add_customer(_name, _last_name, _home_address, _phone_number, _email_address, _status, _poverty_score): '''add new customer function.''' try: with db.transaction(): new_customer = Customer.create(name=_name, last_name=_last_name, home_address=_home_address, phone_number=_phone_number, email_address=_email_address, status=_status, poverty_score=_poverty_score) new_customer.save() except peewee.IntegrityError: LOGGER.warning("Customer Name %s is already taken.", _name)
def add_customer(customer_data): for guy in customer_data: try: with db.transaction(): new_customer = Customer.create( name=guy[0], last_name=guy[1], home_address=guy[2], phone_number=guy[3], email_address=guy[4], status=guy[5], poverty_score=guy[6] ) new_customer.save() except peewee.IntegrityError: print('IntegrityError, Error adding customer %s', guy)
def add_customer(**kwargs): """ This takes the specified dictionary and attempts to create a new customer record. Returns True on successful creation; otherwise, returns False. """ with CUSTOMER_DB.transaction(): try: new_customer = Customer.create(**kwargs) except IntegrityError as exception_info: logging.error('Error creating record: %s', exception_info) return False else: new_customer.save() logging.debug('Successfully creating record: %s', kwargs) return True
def add_customer(customer_id, name, last_name, home_address, phone_number, email_address, status, credit_limit): try: credit_limit = float(credit_limit) except ValueError as err: logging.error(err) raise new_customer = Customer.create(customer_id=customer_id, first_name=name, last_name=last_name, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit) new_customer.save()
def add_customer(customer_id, first_name, last_name, home_address, phone_number, email_address, status, credit_limit): try: cust = Customer.create(customer_id=customer_id, first_name=first_name, last_name=last_name, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit) cust.save() logger.info("Customersuccessfully added!") except Exception as e: logging.info(f"Could not add {first_name} to db") logging.info(e)
def add_customer(name, lastname, home_address, phone_number, email_address, status, credit_limit): """create a new customer object in the database table""" try: with DATABASE.transaction(): new_customer = Customer.create(name=name, lastname=lastname, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit) new_customer.save() LOGGER.info('Database add successful') return new_customer except pw.IntegrityError as exc: LOGGER.info("Error creating %s; Exception: %s", name, exc)
def add_customer(customer_id, name, last_name, home_address, phone_number, email_address, status, credit_limit): try: credit_limit = float(credit_limit) except ValueError as err: raise if not database.table_exists('Customer'): database.create_tables([Customer]) new_customer = Customer.create(customer_id=customer_id, first_name=name, last_name=last_name, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit) new_customer.save()
def add_customer(customer_id, customer_name, customer_lastname, customer_home_address, customer_phone_number, customer_email_address, customer_status, customer_credit_limit): '''Adds a customer to the database''' with database.transaction(): new_customer = Customer.create(customer_id=customer_id, name=customer_name, lastname=customer_lastname, home_address=customer_home_address, phone_number=customer_phone_number, email_address=customer_email_address, status=customer_status, credit_limit=customer_credit_limit) new_customer.save() logger.info('Database add successful') return new_customer
def add_customer(customer_id, name, lastname, home_address, phone_number, email_address, status, credit_limit): '''Adds a customer to the database.''' try: new_customer = Customer.create(cust_id=customer_id, f_name=name, l_name=lastname, address=home_address, phone=phone_number, email=email_address, status=status, credit=credit_limit) new_customer.save() LOGGER.info('Added %s %s to the customer database', name, lastname) except peewee.IntegrityError: LOGGER.info('Customer %s %s currently exists in the database', name, lastname) raise ValueError return new_customer
def add_customer(customer_id, firstname, lastname, home_address, phone_number, email_address, status, credit_limit): '''Add a new customer to the sqlite3 database''' try: logging.info('Attempting to add customer [%s] to the database!', customer_id) new_customer = Customer.create(c_id=customer_id, c_firstname=firstname, c_lastname=lastname, c_home_address=home_address, c_phone_number=phone_number, c_email_address=email_address, c_status=status, c_credit_limit=credit_limit) new_customer.save() LOGGER.info('Added the new customer, [%s %s]!', firstname, lastname) except DoesNotExist: LOGGER.info('Tried adding customer. Customer id [%s] cannot be added!', customer_id)
def add_customer(customer_id, name, last_name, home_address, phone_number, email_address, status, credit_limit): """This function will add a new customer to the sqlite3 database.""" try: credit_limit = float(credit_limit) except ValueError as err: logging.error(err) raise new_customer = Customer.create(customer_id=customer_id, first_name=name, last_name=last_name, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit) new_customer.save()
def add_customer(customer_id, first_name, last_name, mailing_address, phone_number, email_address, active_status, credit_limit): """Add a new customer to the database""" try: new_customer = Customer.create(customer_id=customer_id, first_name=first_name, last_name=last_name, mailing_address=mailing_address, phone_number=phone_number, email_address=email_address, active_status=active_status, credit_limit=credit_limit) new_customer.save() logging.info("Add new customer with id %s -OK", customer_id) except TypeError as oops: logging.debug("Add new customer with id of %s failed", customer_id) logging.debug(oops) raise oops
def test_create_customer(self): """Tests creating a customer.""" new_cust = { 'id': 5, 'name': 'Bob', 'last_name': 'Xavi', 'address': "509 S Main", 'phone': '281-874-2356', 'email': '*****@*****.**', 'status': True, 'credit_limit': 1 } created = Customer.create(**new_cust) # Assert that customer was created correctly for attr, val in new_cust.items(): self.assertEqual(val, getattr(created, attr))
def setUp(self) -> None: TEST_DB.bind([Customer]) TEST_DB.connect() TEST_DB.create_tables([Customer]) self.definitions = { 'Bob': { 'id': 1, 'name': 'Bob', 'last_name': 'Xavi', 'address': "505 N Thayer", 'phone': '713-874-2356', 'email': '*****@*****.**', 'status': True, 'credit_limit': 3400.12 }, 'Alice': { 'id': 2, 'name': 'Alice', 'last_name': 'Wond', 'address': "507 N Thayer", 'phone': '713-874-0001', 'email': '*****@*****.**', 'status': False, 'credit_limit': 12000 }, 'Bob2': { 'id': 3, 'name': 'Bob', 'last_name': 'Xavi', 'address': "509 S Main", 'phone': '281-874-2356', 'email': '*****@*****.**', 'status': True, 'credit_limit': 1 } } # Add all 3 people to database for person, defin in self.definitions.items(): created = Customer.create(**defin) created.save()
def add_customer(customer_id, name, last_name, home_address, phone_number, email_address, active, credit_limit): """ add a new customer to the database """ try: with database.transaction(): new_customer = Customer.create(customer_id=customer_id, name=name, last_name=last_name, home_address=home_address, phone_number=phone_number, email_address=email_address, active=active, credit_limit=credit_limit) new_customer.save() logger.info(f'Customer {customer_id} add successful') except (peewee.IntegrityError, peewee.OperationalError): logger.info(f'Error adding customer: {customer_id}') finally: database.close()
def add_customer(customer_id, name, lastname, home_address, phone_number, email_address, status, credit_limit): ''' This function will add a new customer to the sqlite3 database. ''' try: # id is automatically created and incremented by 1 with DATABASE.transaction(): new_customer = Customer.create( id=customer_id, name=name, last_name=lastname, home_address=home_address, phone_number=phone_number, email=email_address, status=status, credit_limit=credit_limit) new_customer.save() LOGGER.info(f'Database add successful for customerId {name}') except (OperationalError, DoesNotExist) as exc: LOGGER.info(f'Error creating = {name}') LOGGER.info(exc)
def add_customer(customer_id, first_name, last_name, home_address, email_address, phone_number, status, credit_limit, join_date, insertion_date, time_stamp, hobby): """ add customer info to database """ try: # open database DATABASE.connect() DATABASE.execute_sql('PRAGMA foreign_keys=ON;') LOGGER.info("connected to database") # insert customers to database with DATABASE.transaction(): new_customer = Customer.create(customer_id=customer_id, first_name=first_name, last_name=last_name, home_address=home_address, email_address=email_address, phone_number=phone_number, status=status, credit_limit=credit_limit, join_date=join_date, insertion_date=insertion_date, time_stamp=time_stamp, hobby=hobby) new_customer.save() # message if customer added LOGGER.info("new customer added to database") except OperationalError as err: LOGGER.info("conection to database failed") LOGGER.info(err) except (InternalError, InterfaceError, IntegrityError) as err: LOGGER.info("error creating a non-unique customer id %s", customer_id) LOGGER.info(err) finally: # close database DATABASE.close() LOGGER.info("database closed")
def add_customer(customer_id, name, lastname, home_address, phone_number, email_address, status, credit_limit): """ Add a new customer to the sqlite3 database. """ try: new_customer = Customer.create(customer_id=customer_id, name=name, lastname=lastname, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit) new_customer.save() LOGGER.info(f'Successfully added new customer with id {customer_id} ' +\ f'to the database.') except TypeError as err: LOGGER.info(f'Error creating = {customer_id}') LOGGER.info(err) raise err