コード例 #1
0
    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'))
コード例 #2
0
    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
コード例 #3
0
    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
コード例 #4
0
    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
コード例 #5
0
    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
コード例 #6
0
    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
コード例 #7
0
    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'))
コード例 #8
0
    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)
コード例 #9
0
    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)