def post(self,request,genre_id):
        user_id =  request.session['user_id'] 
        username = request.session['username'] 
        egenre = GenreForm(request.POST)
        context = {}
        if 'update-genre' in request.POST:
            if egenre.is_valid():
                g = Genre()
                g.genre = egenre.cleaned_data['genre']
                g.genre_id = genre_id
                self.gdao.update(g)
                context['user_id'] = request.session['user_id'],
                context['username'] = request.session['username'] 
                return redirect(reverse(('admingenredetail'),kwargs={ 'genre_id': genre_id }))

        elif 'delete-genre' in request.POST:
            g = Genre()
            g.genre_id = genre_id
            self.gdao.delete(g)
            context['user_id'] = request.session['user_id'],
            context['username'] = request.session['username']
            return redirect(reverse('admingenreindex')) 

        else:
            return redirect(reverse(('admingenredetail'),kwargs={ 'genre_id': genre_id }))


                
    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
Example #3
0
 def __init__(self):
     self.book_id = None
     self.isbn13 = None
     self.isbn10 = None
     self.title = None
     self.copyRightDate = None
     self.type = None
     self.edition = None
     self.numberOfPages = None
     self.genre = Genre()
     self.author = Author()
     self.publisher = Publisher()
     self.inventory = Inventory()
    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'] 
     agenre = GenreForm(request.POST)
     context = {}
     if 'create-genre' in request.POST:
         if agenre.is_valid():
             genre = Genre()
             genre.genre = agenre.cleaned_data['genre']                
             self.gdao.create(genre) 
             context['user_id'] = request.session['user_id'],
             context['username'] = request.session['username'] 
             return redirect(reverse('admingenreindex'))
     else: 
         return redirect(reverse('adminindex'))
    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'))
    def post(self, request, book_id):
        user_id = request.session['user_id']
        username = request.session['username']
        book_form = BookForm(request.POST,
                             author_choices=self.authors,
                             publisher_choices=self.publishers,
                             genre_choices=self.genres)
        book = self.book_dao.get_byid(book_id)

        context = {'book': book}

        if 'update-book' in request.POST:
            if book_form.is_valid():
                updated_book = Book()
                updated_book.book_id = book_id
                updated_book.title = book_form.cleaned_data['title']
                updated_book.isbn10 = book_form.cleaned_data['isbn10']
                updated_book.isbn13 = book_form.cleaned_data['isbn13']
                updated_book.copyRightDate = book_form.cleaned_data[
                    'copyright_date']
                updated_book.edition = book_form.cleaned_data['edition']
                updated_book.numberOfPages = book_form.cleaned_data[
                    'num_pages']
                updated_book.type = book_form.cleaned_data['book_type']
                author = Author()
                author.author_id = int(book_form.cleaned_data['authors'])
                updated_book.author = author
                publisher = Publisher()
                publisher.publisher_id = int(
                    book_form.cleaned_data['publishers'])
                updated_book.publisher = publisher
                genre = Genre()
                genre.genre_id = int(book_form.cleaned_data['genres'])
                updated_book.genre = genre
                x = self.inventory_dao.get_byid(book_id)
                updated_inventory = Inventory()
                updated_inventory.book_id = book_id
                updated_inventory.quantity_on_hand = book_form.cleaned_data[
                    'quantity_on_hand']
                updated_inventory.quantity_ordered = x.quantity_ordered
                updated_inventory.retail_price = book_form.cleaned_data[
                    'retail_price']
                updated_inventory.cost = book_form.cleaned_data['cost']

                self.book_dao.update(updated_book)
                self.inventory_dao.update(updated_inventory)

                context['notification'] = "Book updated successfully!"
                context['user_id'] = request.session['user_id'],
                context['username'] = request.session['username']
                return redirect(
                    reverse(('adminbookdetail'), kwargs={'book_id': book_id}))

            else:
                context['user_id'] = request.session['user_id'],
                context['username'] = request.session['username']
                context['notification'] = "Not a valid submission."

        elif 'delete-book' in request.POST:
            book_id = int(request.POST.get('delete-book'))
            self.book_dao.delete(book_id)

        if 'add-image' in request.POST:
            image_form = BookImageForm(request.POST, request.FILES)

            if image_form.is_valid():
                image_file = request.FILES['image']
                fs = FileSystemStorage()
                filename = fs.save(image_file.name, image_file)
                uploaded_file_url = fs.url(filename)

                image = Image()
                image.image_url = uploaded_file_url
                image.caption = ''
                image.book.book_id = book_id

                self.image_dao.create(image)

                context['notification'] = filename
                return redirect(
                    reverse(('adminbookdetail'), kwargs={'book_id': book_id}))
            else:
                context['notification'] = "Not a valid submission."

        return render(request, self.template_name, context)
    def post(self, request, book_id):
        book_form = BookForm(request.POST,
                             author_choices=self.authors,
                             publisher_choices=self.publishers,
                             genre_choices=self.genres)
        book = self.book_dao.get_byid(book_id)

        context = {'book': book}

        if 'update-book' in request.POST:
            if book_form.is_valid():
                updated_book = Book()
                updated_book.book_id = book_id
                updated_book.title = book_form.cleaned_data['title']
                updated_book.isbn10 = book_form.cleaned_data['isbn10']
                updated_book.isbn13 = book_form.cleaned_data['isbn13']
                updated_book.copyRightDate = book_form.cleaned_data[
                    'copyright_date']
                updated_book.edition = book_form.cleaned_data['edition']
                updated_book.numberOfPages = book_form.cleaned_data[
                    'num_pages']
                updated_book.type = book_form.cleaned_data['book_type']
                author = Author()
                author.author_id = int(book_form.cleaned_data['authors'])
                updated_book.author = author
                publisher = Publisher()
                publisher.publisher_id = int(
                    book_form.cleaned_data['publishers'])
                updated_book.publisher = publisher
                genre = Genre()
                genre.genre_id = int(book_form.cleaned_data['genres'])
                updated_book.genre = genre

                self.book_dao.update(updated_book)

                context['notification'] = "Book updated successfully!"

            else:
                context['notification'] = "Not a valid submission."

        elif 'delete-book' in request.POST:
            book_id = int(request.POST.get('delete-book'))
            self.book_dao.delete(book_id)

        if 'add-image' in request.POST:
            image_form = BookImageForm(request.POST, request.FILES)

            if image_form.is_valid():
                image_file = request.FILES['image']
                fs = FileSystemStorage()
                filename = fs.save(image_file.name, image_file)
                uploaded_file_url = fs.url(filename)

                image = Image()
                image.image_url = uploaded_file_url
                image.caption = ''
                image.book.book_id = book_id

                self.image_dao.create(image)

                context['notification'] = filename
            else:
                context['notification'] = "Not a valid submission."

        return render(request, self.template_name, context)