Ejemplo n.º 1
0
def print_data_view(key: str) -> None:
    data = get_cats(DbController.instance().get_all_rows(key, '*'))

    is_looping = True
    while is_looping:
        clear()
        dicts_to_table(data, paginate=True)
        index = get_validated_input('Please Select 1 To Sort: ',
                                    int,
                                    fg='Blue',
                                    cancel_on='0',
                                    is_present=[1])

        if index == False:
            is_looping = False
            continue

        if index == 1:
            clear()
            # Get a list of the current column names to be used in a sort order
            sort_on_keys = [key for key in data[0]]
            list_to_table(sort_on_keys, 'Available Sorts', enumerate=True)
            sort_key = get_validated_input(
                'How Would You Like To Sort By [id]? ',
                int,
                fg='Blue',
                min_value=1,
                max_value=len(sort_on_keys),
                cancel_on=0)
            # Refresh data with selected sort order
            data = get_cats(DbController.instance().get_all_rows(
                key, '*', order=f'ORDER BY {sort_key}'))
            continue
Ejemplo n.º 2
0
    def test_should_apply_float_max_value(self, mock_input):
        #Given
        mock_input.side_effect = [2.5, 1.5]

        #When
        get_validated_input('Test', float, max_value=1.5)

        #Then
        self.assertEqual(mock_input.call_count, 2)
Ejemplo n.º 3
0
    def test_should_apply_is_min_length(self, mock_input):
        #Given
        mock_input.side_effect = ['123', '1234']

        #When
        get_validated_input('Test', str, min_length=4)

        #Then
        self.assertEqual(mock_input.call_count, 2)
Ejemplo n.º 4
0
    def test_should_apply_int_min_value(self, mock_input):
        #Given
        mock_input.side_effect = [1, 2]

        #When
        get_validated_input('Test', int, min_value=2)

        #Then
        self.assertEqual(mock_input.call_count, 2)
Ejemplo n.º 5
0
    def test_should_apply_is_present(self, mock_input):
        #Given
        lst = ['not unique']
        mock_input.side_effect = ['Unique', 'Not Unique']

        #When
        get_validated_input('Test', str, is_present=lst)

        #Then
        self.assertEqual(mock_input.call_count, 2)
Ejemplo n.º 6
0
    def test_should_loop_until_valid_input(self, mock_input):
        # Given
        mock_input.side_effect = ['string1', 'string2', '1.99']
        expected = 3

        # When
        get_validated_input('Prompt', float)

        # Then
        self.assertEqual(expected, mock_input.call_count)
Ejemplo n.º 7
0
    def test_should_apply_unique(self, mock_input):
        #Given
        unique = ['not unique']
        mock_input.side_effect = ['Not Unique', 'Unique']

        #When
        get_validated_input('Test', str, unique=unique)

        #Then
        self.assertEqual(mock_input.call_count, 2)
Ejemplo n.º 8
0
def show_update_status_menu() -> None:
    data = get_order_data()
    current_ids = [order['id'] for order in data]

    is_looping = True
    while is_looping:
        clear()
        dicts_to_table(data)

        index = get_validated_input('Please Select An Id To Update: ',
                                    int,
                                    fg='Blue',
                                    cancel_on='0',
                                    is_present=current_ids)
        if not index:
            is_looping = False
            continue

        status_list = DbController.instance().get_all_rows(
            'status', 'id, code')
        current_row = DbController.instance().get_rows_where(
            'orders', '*', 'id', index)[0]

        clear()
        dicts_to_table(data)
        print(fmt_string(f'\nUpdating Order {index}...', fg='Cyan'))
        dicts_to_table(status_list)

        status_index = get_validated_input(f'Please Select A New Status: ',
                                           int,
                                           fg='Blue',
                                           min_length=0,
                                           max_value=len(status_list),
                                           min_value=1,
                                           cancel_on='0')
        if not status_index:
            continue

        current_row['status'] = status_index
        DbController.instance().update('orders', index, current_row)
        data = get_order_data()

        clear()
        dicts_to_table(data)

        if input(
                fmt_string(
                    'Status SuccessFully Updated. Would You Like To Update Another?[y/n]\n',
                    fg='Green')) == 'n':
            is_looping = False
            continue
