Exemplo n.º 1
0
    def test_save_add_to_db(self):
        bk = Book(author='AAAA', title='BBBB', read=True)
        bk.save()
        self.assertIsNotNone(bk.id)  # Check book has ID

        self.assertEqual(bk, bookstore.get_book_by_id(bk.id))
        self.assertTrue(bookstore.exact_match(bk))
Exemplo n.º 2
0
def detailsBook(id):
    global apiInfo

    try:
        # Using eval to convert string to a dictionairy
        bookList = json.loads(
            requests.get(app.config['API_ROOT_URL'] + '/books' + '/' +
                         str(id)).content)
    except:
        bookList = []

    for book in bookList:
        # There is one and only one book
        orgBook = Book(
            book['id'],
            book['name'],
            book['price'],  # Two decimals
            book['isbn'],
            book['isObsolete'],
            book['bookType'])

    orgBook.price = ConvertToTwoDecimals(orgBook.price)
    orgBook.isObsolete = ConvertBooleanToText(orgBook.isObsolete)
    orgBook.bookType = ConvertEnumBookTypeToDescription(orgBook.bookType)

    return render_template('books/details.html',
                           actionTitle='Book details',
                           appTitle=app.config['APP_TITLE'],
                           api=apiInfo,
                           book=vars(orgBook))
 def test_add_book_store_with_books_in(self):
     self.add_test_data()
     book_count = bookstore.book_count()
     bk = Book(title='aa', author='bbbbb')
     bk.save()
     self.assertTrue(bookstore.exact_match(bk))
     self.assertEqual(book_count + 1, bookstore.book_count())
Exemplo n.º 4
0
 def test_show_books_list(self, mock_print):
     bk1 = Book('a', 'aaa')
     bk2 = Book('b', 'bbb')
     books = [bk1, bk2]
     ui.show_books(books)
     mock_print.assert_any_call(bk1)
     mock_print.assert_any_call(bk2)
Exemplo n.º 5
0
 def test_delete_all_books(self):
     self.clear_bookstore()
     bk1 = Book(title='Not in store', author='Not in store')
     bk2 = Book(title='Whatever', author='Whatever')
     store.add_book(bk1)
     store.add_book(bk2)
     store.delete_all_books()
     self.assertEqual(0, store.book_count())
Exemplo n.º 6
0
 def test_delete_all_books(self):
     self.clear_bookstore()
     bk1 = Book('Not in store', 'Not in store')
     bk2 = Book('Whatever', 'Whatever')
     self.BS.add_book(bk1)
     self.BS.add_book(bk2)
     self.BS.delete_all_books()
     self.assertEqual(0, self.BS.book_count())
Exemplo n.º 7
0
    def add_test_data(self):
        self.bk1 = Book('the title', 'the author', False)
        self.bk2 = Book('what the book is called', 'the writer', True)
        self.bk3 = Book('fascinating', 'the author', True)
        self.bk4 = Book('brilliant', 'schrodinger', False)

        self.store.add_book(self.bk1)
        self.store.add_book(self.bk2)
        self.store.add_book(self.bk3)
        self.store.add_book(self.bk4)
Exemplo n.º 8
0
    def add_test_data(self):
        self.bk1 = Book(title='An Interesting Book', author='Ann Author', read=True)
        self.bk2 = Book(title='Booky Book Book', author='B. Bookwriter', read=False)
        self.bk3 = Book(title='Collection of words', author='Creative Creator')

        self.clear_bookstore()

        store.add_book(self.bk1)
        store.add_book(self.bk2)
        store.add_book(self.bk3)
Exemplo n.º 9
0
    def add_test_data(self):
        self.bk1 = Book('An Interesting Book', 'Ann Author', True)
        self.bk2 = Book('Booky Book Book', 'B. Bookwriter', False)
        self.bk3 = Book('Collection of words', 'Creative Creator')

        self.clear_bookstore()

        self.BS.add_book(self.bk1)
        self.BS.add_book(self.bk2)
        self.BS.add_book(self.bk3)
