def execute(self, args): bookService = App.instance.getService('books') # Get arguments title = self.askQuestion('Enter the title:') authorId = self.askQuestion('Enter the author ID:') year = self.askQuestion('Enter the year:') country = self.askQuestion('Enter the country:') language = self.askQuestion('Enter the language:') pages = self.askQuestion('Enter the number of pages:') # Create book book = Book({ 'title': title, 'authorId': authorId, 'year': year, 'country': country, 'language': language, 'pages': pages }) # Try to save if bookService.saveBook(book): self.showInfo('Successfully created book') else: ValidationHelper.printErrorList(book)
def execute(self, args): bookService = App.instance.getService('books') # Check args length if len(args) != 1: self.showUsage() # Get book by id book = bookService.getBookById(args[0]) if book == None: self.showError('Invalid book') # Update book book.title = self.askQuestion('Enter the title:', book.title) book.authorId = self.askQuestion('Enter the authorId:', book.authorId) book.year = self.askQuestion('Enter the year:', book.year) book.country = self.askQuestion('Enter the country:', book.country) book.language = self.askQuestion('Enter the language:', book.language) book.pages = self.askQuestion('Enter the number of pages:', book.pages) # Try to save if bookService.saveBook(book): self.showInfo('Successfully saved book') else: ValidationHelper.printErrorList(book)
def validate(self): authorService = App.instance.getService('authors') if self.title == None or len(self.title) == 0: self.addError('title', 'Title is required') return False if len(self.title) > 255: self.addError('title', 'Title cannot be longer than 255 characters') return False if self.authorId == None: self.addError('authorId', 'Author is required') return False if authorService.getAuthorById(self.authorId) == None: self.addError('authorId', 'Author does not exist') return False if self.year == None: self.addError('year', 'Year is required') return False if ValidationHelper.parseInt(self.year) == False: self.addError('year', 'Year must be a valid number') return False if len(str(self.year)) > 5: self.addError('year', 'Year cannot be longer than 5 characters') return False if self.country == None or len(self.country) == 0: self.addError('country', 'Country is required') return False if len(self.country) > 50: self.addError('country', 'Country cannot be longer than 50 characters') return False if self.language == None or len(self.language) == 0: self.addError('language', 'Language is required') return False if len(self.language) > 50: self.addError('language', 'Language cannot be longer than 50 characters') return False if self.pages == None: self.addError('pages', 'Pages is required') return False if ValidationHelper.parseInt(self.pages) == False: self.addError('pages', 'Pages must be a valid number') return False return True
def execute(self, args): customerService = App.instance.getService('customers') # Get json from file rawPath = self.askQuestion('Which file would you like to import?') filePath = FileHelper.createFilePath(rawPath, True) if not filePath.exists(): self.showError('Csv file doesn\'t exist') try: fileContent = open(filePath, encoding='utf8') data = csv.reader(fileContent) except csv.Error: self.showError( 'Failed to load csv. The file contains invalid data.') except Exception: self.showError('Failed to load csv. Unable to read file contents.') # Skip CSV header next(data, None) # Create customers for customerRow in data: self._validateCsvRow(customerRow) # Create customer customer = Customer({ 'firstName': customerRow[3], 'lastName': customerRow[4], 'gender': 'M' if customerRow[1] == 'male' else 'F', 'language': customerRow[2], 'street': customerRow[5], 'zipcode': customerRow[6], 'city': customerRow[7], 'email': customerRow[8], 'telephone': customerRow[10] }) # Try to save if not customerService.saveCustomer(customer): ValidationHelper.printErrorList(customer) raise Exception('Unable to save customer: ' + customerRow['title']) # Close file fileContent.close() self.showInfo('Successfully imported customers')
def execute(self, args): authorService = App.instance.getService('authors') # Get arguments firstName = self.askQuestion('Enter the first name:') lastName = self.askQuestion('Enter the last name:') # Create author author = Author({ 'firstName': firstName, 'lastName': lastName }) # Try to save if authorService.saveAuthor(author): self.showInfo('Successfully created author') else: ValidationHelper.printErrorList(author)
def execute(self, args): bookLoanService = App.instance.getService('bookLoans') # Get arguments bookItemId = self.askQuestion('Enter the book item ID:') customerId = self.askQuestion('Enter the customer ID:') # Create book loan bookLoan = BookLoan({ 'bookItemId': bookItemId, 'customerId': customerId }) # Try to save if bookLoanService.saveBookLoan(bookLoan): self.showInfo('Successfully created book loan') else: ValidationHelper.printErrorList(bookLoan)
def execute(self, args): authorService = App.instance.getService('authors') # Check args length if len(args) != 1: self.showUsage() # Get author by id author = authorService.getAuthorById(args[0]) if author == None: self.showError('Invalid author') # Update author author.firstName = self.askQuestion('Enter the first name:', author.firstName) author.lastName = self.askQuestion('Enter the last name:', author.lastName) # Try to save if authorService.saveAuthor(author): self.showInfo('Successfully saved author') else: ValidationHelper.printErrorList(author)
def execute(self, args): customerService = App.instance.getService('customers') # Check args length if len(args) != 1: self.showUsage() # Get customer by id customer = customerService.getCustomerById(args[0]) if customer == None: self.showError('Invalid customer') # Update customer customer.firstName = self.askQuestion('Enter the first name:', customer.firstName) customer.lastName = self.askQuestion('Enter the last name:', customer.lastName) customer.gender = self.askQuestion('Enter the gender:', customer.gender) customer.language = self.askQuestion('Enter the language:', customer.language) customer.street = self.askQuestion('Enter the street:', customer.street) customer.zipcode = self.askQuestion('Enter the zipcode:', customer.zipcode) customer.city = self.askQuestion('Enter the city:', customer.city) customer.email = self.askQuestion('Enter the email:', customer.email) customer.telephone = self.askQuestion('Enter the telephone:', customer.telephone) # Try to save if customerService.saveCustomer(customer): self.showInfo('Successfully saved customer') else: ValidationHelper.printErrorList(customer)
def execute(self, args): authorService = App.instance.getService('authors') bookService = App.instance.getService('books') # Get json from file rawPath = self.askQuestion('Which file would you like to import?') filePath = FileHelper.createFilePath(rawPath, True) if not filePath.exists(): self.showError('Json file doesn\'t exist') try: with open(filePath, encoding='utf8') as fileContent: data = json.load(fileContent) except json.JSONDecodeError: self.showError( 'Failed to load json. The file contains invalid data.') except Exception: self.showError( 'Failed to load json. Unable to read file contents.') # Validate the json structure self._validateJson(data) # Create authors authorNames = set(map(lambda a: a['author'], data)) authors = {} for authorName in authorNames: parts = authorName.split(' ') firstName = parts.pop(0) lastName = ' '.join(parts) # Create author author = Author({'firstName': firstName, 'lastName': lastName}) # Save and map it if not authorService.saveAuthor(author): ValidationHelper.printErrorList(author) raise Exception('Unable to save author: ' + authorName) authors[authorName] = author # Create books for bookRow in data: author = authors[bookRow['author']] book = Book({ 'title': bookRow['title'], 'authorId': author.id, 'year': bookRow['year'], 'country': bookRow['country'], 'language': bookRow['language'], 'pages': bookRow['pages'] }) # Try to save if not bookService.saveBook(book): ValidationHelper.printErrorList(book) raise Exception('Unable to save book: ' + bookRow['title']) self.showInfo('Successfully imported books')