Ejemplo n.º 9
0
def show_delete_item_menu(get_key: str) -> None:
    data = get_cats(DbController.instance().get_all_rows(get_key, '*'),
                    action='delete')

    current_ids = [item['id'] for item in data]

    is_looping = True
    while is_looping:
        clear()
        dicts_to_table(data)
        item_id = get_validated_input(f'Please Enter An ID To Delete: ',
                                      int,
                                      fg='Blue',
                                      min_length=1,
                                      is_present=current_ids,
                                      cancel_on='0')
        if not item_id:
            return

        DbController.instance().delete(get_key, item_id)

        clear()
        data = get_cats(DbController.instance().get_all_rows(get_key, '*'),
                        action='delete')
        dicts_to_table(data)

        if input(
                fmt_string(
                    'Item SuccessFully Added. Would You Like To Add Another?[y/n]\n',
                    fg='Green')) == 'n':
            is_looping = False
Ejemplo n.º 10
0
def show_delete_order_menu() -> None:
    data = get_order_data()

    current_ids = [item['id'] for item in data]

    is_looping = True
    while is_looping:
        clear()
        dicts_to_table(data)

        id = get_validated_input('Please Enter An ID To Delete: ',
                                 int,
                                 fg='Blue',
                                 min_length=1,
                                 is_present=current_ids,
                                 cancel_on='0')

        if not id:
            return

        DbController.instance().delete('orders', id)

        data = get_order_data()

        clear()
        dicts_to_table(data)

        if input(
                fmt_string(
                    'Item SuccessFully Updated. Would You Like To Update Another?[y/n]\n',
                    fg='Green')) == 'n':
            is_looping = False
Ejemplo n.º 11
0
    def test_should_return_str_as_float(self):
        # Given
        expected = 1.99

        # When
        actual = get_validated_input('Prompt', float)

        # Then
        self.assertEqual(expected, actual)
Ejemplo n.º 12
0
def print_order_view():
    is_looping = True
    data = get_order_data()

    # Get ID's of current orders to ensure that a valid one is selected
    current_ids = [
        item['id']
        for item in DbController.instance().get_column('orders', 'id')
    ]

    while is_looping:
        clear()
        dicts_to_table(data, paginate=True)
        index = get_validated_input(
            'Please Select An Id To View (-1 To Sort): ',
            int,
            fg='Blue',
            cancel_on='0',
            is_present=current_ids + [-1])
        if index == False:
            is_looping = False
            continue

        if index == -1:
            clear()
            dicts_to_table(data, paginate=False)
            # Get a list of the current coloum names to be used in a sort order
            sort_on_keys = list(data[0].keys())
            list_to_table(sort_on_keys, 'Available Sorts', enumerate=True)
            sort_key = get_validated_input(
                'What Would You Like To Sort By [id]? ',
                int,
                fg='Blue',
                min_value=1,
                max_value=len(sort_on_keys),
                cancel_on=0)
            # Status sorts on id not named string so set this if status is selected
            data = get_order_data(sort_key if sort_key != 7 else 'o.status')
            continue

        for order in data:
            if order['id'] == index:
                clear()
                show_order_detail_menu(order)
Ejemplo n.º 13
0
    def test_should_escape_float(self, mock_input):
        #Given
        mock_input.return_value = 'Name."\\,.,.'
        expected = 'name'

        #When
        actual = get_validated_input('Test', str)

        #Then
        self.assertEqual(expected, actual)
Ejemplo n.º 14
0
def view_procs():
    clear()
    views = DbController.instance().get_available_procs()
    list_to_table(views, 'Views', enumerate=True)
    action = get_validated_input('Select A Item To Preview: ',
                                 int,
                                 fg='Green',
                                 min_value=1,
                                 max_value=len(views),
                                 cancel_on='0')

    if not action:
        return

    dicts_to_table(DbController.instance().call_proc(views[action - 1].replace(
        ' ', '_')))

    input(fmt_string('Press Enter To Continue', fg='Green'))