Exemplo n.º 10
0
    def proceed(self, product):

        # Fetch metadata from Google
        service = GoogleBookService(utils.configuration.googlebook_key)
        data = service.fetch_data(product.book.isbn13)

        if data is not None:

            # Parse the metadata
            parser = GoogleBookDataParser()

            if parser.parse(data):

                # Convert to book
                book = Book()
                book.title = parser.title
                book.isbn10 = parser.isbn10
                book.isbn13 = parser.isbn13
                book.year = parser.publishedYear
                book.description = parser.description
                book.nb_pages = parser.pageCount
                book.language = parser.language

                def convert_name_to_author(name):
                    author = Author()
                    author.name = name
                    return author

                book.authors = map(convert_name_to_author, parser.authors)

                if book is not None and book.isbn10 == product.book.isbn10 and book.isbn13 == product.book.isbn13:

                    # Store the book
                    product.book = book

                    # Save data from Googlebook
                    product.metadata_path = "{0}/{1}.json".format(utils.configuration.tmp_path, product.book.isbn13)
                    with open(product.metadata_path, 'w') as outfile:
                        json.dump(data, outfile)

                    # Download the thumbnail
                    product.thumbnail_path = "{0}/{1}.picture".format(utils.configuration.tmp_path, product.book.isbn13)
                    download = Download(parser.thumbnail_url, product.thumbnail_path)
                    downloader = Downloader(download)
                    downloader.run()

                    if download.status:

                        utils.logger.info("Match found in Google Book database [{0}]".format(book.title))

                        if self.next:
                            return self.next.proceed(product)

                        product.status = Status.Done
                        return product

        utils.logger.info("This book is unknow from Google Book database")

        product.status = Status.Error
        return product
Exemplo n.º 11
0
    def test_create_book_id_increases(self):
        Counter.reset_counter()

        bk = Book('Title', 'Author')
        self.assertEqual(1, bk.id)

        bk2 = Book('Title2', 'Author2')
        self.assertEqual(2, bk2.id)

        bk3 = Book('Title3', 'Author3')
        self.assertEqual(3, bk3.id)
Exemplo n.º 12
0
    def add_test_data(self):
        self.bk1 = Book(title='the title', author='the author', read=False)
        self.bk2 = Book(title='what the book is called',
                        author='the writer',
                        read=True)
        self.bk3 = Book(title='fascinating', author='the author', read=True)
        self.bk4 = Book(title='brilliant', author='schrodinger', read=False)

        store.add_book(self.bk1)
        store.add_book(self.bk2)
        store.add_book(self.bk3)
        store.add_book(self.bk4)
Exemplo n.º 13
0
def create_book(name, **kwargs):
    owner_id = get_current_user_id()
    if not owner_id:
        return False
    description = kwargs.get('description')
    genres_ids = kwargs.get('genres_ids') or []
    cover_image_url = kwargs.get('cover_image_url')
    page_count = kwargs.get('page_count')
    author_name = kwargs.get('author_name')
    year = kwargs.get('year')

    book = Book(name=name, owner_id=owner_id)
    book.description = description
    book.cover_image_url = cover_image_url
    book.page_count = page_count
    book.author_name = author_name
    book.year = year

    genres = []
    for id in genres_ids:
        g = get_genre_by_id(id)
        if g:
            genres.append(g)
    book.genres = genres

    session.add(book)
    session.commit()
    return book
    def test_delete_all_books(self):
        bk1 = Book(title='Interesting Title', author='Author')
        bk2 = Book(title='Whatever', author='W. Whatever')
        bk1.save()
        bk2.save()

        bookstore.delete_all_books()
        self.assertEqual(0, bookstore.book_count())
Exemplo n.º 15
0
 def test_add_book_prevent_duplicates(self, mock_print, mock_input):
     main.main()
     self.assertEqual(1, store.book_count())
     # make book that mimics the first one expected to be created
     expected_book = Book(title='Title', author='Author', read=False, id=1)
     all_books = store.get_all_books()
     self.assertEqual(expected_book, all_books[0])
Exemplo n.º 16
0
 def test_add_book(self, mock_print, mock_input):
     main.main()
     # reset counter and make book that mimics the one expected to be created
     Counter.reset_counter()
     expected_book = Book('Title', 'Author', False)
     all_books = self.store.get_all_books()
     self.assertEqual(expected_book, all_books[0])
