def input_add_data(self): data = {'own': {}, 'relation': {}} for (key, field) in self.model: if field.initial_value is not None: data['own'][key] = field.initial_value elif isinstance(field, ForeignKeyField): foreign_key = Relationship.select_foreign_key_relationship( field.relationship) if foreign_key: data['own'][key] = foreign_key else: break elif isinstance(field, OneToManyField): foreign_key = Relationship.select_foreign_key_relationship_inverse( field.relationship) if foreign_key: data['relation'][key] = foreign_key else: break elif field.default_value is not None: user_input = input( "%s (%s): " % (Style.make_label(key), field.default_value)) if user_input is '': user_input = field.default_value data['own'][key] = user_input else: data['own'][key] = input("%s: " % Style.make_label(key)) return data
def pagination_text(self): text = "" if self.page > 1: text += "%s previous | " % Colour.green('<') text += "page %d" % self.page if self.page * self.limit < self.total: text += " | next %s" % Colour.green('>') print(Style.create_underline(text)) print(text) print(Style.create_underline(text))
def input_edit_data(self, item): data = {'own': {}, 'relation': {}} for (key, field) in self.model: if not field.updatable: continue if isinstance(field, ForeignKeyField): if Menu.yes_no_question('Change %s?' % field.relationship.name): foreign_key = Relationship.select_foreign_key_relationship( field.relationship) if foreign_key: data['own'][key] = foreign_key else: data['own'][key] = item[key] elif isinstance(field, OneToManyField): foreign_key = Relationship.select_foreign_key_relationship_inverse( field.relationship) if foreign_key: data['relation'][key] = foreign_key else: data['relation'][key] = item[key] # Todo: handle removing relationships else: data['own'][key] = self.update_field(item[key], Style.make_label(key)) return data
def add(self): print(Style.create_title('Add %s' % self.table_name)) data = self.input_add_data() self.validate_model_data(data['own']) if self.model.is_valid(): self.save_data(data) else: self.display_errors('%s not added' % self.table_name) Menu.wait_for_input()
def show_jobs_by_assigned_to(self, staff_id): print(Style.create_title('Select job to log time')) job = self.paginated_menu( find=lambda limit, page: self.repository. find_paginated_by_assigned_to(staff_id, limit, page), find_by_id=self.repository.find_by_id) if job: self.show_item_detail(job) self.show_item_menu(job['id'])
def select_foreign_key_relationship(relationship): repository = relationship.repository() print(Style.create_title('Select %s' % relationship.name)) paginated_menu = relationship.paginated_menu or Pagination(repository) item = paginated_menu() if item: return item[0] else: return False
def generate(self): print(Style.create_title('Generate Invoice')) invoice = self.make_paginated_menu() if invoice: jobRepository = JobRepository() jobs = jobRepository.find_jobs_by_invoice_id(invoice['id']) self.enter_billable_time(jobRepository, jobs) jobs = jobRepository.find_jobs_by_invoice_id(invoice['id']) invoice_data = self.make_invoice_dictionary(invoice, jobs) LatexInvoice().generate(**invoice_data) self.mark_invoiced_jobs_as_complete(jobRepository, jobs) Menu.wait_for_input()
def enter_billable_time(self, jobRepository, jobs): print(Style.create_title('Enter Billable Time')) for job in jobs: print('Title: %s' % job['title']) print('Description: %s' % job['description']) print('Estimated Time: %s' % job['estimated_time']) print('Logged Time: %s' % job['actual_time']) billable = '' while not Validation.isFloat(billable): billable = input('Billable Time: ') jobRepository.update_billable_time(job['id'], billable) jobRepository.save() jobRepository.check_rows_updated('Job Updated')
def edit(self): print(Style.create_title('Edit %s' % self.table_name)) item = self.make_paginated_menu() if item: data = self.input_edit_data(item) data['own'] = self.validate_model_data(data['own']) if self.model.is_valid: self.save_update(data, item) else: self.display_errors('%s not updated' % self.table_name) else: print('No changes made') Menu.wait_for_input()
def delete(self): print(Style.create_title('Delete %s' % self.table_name)) item = self.make_paginated_menu() if item: user_action = False while not user_action == 'delete': user_action = input( 'Type \'%s\' to remove this item or %s to cancel: ' % (Colour.red('delete'), Colour.green('c'))) if user_action == 'c': return self.save_remove(item) Menu.wait_for_input()
def select_foreign_key_relationship_inverse(relationship): foreign_keys = [] repository = relationship.repository() while Menu.yes_no_question('Add %s' % relationship.name): print(Style.create_title('Select %s' % relationship.name)) paginated_menu = relationship.paginated_menu or Pagination( repository) item = paginated_menu() if item: foreign_keys.append(item['id']) else: print('\nNo relationship added\n') return relationship.related_name, foreign_keys
def create(title, action_collection: ActionCollection): user_input = 0 keys = [] while True: print(Style.create_title(title)) for action in action_collection.actions: print(Colour.green(action.key) + ": " + str(action)) keys.append(action.key) while user_input not in keys: user_input = input('\nSelect an option: ') for action in action_collection.actions: if action.check_input(user_input): action.execute() if user_input == 'b' or user_input == 'q': break else: user_input = 0
def show_item_detail(self, item): print(Style.create_title('%s Data' % self.table_name)) for header in self.repository.get_headers(): label = Style.make_label(header) print('%s: %s' % (label, Colour.blue(str(item[header]))))
def show(self): print(Style.create_title('Show %s' % self.table_name)) item = self.make_paginated_menu() if item: self.show_item_detail(item) self.show_item_menu(item['id'])
def display_errors(self, message): print(Style.create_title(message)) for (key, value) in self.model.get_errors().items(): print("%s: %s" % (key.capitalize(), Colour.red(value)))