Ejemplo n.º 15
0
def view_logs():
    clear()
    list_to_table([item for item in reversed(LOG_LEVELS.keys())],
                  'Levels',
                  enumerate=True)
    log_level = get_validated_input('Please Select A Log Level: \n',
                                    int,
                                    fg='Blue',
                                    is_present=[1, 2, 3, 4, 5])

    data = []
    try:
        with open('./data/log.log', 'r') as log_file:
            data = log_file.readlines()

    except Exception as err:
        log('critical', str(err))

    rows = []
    for row in data:
        temp_row = {}
        temp_split = row.split(' @ ')
        t = datetime.strptime(temp_split[0], '%Y-%m-%d %H:%M:%S.%f')
        temp_row['time'] = t.strftime('%Y-%m-%d %H:%M')
        temp_rest = temp_split[1]
        rest_split = temp_rest.split(': ')
        temp_row['type'] = rest_split[0].lower()
        msg = rest_split[1].strip()

        max_length = int(os.get_terminal_size().columns * 0.65)
        wrapper = textwrap.TextWrapper(width=max_length)
        wrapped = '\n'.join(wrapper.wrap(msg))
        temp_row['message'] = wrapped

        if LOG_LEVELS[temp_row['type']] >= log_level:
            rows.append(temp_row)

    dicts_to_table(rows, enumerated=True)
    input()
Ejemplo n.º 16
0
def show_update_item_menu(get_key: str) -> None:
    data = get_cats(DbController.instance().get_all_rows(get_key, '*'),
                    action='delete')

    current_ids = [item['id'] for item in data]

    schema = {k.lower(): type(v) for (k, v) in data[0].items()}
    validation = {'all': {'min_length': 1, 'cancel_on': '0', 'fg': 'Blue'}}

    is_looping = True
    while is_looping:
        clear()
        dicts_to_table(data)

        id = get_validated_input('Please Enter An ID To Edit: ',
                                 int,
                                 fg='Blue',
                                 min_length=1,
                                 is_present=current_ids,
                                 cancel_on='0')
        if not id:
            return

        new_dict = dict_builder(schema, ['id', 'items'], validation=validation)
        if not new_dict:
            return

        DbController.instance().update(get_key, id, new_dict)
        clear()
        data = get_cats(DbController.instance().get_all_rows(get_key, '*'),
                        action='delete')
        dicts_to_table(data)

        if input(
                fmt_string(
                    'Item SuccessFully Added. Would You Like To Add Another?[y/n]\n',
                    fg='Green')) == 'n':
            is_looping = False
Ejemplo n.º 17
0
def search_table(table: str):
    clear()
    term = get_validated_input('Please Enter A Search Term: ', fg='Blue')

    if table == 'orders':
        data = DbController.instance().search_joined_table(
            table='orders o',
            term=term,
            fields=[
                'o.id', 'o.name', 'o.address', 'o.area', 'o.phone',
                'courier.name AS Courier', 's.code AS status'
            ],
            targets=['couriers courier', 'status s'],
            conditions=['courier.id = o.courier', 's.id = o.status'])
    else:
        data = get_cats(DbController.instance().search_table(table, term),
                        action='join')

    if len(data) > 0:
        dicts_to_table(data)
    else:
        print(fmt_string('No Results Found', fg='White', bg='Red'))
    input(fmt_string('Press Enter To Continue', fg='Green'))
