def post(self, request, publisher_id): user_id = request.session['user_id'] username = request.session['username'] context = {} epublisher = PublisherForm(request.POST) if 'update-publisher' in request.POST: if epublisher.is_valid(): p = Publisher() p.company_name = epublisher.cleaned_data['company_name'] p.city = epublisher.cleaned_data['city'] p.state_code = epublisher.cleaned_data['state_code'] p.zip_code = epublisher.cleaned_data['zip_code'] p.phone_number = epublisher.cleaned_data['phone_number'] p.contact_name = epublisher.cleaned_data['contact_name'] p.publisher_id = publisher_id self.pdao.update(p) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect( reverse(('adminpublisherdetail'), kwargs={'publisher_id': publisher_id})) if 'delete-publisher' in request.POST: self.pdao.delete(publisher_id) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect(reverse('adminpublisherindex'))
def get_byid(self, publisher_id): publisher = None try: db_config = read_db_config() conn = MySQLConnection(**db_config) cursor = conn.cursor() args = [publisher_id] cursor.callproc('getPublisherByPublisherID', args) for result in cursor.stored_results(): # This loop iterates through the rows in each resultset for publisher_row in result.fetchall(): publisher = Publisher() publisher.publisher_id = publisher_row[0] publisher.company_name = publisher_row[1] publisher.city = publisher_row[2] publisher.state_code = publisher_row[3] publisher.zip_code = publisher_row[4] publisher.phone_number = publisher_row[5] publisher.contact_name = publisher_row[6] # Close the connection to the DB cursor.close() conn.close() except Error as error: print(error) except Exception as e: print(e) return publisher
def get_all(self): publishers = [] try: # Setup connection to the DB db_config = read_db_config() conn = MySQLConnection(**db_config) cursor = conn.cursor() # Calls the stored procedure cursor.callproc('getAllPublishers') # This loop iterates through the resultsets for result in cursor.stored_results(): # This loop iterates through the rows in each resultset for publisher_row in result.fetchall(): publisher = Publisher() publisher.publisher_id = publisher_row[0] publisher.company_name = publisher_row[1] publisher.city = publisher_row[2] publisher.state_code = publisher_row[3] publisher.zip_code = publisher_row[4] publisher.phone_number = publisher_row[5] publisher.contact_name = publisher_row[6] publishers.append(publisher) # Close the connection to the DB cursor.close() conn.close() except Error as error: print(error) except Exception as e: print(e) return publishers
def get_all(self): allBooks = [] try: db_config = read_db_config() conn = MySQLConnection(**db_config) cursor = conn.cursor() cursor.callproc('getAllBooks') for result in cursor.stored_results(): books = result.fetchall() for x in books: currentbook = Book() currentbook.set_book_id(x[0]) currentbook.set_isbn13(x[1]) currentbook.set_isbn10(x[2]) currentbook.set_title(x[3]) currentbook.set_copyRightDate(x[4]) currentbook.set_type(x[5]) currentbook.set_edition(x[6]) currentbook.set_numberOfPages(x[7]) genre = Genre() genre.genre_id = x[8] genre.genre = x[14] author = Author() author.author_id = x[9] author.first_name = x[12] author.last_name = x[13] publisher = Publisher() publisher.publisher_id = x[10] publisher.company_name = x[11] currentbook.set_genre(genre) currentbook.set_author(author) currentbook.set_publisher(publisher) currentbook.inventory.quantity_on_hand = x[15] currentbook.inventory.quantity_ordered = x[16] currentbook.inventory.cost = x[17] currentbook.inventory.retail_price = x[18] allBooks.append(currentbook) conn.commit() cursor.close() conn.close() except Error as error: print(error) return allBooks
def get_byid(self,book_id): book = Book() try: db_config = read_db_config() conn = MySQLConnection(**db_config) cursor = conn.cursor() args = [book_id] cursor.callproc('getBookByID', args) # This gets the first resultset result = next(cursor.stored_results()) # This gets the first row in the resultset book_row = result.fetchone() book.set_book_id(book_row[0]) book.set_isbn13(book_row[1]) book.set_isbn10(book_row[2]) book.set_title(book_row[3]) book.set_copyRightDate(book_row[4]) book.set_type(book_row[5]) book.set_edition(book_row[6]) book.set_numberOfPages(book_row[7]) genre = Genre() genre.genre_id = book_row[8] genre.genre = book_row[14] author = Author() author.author_id = book_row[9] author.first_name = book_row[12] author.last_name = book_row[13] publisher = Publisher() publisher.publisher_id = book_row[10] publisher.company_name = book_row[11] book.set_genre(genre) book.set_author(author) book.set_publisher(publisher) book.inventory.quantity_on_hand = book_row[15] book.inventory.quantity_ordered = book_row[16] book.inventory.cost = book_row[17] book.inventory.retail_price = book_row[18] cursor.close() conn.close() except Error as error: print(error) except Exception as e: print(e) return book
def post(self, request): user_id = request.session['user_id'] username = request.session['username'] context = {} apublisher = PublisherForm(request.POST) if 'create-publisher' in request.POST: if apublisher.is_valid(): p = Publisher() p.company_name = apublisher.cleaned_data['company_name'] p.city = apublisher.cleaned_data['city'] p.state_code = apublisher.cleaned_data['state_code'] p.zip_code = apublisher.cleaned_data['zip_code'] p.phone_number = apublisher.cleaned_data['phone_number'] p.contact_name = apublisher.cleaned_data['contact_name'] self.pdao.create(p) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect(reverse('adminpublisherindex'))
def get_byname(self, company_name): publisher = Publisher() try: db_config = read_db_config() conn = MySQLConnection(**db_config) cursor = conn.cursor() args = (company_name, ) cursor.callproc('getPublisherByName', args) # This gets the first resultset result = next(cursor.stored_results()) # This gets the first row in the resultset publisher_row = result.fetchone() publisher.publisher_id = publisher_row[0] publisher.company_name = publisher_row[1] publisher.city = publisher_row[2] publisher.state_code = publisher_row[3] publisher.zip_code = publisher_row[4] publisher.phone_number = publisher_row[5] publisher.contact_name = publisher_row[6] leftover_rows = len(result.fetchall()) # Probably don't need this since we should make the company name a UI if leftover_rows != 0: print( str(leftover_rows) + " more row(s) have the company name: " + "\"" + company_name + "\"") cursor.close() conn.close() except Error as error: print(error) except Exception as e: print(e) return publisher
def post(self, request): context = {} self.get_dropdown_data() # Handle POST requests if 'create-book' in request.POST: sub_book_form = BookForm(request.POST, author_choices=self.authors, publisher_choices=self.publishers, genre_choices=self.genres) book = Book() if sub_book_form.is_valid(): book.title = sub_book_form.cleaned_data['title'] book.isbn10 = sub_book_form.cleaned_data['isbn10'] book.isbn13 = sub_book_form.cleaned_data['isbn13'] book.copyRightDate = sub_book_form.cleaned_data[ 'copyright_date'] book.edition = sub_book_form.cleaned_data['edition'] book.numberOfPages = sub_book_form.cleaned_data['num_pages'] book.type = sub_book_form.cleaned_data['book_type'] author = Author() author.author_id = int(sub_book_form.cleaned_data['authors']) book.author = author publisher = Publisher() publisher.publisher_id = int( sub_book_form.cleaned_data['publishers']) book.publisher = publisher genre = Genre() genre.genre_id = int(sub_book_form.cleaned_data['genres']) book.genre = genre book.inventory.quantity_on_hand = sub_book_form.cleaned_data[ 'quantity_on_hand'] book.inventory.cost = sub_book_form.cleaned_data['cost'] book.inventory.retail_price = sub_book_form.cleaned_data[ 'retail_price'] self.book_dao.create(book) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] context['notification'] = "Book saved successfully!" return redirect(reverse('adminbookindex')) else: context['notification'] = "Not a valid submission." elif 'create-publisher' in request.POST: sub_publisher_form = PublisherForm(request.POST) publisher = Publisher() if sub_publisher_form.is_valid(): publisher.company_name = sub_publisher_form.cleaned_data[ 'company_name'] publisher.city = sub_publisher_form.cleaned_data['city'] publisher.state_code = sub_publisher_form.cleaned_data[ 'state_code'] publisher.zip_code = sub_publisher_form.cleaned_data[ 'zip_code'] publisher.phone_number = sub_publisher_form.cleaned_data[ 'phone_number'] publisher.contact_name = sub_publisher_form.cleaned_data[ 'contact_name'] self.publisher_dao.create(publisher) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] context['notification'] = "Publisher saved successfully!" return redirect(reverse('adminaddbook')) else: context['notification'] = "Not a valid submission." elif 'create-author' in request.POST: sub_author_form = AuthorForm(request.POST) author = Author() if sub_author_form.is_valid(): author.first_name = sub_author_form.cleaned_data['first_name'] author.last_name = sub_author_form.cleaned_data['last_name'] self.author_dao.create(author) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] context['notification'] = "Author saved successfully!" return redirect(reverse('adminaddbook')) else: context['notification'] = "Not a valid submission." elif 'create-genre' in request.POST: sub_genre_form = GenreForm(request.POST) genre = Genre() if sub_genre_form.is_valid(): genre.genre = sub_genre_form.cleaned_data['genre'] self.genre_dao.create(genre) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] context['notification'] = "Genre saved successfully!" return redirect(reverse('adminaddbook')) else: context['notification'] = "Not a valid submission." else: return redirect(reverse('adminaddbook'))