Exemple #1
0
def delete_customer(customer_id):
    '''
    Delete a customer based on the customer ID
    '''
    LOGGER.info(f'Deleting customer {customer_id}...')
    Customer.delete().where(Customer.customer_id == customer_id).execute()
    LOGGER.info(f'Deleted customer {customer_id}')
Exemple #2
0
def add_customer(**kwargs):
    '''
    Add a new customer to the Customers database.
    '''
    Customer.create(customer_id=kwargs['customer_id'],
                    first_name=kwargs['first_name'],
                    last_name=kwargs['last_name'],
                    home_address=kwargs['home_address'],
                    phone_number=kwargs['phone_number'],
                    email_address=kwargs['email_address'],
                    status=kwargs['status'],
                    credit_limit=kwargs['credit_limit'])
Exemple #3
0
def list_active_customers():
    '''
    List count of active customers
    '''
    count = Customer.select().where(Customer.status == 1).count()
    LOGGER.info(f'There are {count} active customers.')
    return count
Exemple #4
0
def init_db(path=None):
    '''
    Populate the DB with customer info
    '''
    DATABASE.create_tables([Customer])

    LOGGER.info(f'Input data path: {path}')

    try:
        with open(path, 'r') as file:
            file_obj = iter(file)  # make file iterable explicitly
            next(file_obj)  # skip header line
            while True:
                try:
                    line = next(file_obj)
                    customer_data = line.rstrip('\n').split(',')
                    status = customer_data[6]
                    limit = Decimal(customer_data[7])

                    # convert status to boolean from string
                    if status.lower() == 'active':
                        status = 1
                    elif status.lower() == 'inactive':
                        status = 0
                    else:
                        LOGGER.warning(
                            f'Unknown state for customer: {customer_data}')
                        status = 0  # set to inactive

                    Customer.create(customer_id=customer_data[0],
                                    first_name=customer_data[1],
                                    last_name=customer_data[2],
                                    home_address=customer_data[3],
                                    phone_number=customer_data[4],
                                    email_address=customer_data[5],
                                    status=status,
                                    credit_limit=limit)
                    LOGGER.info(customer_data)
                except StopIteration:
                    break
    except FileNotFoundError:  # No data file provided, create 2 records in Customer table
        init_db_with_small_data()
Exemple #5
0
def init_db_with_small_data():
    '''
    Populate customer table using small data set
    '''
    Customer.create(customer_id='ca0001',
                    first_name='Kate',
                    last_name='Perry',
                    home_address='One Microsoft Way, Redmond, 98052',
                    phone_number='425-595-8068',
                    email_address='*****@*****.**',
                    status=0,
                    credit_limit=19818.98)
    Customer.create(customer_id='ca0002',
                    first_name='Eric',
                    last_name='McCarthy',
                    home_address='910 232nd PL NE, Sammamish, 98074',
                    phone_number='425-111-4567',
                    email_address='*****@*****.**',
                    status=1,
                    credit_limit=200.0)
Exemple #6
0
def init_db():
    '''
    Populate the DB with customer info
    '''
    DATABASE.create_tables([Customer])
    Customer.create(customer_id=random.randint(1, 1000),
                    first_name='Kate',
                    last_name='Perry',
                    home_address='One Microsoft Way, Redmond, 98052',
                    phone_number='425-595-8068',
                    email_address='*****@*****.**',
                    status=0,
                    credit_limit=19818.98)
    Customer.create(customer_id=random.randint(1, 1000),
                    first_name='Eric',
                    last_name='McCarthy',
                    home_address='910 232nd PL NE, Sammamish, 98074',
                    phone_number='425-111-4567',
                    email_address='*****@*****.**',
                    status=1,
                    credit_limit=200.0)
Exemple #7
0
def update_customer_credit(customer_id, new_credit_limit):
    '''
    Update a customer's credit limit
    '''
    LOGGER.info(
        f'{customer_id} needs to increase credit_limit to {new_credit_limit}')
    customer = Customer.select().where(
        Customer.customer_id == customer_id).get()

    if customer:
        LOGGER.info(
            f'Old credit_limit {customer.credit_limit}; new credit_limit {new_credit_limit}'
        )
        # customer.new_credit_limit = new_credit_limit
        # customer.save()

        Customer.update(credit_limit=new_credit_limit).where(
            Customer.customer_id == customer_id).execute()

    else:
        LOGGER.warning(f'Not customer found for ID {customer_id}')
        raise ValueError

    LOGGER.info(f'Update credit successfully to {customer.credit_limit}')
Exemple #8
0
def search_customer(customer_id):
    '''
    Search for a customer based on customer ID
    '''
    customer_dict = {}

    try:
        customer = Customer.select().where(
            Customer.customer_id == customer_id).get()

        LOGGER.info(f'Found customer_id: {customer.customer_id}')

        if customer:
            customer_dict['customer_id'] = customer.customer_id
            customer_dict['first_name'] = customer.first_name
            customer_dict['last_name'] = customer.last_name
            customer_dict['phone_number'] = customer.phone_number
            customer_dict['email'] = customer.email_address

        LOGGER.info(f'len is {len(customer_dict)}')
    except Customer.DoesNotExist:
        LOGGER.warning(f'Customer with ID {customer_id} does not exist!!!')

    return customer_dict
Exemple #9
0
def clean_up_db():
    '''
    Clean up the customer db
    '''
    Customer.drop_table()