Ejemplo n.º 18
0
def update_catagory_mapping():
    is_in_cat = True
    while is_in_cat:
        cats = DbController.instance().get_all_rows('catagories', '*')
        cat_ids = [item['id'] for item in cats]
        clear()
        dicts_to_table(cats)

        selected_catagory = get_validated_input(
            'Please Select A Catagory To Update: ',
            int,
            fg='Blue',
            is_present=cat_ids,
            cancel_on='0')

        if not selected_catagory:
            is_in_cat = False
            continue

        is_editing = True
        while is_editing:
            data = DbController.instance().get_rows_where(
                'products', 'name, id', 'catagory', selected_catagory)
            product_ids = [
                item['id'] for item in DbController.instance().get_all_rows(
                    'products', 'id')
            ]
            list_names = ['*' + item['name'] for item in data]
            list_ids = [item['id'] for item in data]

            clear()
            list_to_table(list_names, "Current Menu", max_length=3)

            dicts_to_table(get_cats(DbController.instance().get_all_rows(
                'products', '*')),
                           paginate=True,
                           page_length=10,
                           on_clear=lambda: list_to_table(
                               list_names, "Current Menu", max_length=3))

            selected_item = get_validated_input(
                'Please Select An Item To Add: ',
                int,
                fg='Blue',
                is_present=product_ids,
                cancel_on='0',
                cancel_text='GO BACK')

            if not selected_item:
                is_editing = False
                continue

            if selected_catagory == 9 and selected_item in list_ids:
                input(
                    fmt_string(
                        'That Item Is Already In This Menu, But You Can Not Remove From The Default Menu',
                        fg='White',
                        bg='Red'))
                continue

            elif selected_item in list_ids:
                if input(
                        fmt_string(
                            'That Item Is Already In This Menu. Would You Like To Remove It [y/n]? \n',
                            fg='White',
                            bg='Red')) == 'y':
                    DbController.instance().update('products', selected_item,
                                                   {'catagory': 9})

            else:
                DbController.instance().update('products', selected_item,
                                               {'catagory': selected_catagory})
Ejemplo n.º 19
0
def select_order_items(order_id) -> None:
    current_basket = list(DbController.instance().get_joins_where(
        source='basket b',
        fields=['p.id', 'p.name', 'b.quantity'],
        targets=['products p'],
        conditions=['b.item = p.id'],
        where=f'b.order_id = {order_id}'))

    current_rows = DbController.instance().get_rows_where(
        'basket', '*', 'order_id', order_id)
    current_ids = [item['item'] for item in current_rows]

    catagories = DbController.instance().get_all_rows('catagories', '*')
    catagory_ids = [cat['id'] for cat in catagories]

    to_update = []
    to_insert = []
    to_delete = []

    is_in_cat = True
    while is_in_cat:
        clear()
        if len(current_basket) > 0:
            dicts_to_table(current_basket)
        print(fmt_string(f'Updating Basket For Order {order_id}...',
                         fg='Cyan'))
        dicts_to_table(catagories)

        catagory = get_validated_input('Please Select A Catagory: ',
                                       int,
                                       fg='Blue',
                                       is_present=catagory_ids,
                                       cancel_on='0',
                                       cancel_text='SKIP')

        if not catagory:
            is_in_cat = False
            continue

        is_in_product = True
        while (is_in_product):
            clear()

            # Reconcile duplicate records -> only affects locally
            if len(current_basket) > 0:
                for i in range(len(current_basket) - 1):
                    for j in range(i + 1, len(current_basket)):
                        if current_basket[i]['id'] == current_basket[j]['id']:
                            current_basket[i]['quantity'] += current_basket[j][
                                'quantity']
                            del current_basket[j]

                    if current_basket[i]['quantity'] <= 0:
                        del current_basket[i]

                dicts_to_table(current_basket)

            products = DbController.instance().get_rows_where(
                'products', 'id, name', 'catagory', catagory)
            product_ids = [item['id'] for item in products]

            print(
                fmt_string(f'Updating Basket For Order {order_id}...',
                           fg='Cyan'))
            dicts_to_table(products)

            product = get_validated_input('Please Select An Item: ',
                                          int,
                                          fg='Blue',
                                          is_present=product_ids,
                                          cancel_on='0',
                                          cancel_text='GO BACK')

            if not product:
                is_in_product = False
                continue

            quantity = get_validated_input('Please Enter A Quantity: ',
                                           int,
                                           fg='Blue',
                                           cancel_on='0',
                                           cancel_text='GO BACK')

            if product in current_ids:
                for item in current_rows:
                    if item['item'] == product:
                        if item['quantity'] + quantity <= 0:
                            to_delete.append(item)
                            current_rows.remove(item)

                            for row in current_basket:
                                print(row['id'], product)
                                if row['id'] == product:
                                    current_basket.remove(row)

                        else:
                            item['quantity'] += quantity
                            to_update.append(item)

                            for row in current_basket:
                                if row['id'] == product:
                                    row['quantity'] += quantity

            else:
                to_insert.append({
                    'order_id': order_id,
                    'item': product,
                    'quantity': quantity
                })
                for row in products:
                    if row['id'] == product:
                        current_basket.append({
                            'id': product,
                            'name': row['name'],
                            'quantity': quantity
                        })

            # reconcile updates to additions -> would affect table
            for i in range(len(to_insert) - 1):
                for j in range(i + 1, len(to_insert)):
                    if to_insert[i]['item'] == to_insert[j]['item']:
                        to_insert[i]['quantity'] += to_insert[j]['quantity']
                        del to_insert[j]

                if to_insert[i]['quantity'] <= 0:
                    del to_insert[i]

    for record in to_update:
        DbController.instance().update_where('basket', ['order_id', 'item'],
                                             [order_id, record['item']],
                                             record)

    for record in to_delete:
        DbController.instance().delete_where('basket', ['order_id', 'item'],
                                             [order_id, record['item']])

    for record in to_insert:
        DbController.instance().insert('basket', record)