Exemplo n.º 17
0
def deleteBook(id):
    global apiInfo

    try:
        bookList = json.loads(
            requests.get(app.config['API_ROOT_URL'] + '/books' + '/' +
                         str(id)).content)
    except:
        bookList = []

    for book in bookList:
        # There is one and only one book
        # Use constructor b/c mutating members directly result in unpredictable data
        orgBook = Book(book['id'], book['name'])

    form = DeleteBookForm()

    if form.validate_on_submit():
        requests.delete(app.config['API_ROOT_URL'] + '/books' + '/' + str(id))

        flash('Deleted book id = {}'.format(id))
        return redirect('/books')

    return render_template('books/delete.html',
                           actionTitle='Delete book',
                           appTitle=app.config['APP_TITLE'],
                           api=apiInfo,
                           book=vars(orgBook),
                           form=form)
Exemplo n.º 18
0
    def borrows(self, data_list):
        successful_borrows = []
        user = User.get_by_id(data_list[0]["user"]["user_id"])

        with database.atomic():
            num_book_borrowing = BookCirculation.select().where(
                (BookCirculation.user == user)
                & (BookCirculation.return_time.is_null(True))).count()

            if num_book_borrowing + len(data_list) > 5:
                raise BorrowingExceededError()

            for data in data_list:
                book = Book.get_by_id(data["book"]["book_id"])
                data['book'] = book
                data['user'] = user

                if BookCirculation.select().where(
                    (BookCirculation.book == book) &
                    (BookCirculation.return_time.is_null(True))).count() != 0:
                    raise AlreadyBorrowedError()

                successful_borrows.append(BookCirculation.create(**data))

        SendBorrowNotification(successful_borrows).start()

        return successful_borrows
Exemplo n.º 19
0
    def get(self):
        books = (Book.query().filter(Book.listed == True).filter(
            Book.status == 'approved').filter(Book.deleted == False).order(
                -Book.display_order, -Book.votes_for).fetch(10))
        for book in books:
            if book.book_image:
                book.book_image = json.loads(book.book_image)['link']
            if book.icon:
                book.icon = json.loads(book.icon)['link']
            book.author_users = [
                user.to_client_dict() for user in User.get_by_id(book.authors)
            ]
        pages = (Page.query().filter(Page.listed == True).filter(
            Page.status == 'approved').filter(Book.deleted == False).order(
                -Page.display_order, -Page.votes_for).fetch(5))
        for page in pages:
            if page.iconPath:
                page.icon_thumbnail = page.iconPath + '?size=120'
            page.author_users = [
                user.to_client_dict() for user in User.get_by_id(page.authors)
            ]

        self.write(
            'main.html',
            featuredBooks=books,
            featuredPages=pages,
        )
Exemplo n.º 20
0
def get_books_by_read_value(read):
    """ Get a list of books that have been read, or list of books that have not been read.
    :param read True for books that have been read, False for books that have not been read
    :returns all books with the read value.
    """
    query = Book.select().where(Book.read == read)
    return list(query)
Exemplo n.º 21
0
 def test_add_book(self):
     self.add_test_data()
     book_count = self.BS.book_count()
     bk = Book('aa', 'bbbbb')
     self.BS.add_book(bk)
     self.assertTrue(self.BS.get_book_by_id(bk.id))
     self.assertEqual(book_count + 1, self.BS.book_count())
Exemplo n.º 22
0
def add_book():
    """Add new book to db"""

    data = request.get_json()
    print(data)
    print("session id: {}".format(session['user_id']))

    q = Book.query.filter((Book.title == data["title"])
                          & (Book.author == data["author"]))

    if q.count() == 0:
        new_book = Book(title=data['title'], author=data['author'])
        db.session.add(new_book)
        db.session.commit()
        new_user_book = BookUser(book_id=new_book.id,
                                 user_id=session['user_id'])
        db.session.add(new_user_book)
        db.session.commit()
        book = {
            'title': data["title"],
            'author': data["author"],
            'id': new_book.id
        }
        return jsonify(book)

    else:
        book = q.one()
        new_user_book = BookUser(book_id=book.id, user_id=session['user_id'])
        db.session.add(new_user_book)
        db.session.commit()
        book = {'title': book.title, 'author': book.author, 'id': book.id}
        return jsonify(book)
