def db_init():
    """
    Function to initialize DB, create
    and add tables
    """
    DB.init('customer.db')
    DB.create_tables([Customer])
    add_customer(CUSTOMER_LIST)
示例#2
0
def setup_db():
    """ Sets up database for test cases """
    DB.drop_tables([Customer])
    #    DB.close()
    DB.create_tables([Customer])
    add_customer(1, "Leo", "Messi", "100 Las Ramblas", 1234567890,
                 "*****@*****.**", True, 5000000.00)
    add_customer(2, "Ivan", "Rakitic", "111 Las Ramblas", 2345678901,
                 "*****@*****.**", True, 2000000.00)
    add_customer(3, "Gerard", "Pique", "123 Las Ramblas", 3333333333,
                 "*****@*****.**", True, 300000.00)
示例#3
0
def db_init():
    """
    Function to initialize DB, create/add
    tables and add customer data to the DB
    """
    DB.init('customer.db')
    DB.create_tables([Customer])
    [
        add_customer(customer) for customer in CUSTOMER_LIST
        if customer is not None
    ]
示例#4
0
def add_customer(customer_id, name, lastname, home_address, phone_number,
                 email_address, status, credit_limit):
    """
        Adds a new customer to the database
        returns:
    """
    logging.info("attempting to add customer: %s %s...", name, lastname)
    try:
        with DB.transaction():
            # add new customer entry
            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)

            # save new customer entry in database
            new_customer.save()
            LOGGER.info("successfully saved new customer %s %s!", name,
                        lastname)

    except IntegrityError:
        LOGGER.error("IntegrityError occurred while creating customer %s %s!",
                     name, lastname)
示例#5
0
def search_customer(customer_id):
    """
        Searches customer by ID
        returns: dict; with name, lastname, email, phone
                 (or)
                 empty if no customer found
    """
    logging.info("searching customer ID %s...", customer_id)
    try:
        with DB.transaction():
            # query database for customer by ID
            searched_customer = Customer.get(
                Customer.customer_id == customer_id)
            logging.info("successfully found customer!")
            # return dict with name, lastname, email, phone number
            return {
                "name": searched_customer.name,
                "lastname": searched_customer.lastname,
                "email_address": searched_customer.email_address,
                "phone_number": searched_customer.phone_number
            }

    except DoesNotExist:
        LOGGER.error("DoesNotExisterror: customer ID %s not found!",
                     customer_id)
        # return empty dict
        return {}
示例#6
0
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)
示例#7
0
def delete_customer(customer_id):
    '''delete a customer by id.'''
    try:
        with DB.atomic():
            delete_query = Customer.delete().where(Customer.customer_id == customer_id)
            delete_query.execute()
    except IntegrityError:
        LOGGER.warning("Customer ID %s not found.", customer_id)
def delete_customer(delete_name):
    '''delete a customer by id.'''
    try:
        with DB.atomic():
            Customer.delete().where(Customer.name == delete_name).execute()
            result = f'Succesfully deleted {delete_name}'
    except IntegrityError:
        result = f'{delete_name} does not exist'
    return result
示例#9
0
def update_customer_credit(customer_id, credit_limit):
    """Update an existing customer's credit limit"""
    try:
        with DB.atomic():
            customer = Customer.get(Customer.customer_id == customer_id)
            customer.credit_limit = credit_limit
            customer.save()
    except pw.DoesNotExist:
        LOGGER.error("Failed to update ID '%s': not found in database",
                     customer_id)
        raise ValueError
示例#10
0
def add_customer(name, lastname, home_address, phone_number, email_address,
                 status, poverty_score):
    '''atomic() is easier than transaction()'''
    try:
        with DB.atomic():
            Customer.create(name=name,
                            lastname=lastname,
                            home_address=home_address,
                            phone_number=phone_number,
                            email_address=email_address,
                            status=status,
                            credit_limit=poverty_score)
    except IntegrityError:
        LOGGER.warning("Name %s is already taken.", name)
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)
        LOGGER.info("Adding customer %s to database.", customer_id)
    except IntegrityError:
        LOGGER.warning("Customer ID %s is already taken.", customer_id)
