def _get_book(name_or_id): if name_or_id.isdigit(): book = Book.get(Book.id == name_or_id) else: book = Book.get(Book.name == name_or_id) return book
def test_add_book_shifted_minus(): add_book("Test Book", "Test Author", date_shift='-5') assert len(Book.select()) == 1 book = Book.select()[0] assert book.name == "Test Book" assert book.date_created == datetime.date.today() assert book.date_of_origin == datetime.date.today() - datetime.timedelta(5)
def test_remove_book(): add_book("Test Book 1", "Test Author", date_shift='+0') add_book("Test Book 2", "Test Author", date_shift='+0') add_book("Test Book 3", "Test Author", date_shift='+0') remove_book("Test Book 1") remove_book("Test Book 3") books = list(Book.select()) assert len(books) == 1 assert books[0].name == "Test Book 2"
def list_books(order_by, asc_or_desc): mapper = { 'id': Book.id, 'date_created': Book.date_created, 'date_of_origin': Book.date_of_origin, 'name': Book.name } if asc_or_desc == 'asc': order_field = mapper[order_by].asc() else: order_field = mapper[order_by].desc() return list(Book.select().order_by(order_field))
def add_book(name, author, isbn='', date_shift='+0'): if date_shift[0] == '+': adjusted_date = datetime.date.today() + datetime.timedelta( int(date_shift[1:])) elif date_shift[0] == '-': adjusted_date = datetime.date.today() - datetime.timedelta( int(date_shift[1:])) else: raise Exception("Only +XX or -XX allowed for add_book date") Book(name=name, author=author, isbn=isbn, date_of_origin=adjusted_date).save() LOGGER.debug("Created new Book(id, name={}, date_origin={})".format( id, name, adjusted_date))
def _get_books_and_overdue_days_to_review(): for b in Book.select(): reviews = Review.select().join( Book, on=(Review.book == Book.id)).where(Book.id == b.id).order_by( Review.date_of_review.asc()) if not reviews: days_since_last_review = (datetime.date.today() - b.date_of_origin).days if days_since_last_review > SPACED_REPETITION_INTERVALS[0]: days_overdue = days_since_last_review - SPACED_REPETITION_INTERVALS[ 0] yield (days_overdue, b) else: days_since_last_review = (datetime.date.today() - reviews[-1].date_of_review).days # days_past_scheduled_review = SPACED_REPETITION_INTERVALS[len(reviews)]) - days_since_last_review if days_since_last_review > SPACED_REPETITION_INTERVALS[len( reviews)]: days_overdue = days_since_last_review - SPACED_REPETITION_INTERVALS[ 0] yield (days_overdue, b)