Exemplo n.º 23
0
 def find(self, isbn):
     books = self.__db['books'];
     docs = books.find({'isbn': isbn})
     if docs.count() == 0:
         return None
     doc = docs[0]
     return Book(doc['isbn'], doc['title'], doc['price'])
Exemplo n.º 24
0
 def get(self):
     file = open('header.html')
     self.response.write(file.read())
     query = int(self.request.get("query"))
     BK = Book.query(Book.BPages <= query)
     self.response.write(
         "<table align = 'center' border='1' cellspacing='10' cellpadding = '10'>"
     )
     self.response.write("<tr>")
     self.response.write("<td><b>Book Code</b></td>")
     self.response.write("<td><b>Book Title</b></td>")
     self.response.write("<td><b>Book Price</b></td>")
     self.response.write("<td><b>Book Pages</b></td>")
     self.response.write("<td><b>Book Pub</b></td>")
     self.response.write("<td><b>Book Desc</b></td>")
     self.response.write("</tr>")
     for b in BK:
         self.response.write("<tr>")
         self.response.write("<td>" + str(b.BCode) + "</td>")
         self.response.write("<td>" + b.BTitle + "</td>")
         self.response.write("<td>" + str(b.BPrice) + "</td>")
         self.response.write("<td>" + str(b.BPages) + "</td>")
         self.response.write("<td>" + b.BPub + "</td>")
         self.response.write("<td>" + b.BDesc + "</td>")
         self.response.write("</tr>")
     self.response.write("</table")
Exemplo n.º 25
0
    def get_book(self, book_id: str) -> Book:
        r = requests.get(self.__api.get_url('books') + '/' + book_id)
        json_book = r.json()['book']
        chapters = list()
        for json_chapter in json_book['chapters']:
            chapters.append(
                Chapter(id=json_chapter['id'],
                        order_number=json_chapter['order_nr'],
                        title=json_chapter['title'],
                        text=json_chapter['text']))

        book = Book(id=json_book['id'],
                    published_at=json_book['published_at'],
                    title=json_book['title'],
                    subtitle=json_book['subtitle'],
                    author=json_book['author'],
                    language=json_book['language'],
                    about_the_book=json_book['about_the_book'],
                    teaser=json_book['teaser'],
                    who_should_read=json_book['who_should_read'],
                    about_the_author=json_book['about_the_author'],
                    market=json_book['market'],
                    image_url=json_book['image_url'],
                    chapters=chapters)
        return book
Exemplo n.º 26
0
def delete_book(book):
    """ Removes book from store. Raises BookError if book not in store. 
    :param book the Book to delete """

    rows_deleted = Book.delete().where(Book.id == book.id).execute()
    if not rows_deleted:
        raise BookError('Tried to delete book that doesn\'t exist')
Exemplo n.º 27
0
    def search(self, title):
        docs = self.__repo.search(title)
        books = []

        for doc in docs:
            books.append(Book(doc['isbn'], doc['title'], doc['price']))
        return books
Exemplo n.º 28
0
def save_book(book: book.Book) -> Book:
    try:
        db_zipfile = ZipFile(zip_name=book.zip_file.zip_name)
        db_session.merge(db_zipfile)
        db_session.commit()
    except IntegrityError as e:
        db_session.rollback()
        db_zipfile = db_session.query(ZipFile).filter(
            ZipFile.zip_name == book.zip_file.zip_name).first()
        logging.error(e)
    db_book = Book(zip_name=book.zip_file.zip_name,
                   book_name=book.book_name,
                   title=book.title,
                   annotation=book.annotation,
                   authors=book.authors,
                   language=book.lang,
                   genre=book.genre)

    try:
        db_session.add(db_book)
        db_session.commit()
    except IntegrityError as e:
        db_session.rollback()
        db_book = db_session.query(Book).filter(
            Book.zip_name == book.zip_file.zip_name
            and Book.book_name == book.book_name).first()
        logging.error(e)
    for word in book.words:
        save_book_word(db_book, word)
    db_session.commit()
    return db_book