def update_customer_credit(customer_id: int, credit_limit: float):
    """
    Updates an existing customer's credit limit. Raises a ValueError if no such
    customer exists.

    :param customer_id: int, customer ID
    :param credit_limit: float, new credit limit
    """

    try:
        res = _get_by_id(customer_id)
    except pw.DoesNotExist:
        raise ValueError(f"No customer with ID {customer_id}")
    else:
        with DB.transaction():
            res.credit_limit = credit_limit
            res.save()
示例#13
0
def delete_customer(customer_id):
    """
        Deletes a customer from the database
        returns:
    """
    logging.info("attempting to delete customer ID %s...", customer_id)
    try:
        with DB.transaction():
            # query database for customer by ID
            to_delete = Customer.get(Customer.customer_id == customer_id)
            # delete the customer found
            to_delete.delete_instance()
            logging.info("successfully deleted customer!")

    except DoesNotExist:
        LOGGER.error("DoesNotExisterror: customer ID %s delete failed!",
                     customer_id)
示例#14
0
def update_customer_credit(customer_id, credit_limit):
    """
        Searches customer by ID and updates credit limit
        returns:
    """
    logging.info("attempting to update customer ID %s with limit: %s...",
                 customer_id, credit_limit)
    try:
        with DB.transaction():
            # query database for customer by ID
            update_customer = Customer.get(Customer.customer_id == customer_id)
            # update credit limit
            update_customer.credit_limit = credit_limit
            update_customer.save()
            logging.info("successfully updated credit limit!")
    except DoesNotExist:
        LOGGER.error("error: customer ID %s update failed!", customer_id)
        # if customer does not exist, raise ValueError
        raise ValueError
示例#15
0
def add_customer(customer):
    """
    This function will add new custome to Customer table
    in customer.db
    """
    try:
        with DB.transaction():
            logging.info('Adding \"%s\" as new customer',
                         (customer[NAME], customer[LASTNAME]))
            new_customer = Customer.create(name=customer[NAME],
                                           lastname=customer[LASTNAME],
                                           home_address=customer[ADDRESS],
                                           phone_number=customer[PHONE],
                                           email_address=customer[EMAIL],
                                           status=customer[STATUS],
                                           credit_limit=customer[LIMIT])
            logging.debug('New Customer : \"%s \" added to the database',\
                        (customer[NAME], customer[LASTNAME]))
            new_customer.save()
    except peewee.IntegrityError as exp:
        logging.debug('IntegrityError, Error adding customer %s', exp)
示例#16
0
def add_customer(customer_id, name, lastname, home_address, phone_number,
                 email_address, status, credit_limit):
    #pylint: disable=too-many-arguments
    '''
    This function adds a customer and its data to the customer database
    '''
    logging.debug('Attempting to add customer %s to database', customer_id)
    with DB.atomic():
        try:
            added_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)
            added_customer.save()
            logging.debug('Successfully added customer %s to database',
                          customer_id)
        except p.DatabaseError:
            logging.error('Could not add customer %s to database', customer_id)
示例#17
0
 def tearDown(self):
     Customer.delete().execute()
     DB.close()
示例#18
0
 def setUp(self):
     DB.create_tables([Customer])
"""Creates a customer database with peewee ORM, sqlite and python"""
import logging
from customer_model import Customer, DB

logging.basicConfig(level=logging.INFO)

logging.info("Creating the Customer table")
DB.create_tables([Customer])
logging.info("Closing the customer database")
#DB.close()
示例#20
0
def setup_db():
    """ Sets up database for test cases """
    DB.drop_tables([Customer])
    DB.close()
    DB.create_tables([Customer])
    DB.close()