Ejemplo n.º 20
0
def show_update_order_menu() -> None:
    data = get_order_data()
    current_ids = [item['id'] for item in data]
    status_list = DbController.instance().get_all_rows('status', '*')
    status_ids = [status['id'] for status in status_list]
    courier_list = DbController.instance().get_all_rows('couriers', '*')
    courier_ids = [courier['id'] for courier in courier_list]

    is_looping = True
    while is_looping:
        clear()
        dicts_to_table(data)
        id = get_validated_input('Please Enter An ID To Edit: ',
                                 int,
                                 fg='Blue',
                                 min_length=1,
                                 is_present=current_ids,
                                 cancel_on='0')
        if not id:
            return

        order = DbController.instance().get_joins_where(
            fields=[
                'o.id', 'o.name', 'o.address', 'o.area', 'o.phone',
                'courier.name AS courier', 's.code AS status'
            ],
            source='orders o',
            targets=['couriers courier', 'status s'],
            conditions=['courier.id = o.courier', 's.id = o.status'],
            where=f'o.id = {id}',
            order='ORDER BY o.status')

        clear()
        dicts_to_table(order)

        schema = {k.lower(): type(v) for (k, v) in data[0].items()}
        schema['courier'] = int
        schema['status'] = int
        validation = {
            'all': {
                'min_length': 0,
                'cancel_on': '',
                'cancel_text': 'SKIP',
                'fg': 'Blue'
            },
            'status': {
                'is_present': status_ids,
                'cancel_on': '0'
            },
            'courier': {
                'is_present': courier_ids,
                'cancel_on': '0'
            }
        }
        on_key = {
            'status': [
                clear, lambda: dicts_to_table(order),
                lambda: dicts_to_table(status_list)
            ],
            'courier': [
                clear, lambda: dicts_to_table(order),
                lambda: dicts_to_table(courier_list)
            ]
        }

        new_dict = dict_builder(schema, ['id'],
                                validation,
                                on_key=on_key,
                                on_cancel='skip')

        if new_dict:
            DbController.instance().update('orders', id, new_dict)

        select_order_items(id)

        clear()
        data = get_order_data()
        dicts_to_table(data)

        if input(
                fmt_string(
                    'Item SuccessFully Updated. Would You Like To Update Another?[y/n]\n',
                    fg='Green')) == 'n':
            is_looping = False