def __getItems(self): itemsMenu = CUI(self.instance.__name__ + 's') self.itemsCurrentMenu[0] = itemsMenu try: if self.page < math.ceil( self.Controller.getCount() / self.per_page): itemsMenu.addField( 'NEXT', lambda: self.__changePageParams( self.page + 1, self.per_page)) if self.page > 1: itemsMenu.addField( 'PREV', lambda: self.__changePageParams( self.page - 1, self.per_page)) entities = self.Controller.getRange(self.page, self.per_page) for entity in entities: if 'name' in self.Controller.getModelKeys(): itemsMenu.addField(f"<{entity.id}> {entity.name}", lambda id=entity.id: self.__getItem(id)) else: itemsMenu.addField(f"<{entity.id}>", lambda id=entity.id: self.__getItem(id)) except Exception as err: itemsMenu.setError(str(err)) itemsMenu.run('Return to main menu')
def __getClientOrders(self): searchMenu = CUI('Client Orders') self.currentMenu = searchMenu try: client_id = int(input('Enter client_id: ')) if not (isinstance(client_id, int) and client_id > 0): raise Exception('Invalid input') startTime = time.time() allRecords = self.searchController.getAllClientOrders(client_id) endTime = time.time() searchMenu.setError('\nElapsed time: ' + str(endTime - startTime)[:9] + 's' '\nRows num: ' + str(len(allRecords))) searchMenu.addField('<Order id> | transaction date | taxes_sum') for record in allRecords: searchMenu.addField(f"<{record[0]}> {record[1]} {record[2]}") except Exception as err: searchMenu.setError(str(err)) searchMenu.run('Return to prev menu')
def __getCategoryProducts(self): searchMenu = CUI('Cayegory Products') self.currentMenu = searchMenu try: category_id = int(input('Enter category_id: ')) if not (isinstance(category_id, int) and category_id > 0): raise Exception('Invalid input') startTime = time.time() allRecords = self.searchController.getAllCategoryProducts( category_id) endTime = time.time() searchMenu.setError('\nElapsed time: ' + str(endTime - startTime)[:9] + 's' '\nRows num: ' + str(len(allRecords))) searchMenu.addField( '<Product id> | Product name | Product cost | Manufacturer') for record in allRecords: searchMenu.addField( f"<{record[0]}> {record[1]} {record[2]} {record[3]}") except Exception as err: searchMenu.setError(str(err)) searchMenu.run('Return to prev menu')
def __getItem(self, id: int): itemMenu = CUI(f'{self.instance.__name__} menu') self.itemsCurrentMenu[1] = itemMenu try: item = self.Controller.getById(id) for (key, value ) in self.Controller.getModelEntityMappedKeys(item).items(): itemMenu.addField(str(key) + ' : ' + str(value)) itemMenu.addField('DELETE', lambda: self.__deleteItem(item.id)) itemMenu.addField('UPDATE', lambda: self.__updateItem(item)) itemMenu.addField('Return to prev menu', lambda: self.__supportCUIFunc()) except Exception as err: itemMenu.setError(str(err)) itemMenu.run(False)
def __getOrder(self, id: int): orderMenu = CUI('Order menu') self.currentMenu[1] = orderMenu try: order: Order = self.orderController.getById(id) values = order.getValues().split(',') keys = order.getKeys().split(',') for i in range(len(keys)): orderMenu.addField(keys[i] + ' : ' + values[i]) orderMenu.addField('DELETE', lambda: self.__deleteOrder(order.id)) orderMenu.addField('UPDATE', lambda: self.__updateOrder(order.id)) orderMenu.addField('Return to prev menu', lambda: self.__supportCUIFunc()) except Exception as err: orderMenu.setError(str(err)) orderMenu.run(False)
def __getClient(self, id: int): clientMenu = CUI('Client menu') self.currentMenu[1] = clientMenu try: client: Client = self.clientController.getById(id) values = client.getValues().split(',') keys = client.getKeys().split(',') for i in range(len(keys)): clientMenu.addField(keys[i] + ' : ' + values[i]) clientMenu.addField('DELETE', lambda: self.__deleteClient(client.id)) clientMenu.addField('UPDATE', lambda: self.__updateClient(client.id)) clientMenu.addField('Return to prev menu', lambda: self.__supportCUIFunc()) except Exception as err: clientMenu.setError(str(err)) clientMenu.run(False)
def __getProductsByCostRange(self): searchMenu = CUI('Products') self.currentMenu = searchMenu try: if self.min is None or self.max is None: self.min = int(input('Enter min value: ')) self.max = int(input('Enter max value: ')) if not (isinstance(self.min, int) and isinstance(self.max, int) and self.max > 0 and self.min > 0 and self.max >= self.min): raise Exception('Invalid input') startTime = time.time() allRecords = self.searchController.getProductsByCostRange( self.min, self.max) endTime = time.time() searchMenu.setError('\nElapsed time: ' + str(endTime - startTime)[:9] + 's' '\nRows num: ' + str(len(allRecords))) if self.page < math.ceil(len(allRecords) / self.per_page): searchMenu.addField( 'NEXT', lambda: self.__changePageParams( self.page + 1, self.per_page)) if self.page > 1: searchMenu.addField( 'PREV', lambda: self.__changePageParams( self.page - 1, self.per_page)) searchMenu.addField( '<ID> | Product name | Product cost | Category name') for record in self.searchController.getProductsByCostRange( self.min, self.max, self.page, self.per_page): searchMenu.addField( f"<{record[0]}> {record[1]} {record[2]} {record[3]}") except Exception as err: searchMenu.setError(str(err)) searchMenu.addField('Return to prev menu', lambda: self.__exitMenu()) searchMenu.run(False)
def __getClients(self): clientsMenu = CUI('Clients') self.currentMenu[0] = clientsMenu try: if self.page < math.ceil( self.clientController.getCount() / self.per_page): clientsMenu.addField( 'NEXT', lambda: self.__changePageParams( self.page + 1, self.per_page)) if self.page > 1: clientsMenu.addField( 'PREV', lambda: self.__changePageParams( self.page - 1, self.per_page)) clients = self.clientController.getAll(self.page, self.per_page) for client in clients: clientsMenu.addField(f"<{client.id}> {client.name}", lambda id=client.id: self.__getClient(id)) except Exception as err: clientsMenu.setError(str(err)) clientsMenu.run('Return to main menu')
def __getOrders(self): ordersMenu = CUI('Order') self.currentMenu[0] = ordersMenu try: if self.page < math.ceil( self.orderController.getCount() / self.per_page): ordersMenu.addField( 'NEXT', lambda: self.__changePageParams( self.page + 1, self.per_page)) if self.page > 1: ordersMenu.addField( 'PREV', lambda: self.__changePageParams( self.page - 1, self.per_page)) orders = self.orderController.getAll(self.page, self.per_page) for order in orders: ordersMenu.addField(f"<{order.id}> {order.transaction_date}", lambda id=order.id: self.__getOrder(id)) except Exception as err: ordersMenu.setError(str(err)) ordersMenu.run('Return to main menu')
class ClientView: def __init__(self): self.currentMenu = [None, None] self.page = 1 self.per_page = 10 self.CUI = CUI("Client model menu") self.clientController = ClientController() self.CUI.addField('Add Client', lambda: self.__addClient()) self.CUI.addField('Generate rows', lambda: self.__generateRows()) self.CUI.addField('Clients', lambda: self.__getClients()) def run(self): self.CUI.run() def __generateRows(self): try: rowsNum = int(input('Enter rows num: ')) if not (isinstance(rowsNum, int) and rowsNum > 0): raise Exception('Invalid input') self.CUI.setError(' Please wait! Rows are generating... ') time = self.clientController.generateRows(rowsNum) self.CUI.setError(' Rows generated! Elapsed time: ' + time) except Exception as error: self.CUI.setError(str(error)) def __addClient(self): try: result = self.clientController.add() if isinstance(result, bool) and not result: raise Exception('Inccorect values') else: self.CUI.setError('New Client id: ' + str(result)) except Exception as err: self.CUI.setError(str(err)) def __changePageParams(self, page: int, per_page: int): self.page = page self.per_page = per_page self.currentMenu[0].stop() self.__getClients() def __getClients(self): clientsMenu = CUI('Clients') self.currentMenu[0] = clientsMenu try: if self.page < math.ceil( self.clientController.getCount() / self.per_page): clientsMenu.addField( 'NEXT', lambda: self.__changePageParams( self.page + 1, self.per_page)) if self.page > 1: clientsMenu.addField( 'PREV', lambda: self.__changePageParams( self.page - 1, self.per_page)) clients = self.clientController.getAll(self.page, self.per_page) for client in clients: clientsMenu.addField(f"<{client.id}> {client.name}", lambda id=client.id: self.__getClient(id)) except Exception as err: clientsMenu.setError(str(err)) clientsMenu.run('Return to main menu') def __updateClient(self, id: int): if self.clientController.update(id): self.currentMenu[1].stop() self.__getClient(id) else: self.currentMenu[1].setError('Incorrect update values') def __deleteClient(self, id: int): self.clientController.delete(id) self.currentMenu[1].stop() self.__supportCUIFunc() def __supportCUIFunc(self): self.currentMenu[1].stop() self.__changePageParams(self.page, self.per_page) def __getClient(self, id: int): clientMenu = CUI('Client menu') self.currentMenu[1] = clientMenu try: client: Client = self.clientController.getById(id) values = client.getValues().split(',') keys = client.getKeys().split(',') for i in range(len(keys)): clientMenu.addField(keys[i] + ' : ' + values[i]) clientMenu.addField('DELETE', lambda: self.__deleteClient(client.id)) clientMenu.addField('UPDATE', lambda: self.__updateClient(client.id)) clientMenu.addField('Return to prev menu', lambda: self.__supportCUIFunc()) except Exception as err: clientMenu.setError(str(err)) clientMenu.run(False)
class EntityView: def __init__(self, instance): self.instance = instance self.page = 1 self.itemsCurrentMenu = [None, None] self.per_page = 10 self.CUI = CUI(f"{self.instance.__name__} model menu") self.Controller = ModelController(instance) self.CUI.addField(f'Add {self.instance.__name__}', lambda: self.__addItem()) self.CUI.addField(f'{self.instance.__name__}s', lambda: self.__getItems()) def __supportFillItemFunc(self, key, value, item): try: new_value = input(f'Enter new {key} value: ') if isinstance(new_value, str) and len(new_value) > 0: if new_value.isdigit(): setattr(item, key, int(new_value)) else: setattr(item, key, new_value) else: raise Exception('Incorrect input') self.currentMenu.renameField(f'{key}: <{value}>', f'{key}: <{new_value}>') except Exception as err: self.currentMenu.setError(str(err)) def __supportFillFunc(self, key, mapped): try: value = input(f'Enter new {key} value: ') old_value = None if key in mapped and mapped[key] is not None: old_value = mapped[key] if isinstance(value, str) and len(value) > 0: if value.isdigit(): mapped[key] = int(value) else: mapped[key] = value else: raise Exception('Incorrect input') if old_value is None: self.currentMenu.renameField(f'{key}', f'{key}: <{value}>') else: self.currentMenu.renameField(f'{key}: <{old_value}>', f'{key}: <{value}>') except Exception as err: self.currentMenu.setError(str(err)) def __fillEntityMenu(self, *args): self.currentMenu = CUI(f'{self.instance.__name__} fill menu') try: if len(args) > 0 and isinstance(args[0], self.instance): item = args[0] for (key, value) in self.Controller.getModelEntityMappedKeys( item).items(): self.currentMenu.addField( f'{key}: <{value}>', lambda key=key, value=value: self. __supportFillItemFunc(key, value, item)) elif len(args) > 0 and isinstance(args[0], dict): mapped = args[0] for key in self.Controller.getModelKeys(): mapped[key] = None self.currentMenu.addField( f'{key}', lambda key=key: self.__supportFillFunc(key, mapped)) else: raise Exception('Invalid arguments') except Exception as err: self.currentMenu.setError(str(err)) self.currentMenu.run('Save and Return') def run(self): self.CUI.run() def __addItem(self): try: mapped = {} self.__fillEntityMenu(mapped) exec_str = '' for value in mapped.values(): if value is None or \ (isinstance(value, str) and any((char in exec_bad_chars) for char in value)) \ : raise Exception('Invalid entity fill') if isinstance(value, str): exec_str += f"'{value}', " else: exec_str += f"{value}, " exec("self.entity = self.instance(%s)" % exec_str[:-1]) entityId = self.Controller.add(self.entity) self.CUI.setError(f'New Entity created! Id: {entityId}') except Exception as err: self.CUI.setError(str(err)) def __changePageParams(self, page: int, per_page: int): self.page = page self.per_page = per_page self.itemsCurrentMenu[0].stop() self.__getItems() def __getItems(self): itemsMenu = CUI(self.instance.__name__ + 's') self.itemsCurrentMenu[0] = itemsMenu try: if self.page < math.ceil( self.Controller.getCount() / self.per_page): itemsMenu.addField( 'NEXT', lambda: self.__changePageParams( self.page + 1, self.per_page)) if self.page > 1: itemsMenu.addField( 'PREV', lambda: self.__changePageParams( self.page - 1, self.per_page)) entities = self.Controller.getRange(self.page, self.per_page) for entity in entities: if 'name' in self.Controller.getModelKeys(): itemsMenu.addField(f"<{entity.id}> {entity.name}", lambda id=entity.id: self.__getItem(id)) else: itemsMenu.addField(f"<{entity.id}>", lambda id=entity.id: self.__getItem(id)) except Exception as err: itemsMenu.setError(str(err)) itemsMenu.run('Return to main menu') def __updateItem(self, item): self.__fillEntityMenu(item) self.Controller.update(item) self.itemsCurrentMenu[1].stop() self.__getItem(item.id) def __deleteItem(self, id: int): self.Controller.delete(id) self.itemsCurrentMenu[1].stop() self.__supportCUIFunc() def __supportCUIFunc(self): self.itemsCurrentMenu[1].stop() self.__changePageParams(self.page, self.per_page) def __getItem(self, id: int): itemMenu = CUI(f'{self.instance.__name__} menu') self.itemsCurrentMenu[1] = itemMenu try: item = self.Controller.getById(id) for (key, value ) in self.Controller.getModelEntityMappedKeys(item).items(): itemMenu.addField(str(key) + ' : ' + str(value)) itemMenu.addField('DELETE', lambda: self.__deleteItem(item.id)) itemMenu.addField('UPDATE', lambda: self.__updateItem(item)) itemMenu.addField('Return to prev menu', lambda: self.__supportCUIFunc()) except Exception as err: itemMenu.setError(str(err)) itemMenu.run(False)