def upload_csv(filename): """ Adds data for customer from csv file """ with open(filename, newline="", encoding="ISO-8859-1") as csvfile: all_customers = csv.reader(csvfile) header = next(all_customer, None) with Customer.DATABASE.atomic(): # Populates the databse with info Customer.insert_many(all_customers, headers).execute
def _insert_customer_list(customer_list, database): ''' Internal function to add customer list with error checking :param customer_list: sequence of customer dictionaries to insert :param database: database object for the atomic transaction ''' with database.atomic(): try: Customer.insert_many(customer_list).execute() except pw.IntegrityError as error: logging.error(f"Customer already exists: {str(error)}") raise else: LOGGER.info(f"Inserted {len(customer_list)} customer(s)")
def add_customer(customer_id, name, lastname, home_address, phone_number, email_address, status, credit_limit): """ :param customer_id: :param name: :param lastname: :param home_address: :param phone_number: :param email_address: :param status: :param credit_limit: :param stage: :return: """ try: credit_limit = float(credit_limit) except ValueError as err: logging.error(err) raise args = { 'customer_id' : customer_id, 'first_name' : name, 'last_name' : lastname, 'home_address' : home_address, 'phone_number' : phone_number, 'email_address' : email_address, 'status' : status, 'credit_limit' : credit_limit } new_customer = Customer.create(**args) new_customer.save()
def search_customer(customer_id): """ Search for customer name in database :return: Customer info if Ture, empty dict if None :param customer_id: customer_id to search for """ find_customer_dict = {} LOGGER.info("Search for customer name in database...") try: find_customer = Customer.get(Customer.customer_id == customer_id) if find_customer: find_customer_dict = { "first_name": find_customer.first_name, "last_name": find_customer.last_name, "email_address": find_customer.email_address, "phone_number": find_customer.phone_number } LOGGER.info('%s %s is in the database. There phone # is %s', find_customer.first_name, find_customer.last_name, find_customer.phone_number) except pw.DoesNotExist: LOGGER.info('No customer with customer id of %s', Customer.customer_id) return find_customer_dict
def add_customer(customer_id, first_name, last_name, home_address, phone_number, email_address, status, credit_limit): """ Returns new customer with **kwargs info """ try: credit_limit = float(credit_limit) except ValueError as err: logging.error(err) raise try: with Customer.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, status=status, credit_limit=credit_limit) new_customer.save() LOGGER.info('Customer database add successful') LOGGER.info('%s %s has been created', Customer.first_name, Customer.last_name) except pw.IntegrityError: LOGGER.error( "Customer id %s, already exitsts in database. Error adding %s %S", customer_id, first_name, last_name) raise pw.IntegrityError
def delete_customer(customer_id): """ :param customer_id: :param stage: :return: """ customer = Customer.get(Customer.customer_id == customer_id) customer.delete_instance()
def list_active_customers(): """ :param stage: :return: """ results_dict = Customer.select().where(Customer.status == True).dicts() return results_dict
def list_active_customers(): """ Returns an integer with the number of customers whose status is currently active. """ customer_count = Customer.select().where(Customer.status == True).count() LOGGER.info('%s customers are currently active', customer_count) return customer_count
def delete_customer(id=None): delete_customer = Customer.get(Customer.id == id) logging.info( " Make sure that this customer will be deleted from our database") if delete_customer: delete_customer.delete_instance() delete_customer.save() else: logging.error(f"We could not delete {first_name} from the database") raise ValueError("Customer not found")
def update_customer_credit(id, credit_limit): update_custumer = Customer.get(Customer.id == id) if update_customer: update_customer.credit_limit = credit_limit update_customer.save() else: raise ValueError logging.info(error) logging.info(f"We could not update {first_name} from the database") database.close()
def search_customer(id=None): try: find_customer_model = Customer.get(Customer.id == id) except pw.DoesNotExist as err: logging.error(err) find_customer_dict = {} else: find_customer_dict = model_dictionary(find_customer_model) return find_customer_dict
def test_add_ok_customer(): customer_id = 'W3434' name = 'Tim' last_name = 'Pauley' home_address = '1234 Queen Anne Ave N Seattle, Wa 98019' phone_number = '206-123-4567' email_address = '*****@*****.**' status = True credit_limit = '40,0000' add_customer( customer_id ,name ,last_name ,home_address ,email_address ,status ,credit_limit ) Customer.get(Customer.customer_id=customer_id) assert(test_customer.email_address = email_address)
def update_customer_credit(customer_id, credit_limit): """ :param customer_id: :param credit_limit: :param stage: :return: """ customer = Customer.get(Customer.customer_id == customer_id) update = customer.update(credit_limit=credit_limit) update.execute()
def delete_customer(customer_id): try: acustomer = Customer.get(Customer.customer_id == customer_id) logging.info( " Make sure that this customer will be deleted from our database") acustomer.delete_instance() acustomer.save() except Exception as e: logging.info(e) return output_dict database.close()
def search_customer(customer_id): """ :param customer_id: :param stage: :return: """ try: results = Customer.get(Customer.customer_id == customer_id) except Exception: return False return results
def update_customer_credit(customer_id, credit_limit): """ Updates the customer credit limit :param cusomer_id: credit_limit to update new limit """ LOGGER.info("Updating customer credit limit") try: customer = Customer.get(Customer.customer_id == customer_id) customer.credit_limit = credit_limit customer.save() except pw.DoesNotExist: LOGGER.error("Customer_id of %s does not exist", customer_id)
def delete_customer(customer_id, stage='prod'): """ :param customer_id: :param stage: :return: """ if stage == 'prod': customer = Customer.get(cm.customer_id == customer_id) customer.delete_instance() if stage == 'dev': customer = TestCustomer.get(TestCustomer.customer_id == customer_id) customer.delete_instance()
def list_active_customers(stage='prod'): """ :param stage: :return: """ if stage == 'prod': results_dict = Customer.select().where(Customer.status is True).dicts() if stage == 'dev': results_dict = TestCustomer.select().where( TestCustomer.status is True).dicts() return results_dict
def search_customer(customer_id): output_dict = {} try: acustomer = Customer.get(Customer.customer_id == customer_id) logging.info(" A customer search from the database") output_dict['first_name'] = acustomer.first_name output_dict['last_name'] = acustomer.last_name output_dict['email_address'] = acustomer.email_address output_dict['phone_number'] = acustomer.phone_number acustomer.save() except Exception as e: logging.info(e) return output_dict database.close()
def delete_customer(customer_id): """ Deletes customer from database :return: empty dict if custome doesnt exist :param customer_id: customer_id to search and delete for """ LOGGER.info("Creating new customer named Tom and then deleting him") try: customer_remove = Customer.get(Customer.customer_id == customer_id) customer_remove.delete_instance() except pw.DoesNotExist: return None LOGGER.info("Customer successfully deleted in database")
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 update_customer_credit(customer_id, credit_limit, stage='prod'): """ :param customer_id: :param credit_limit: :param stage: :return: """ if stage == 'prod': customer = Customer.get(cm.customer_id == customer_id) update = customer.update(credit_limit=credit_limit) update.execute() if stage == 'dev': customer = TestCustomer.get(TestCustomer.customer_id == customer_id) update = customer.update(credit_limit=credit_limit) update.execute()
def search_customer(customer_id, stage='prod'): """ :param customer_id: :param stage: :return: """ try: if stage == 'prod': results = Customer.get(cm.customer_id == customer_id) if stage == 'dev': results = TestCustomer.get(TestCustomer.customer_id == customer_id) except Exception: return False return results
def add_customer(customer_id, first_name, last_name, home_address, phone_number, email_address, status, credit_limit): try: 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, status=status, credit_limit=credit_limit) logger.info("The new Customer is successfully added!") new_customer.save() except ValueError as e: logging.info(e) logging.info(f"Could not add {first_name} to db") database.close()
def return_all_customer_info(): """ Prints all customer info """ all_customer_records = Customer.select() for persion in all_customer_records: print( f"Customer id: {person.customer_id}\nFirst Name: {person.first_name}\nLast Name: {person.last_name}\n" f"Home Address: {person.home_address}\nPhone Number: {person.phone_number}\n" f"Email Address: {person.email_address}\nStatus: {person.status}\nCredit Limit: ${person.credit_limit}\n" ) if __name__ == "__main__": cc.main() search_customer("W3434fd") list_active_customers()
def list_active_customers(): """ :return: This function will return an integer with the number of customers whose status is currently active. """ try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') with database.transaction(): for customer in cls_Customer.select().where( cls_Customer.status == 1): logger.info( f'customers whose first name is {customer.first_name} current status is active' ) except Exception as e: logger.info('there could be no active ') logger.info(e) finally: logger.info('database closes') database.close()
def add_customer(id, first_name, last_name, home_address, phone_number, email_address, status, credit_limit): try: credit_limit = float(credit_limit) logger.info("The new Customer is successfully added!") except ValueError as err: logging.error(err) raise if not database.table_exists('Customer'): database.create_tables([Customer]) new_customer = Customer.create(id=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) new_customer.save() logging.info(f"Added {first_name} to database") database.close()
def add_customer(customer_id, first_name, last_name, home_address, phone_number, email_address, status, credit_limit): """ Returns new customer with **kwargs info """ try: credit_limit = float(credit_limit) except ValueError as err: logging.error(err) raise 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, status=status, credit_limit=credit_limit) new_customer.save() LOGGER.info('Customer database add successful') LOGGER.info('%s %s has been created', Customer.first_name, Customer.last_name)
'''This is to test the basic operations '''