Exemplo n.º 29
0
    def get(self, book_id=None):
        user = self.get_current_user()
        expand = ''
        if user is None:
            self.redirect('/books/{}'.format(book_id))
            return

        if book_id is not None:
            book_id = Book.get_long_uid(book_id)
            book = self.api.get_by_id(book_id)

            if not user.is_admin and user.uid not in book.authors:
                self.redirect('/books/{}'.format(book_id))

            book.book_image = util.extract_value_from_json(
                book.book_image, 'link')
            icon_link = util.extract_value_from_json(book.icon, 'link')
            book.icon = icon_link + '?size=360' if icon_link else icon_link
            book_json = json.dumps(book.to_client_dict())

        expand = Chapter.get_long_uid(self.request.get('expand'))

        self.write(
            'book-manage.html',
            book=book.to_client_dict(),
            book_json=book_json,
            expand=expand,
        )
Exemplo n.º 30
0
 def get(self, book_id):
     uid = self.request.get('uid')
     token = self.request.get('token')
     user = AcceptAuthorshipToken.get_user_from_token_string(token)
     valid = user is not None
     logging.info('book_id={}, uid={}, token={}, user={}'.format(
         book_id, uid, token, user))
     if book_id is not None:
         book_id = Book.get_long_uid(book_id)
         book = self.api.get_by_id(book_id)
         authors = book.authors
         # Add to authors if token is valid
         if valid and uid not in authors:
             authors.append(uid)
             book.authors = authors
             book.put()
             AcceptAuthorshipToken.delete(token)
             self.redirect('/books/{}'.format(book_id), permanent=True)
         else:
             self.write('404.html', message='invalid token')
             # self.response.write(json.dumps(
             #     {'error': True, 'message': 'invalid token'}))
     else:
         self.response.write(
             json.dumps({
                 'error': True,
                 'message': 'invalid parameters'
             }))
Exemplo n.º 31
0
def create_new_user_submitted_book():
    """Create a new user-submitted book."""

    title = request.form.get('title')
    author = request.form.get('author')

    # Validate if username or email already exists in users table in database
    if not crud.get_book_by_title(title):

        book = Book(title=title)

        db.session.add(book)
        db.session.commit()

        flash('Book submitted successfully!', 'success')

        return redirect("/books")

    # If book does exist, flash message
    else:
        if Book.query.filter_by(title=title).all():
            flash(f"The book you submitted already exists in the library",
                  "warning")

        return redirect("/books")
Exemplo n.º 32
0
    def read_the_book(self, path, f_name, sh_names):
        """
        this method is reading each book, chapter by chapter and verse after
        verse and put it into the DataBase

        path - the path to the book
        f_name - full title of the current book
        sh_names - list of the shorten names
        """
        try:
            module = open(os.path.join(self.path, path), 'r')
        except:
            print "oops, the module %s doesn't exists" % \
                (os.path.join(self.path, path))
            return
        print path, f_name, sh_names
        book = Book()
        book.name = f_name
        book.shortname = sh_names

        reg = re.compile(ur'(\d+)')
        tag_remove = re.compile(ur'<.*?>')
        re_strong = re.compile(ur' [0-9]+')
        chapt_number = 1
        for line in module:
            uline = line.decode('utf-8', 'replace')
            if u'<h4>' in uline.lower() or u'<h1>' in uline.lower():
                uline = tag_remove.sub('', uline)
                chapter = Chapter()
                chapter.name = uline
                chapter.number = chapt_number
                chapt_number += 1
                book.chapters.append(chapter)
                session.add(chapter)
            if u'<p>' in uline.lower() or u'<sup>' in uline.lower():
                uline = tag_remove.sub('', uline)
                uline = re_strong.sub('', uline)
                verse = Verse()
                verse.number = int(reg.findall(uline)[0])
                verse.text = uline
                chapter.verses.append(verse)
                session.add(verse)
                session.add(chapter)

        session.add(book)
        session.commit()
        module.close()
Exemplo n.º 33
0
def addRecord(data):
    """
    Data should be a tuple of two dictionaries in the following format:
    
    ("author":{"first_name":"John", "last_name":"Doe"},
     "book":{"title":"Some book", "isbn":"1234567890", 
             "publisher":"Packt"}
    )
    """
    book = Book()
    book.title = data["book"]["title"]
    book.isbn = data["book"]["isbn"]
    book.publisher = data["book"]["publisher"]
    author = Person()
    author.first_name = data["author"]["first_name"]
    author.last_name = data["author"]["last_name"]
    book.person = author
    
    # connect to session and commit data to database
    session = connectToDatabase()
    session.add(book)
    session.commit()
    session.close()
Exemplo n.º 34
0
def get_book_info(url_list):
    """Takes in a list of urls and searches Google Books API."""
    
    """collects a book cover links, description, & title
    generates placeholder values for ISBN not found"""

    default_book_title = 'ISBN is not matched with title'
    default_book_author = 'ISBN is not matched with an author'
    default_book_cover_sm = None
    default_book_cover_md = None
    default_book_description = "ISBN is not matched with a description"

    for url in url_list:
        isbn = url.split('+')[1].split('&')[0]
        
        time.sleep(1)
        if isbn not in db_isbn:
            # book_request = urllib2.urlopen(url)
            
            request = urllib2.Request(url, headers={'User-agent':'Mozilla/11.0'})
            response = urllib2.urlopen(request)
            book_dict = json.load(response)
        
        
            if book_dict['totalItems'] > 0:
            
                if 'title'in book_dict['items'][0]['volumeInfo'].keys():
                    book_title = book_dict['items'][0]['volumeInfo']['title']
                else:
                    book_title = default_book_title
                
                if 'imageLinks' in book_dict['items'][0]['volumeInfo'].keys():
                    book_cover_sm = book_dict['items'][0]['volumeInfo']['imageLinks']['smallThumbnail']
                    book_cover_md = book_dict['items'][0]['volumeInfo']['imageLinks']['thumbnail']
                else:
                    book_cover_sm = default_book_cover_sm
                    book_cover_md = default_book_cover_md
                
                if 'authors' in book_dict['items'][0]['volumeInfo'].keys():
                    book_author = book_dict['items'][0]['volumeInfo']['authors'][0]
                else:
                    book_author = default_book_author
                    
                if 'description' in book_dict['items'][0]['volumeInfo'].keys():
                    if len(book_dict['items'][0]['volumeInfo']['description']) > 750:
                        book_description = book_dict['items'][0]['volumeInfo']['description'][0:700] + "[...]"
                    else:
                        book_description = book_dict['items'][0]['volumeInfo']['description']
                else:
                    book_description = default_book_description
                
            else:
                # default values in case book cover or information is not available
                book_title = default_book_title
                book_author = default_book_author
                book_cover_sm = default_book_cover_sm
                book_cover_md = default_book_cover_md
                book_description = default_book_description
                
        
            new_book = Book(title=book_title.encode('ascii', 'replace'),
                            author=book_author.encode('ascii', 'replace'),
                            description=book_description.encode('ascii', 'replace'),
                            isbn=isbn,
                            image_url_sm=book_cover_sm,
                            image_url_md=book_cover_md)
        
            print new_book.title
            new_book.commit_to_db()
Exemplo n.º 35
0
def _load_book_from_worldcat_details_page(html_content, details_page_url, oclc_id):
    """Given the HTML content of the details page, create a new Book object with all the necessary details."""

    current_book = Book()
    pq_page = pq(html_content)

    current_book.book_id = oclc_id

    current_book.title = _load_title_from_worldcat_details_page(pq_page)  # TITLE (e.g. 'Lean in : women, work, and the will to lead')
    current_book.publisher = _load_publisher_from_worldcat_details_page(pq_page)  # PUBLISHER (e.g. 'New York : Alfred A. Knopf, 2013.')
    current_book.worldcaturl = details_page_url
    current_book.page_count = _load_page_count_from_worldcat_details_page(pq_page)
    current_book.summary = _load_summary_from_worldcat_details_page(pq_page)
    current_book.coverurl = _load_cover_url_from_worldcat_details_page(pq_page)

    # TODO - check if author_name is unique before insertion
    current_book.authors = _load_authors_from_worldcat_details_page(html_content)

    isbn10_list, isbn13_list = _load_isbns_from_worldcat_details_page(html_content)
    current_book.isbn10s = isbn10_list
    current_book.isbn13s = isbn13_list

    return current_book