def test_remove_review(): add_book("Test Book 1", "Test Author", date_shift='+0') add_review('1', '+3') reviews = list(Review.select().join(Book, on=(Review.book == Book.id))) assert len(reviews) == 1 remove_review(1) reviews_after_remove = list(Review.select().join( Book, on=(Review.book == Book.id))) assert len(reviews_after_remove) == 0
def test_list_reviews(): add_book("Test Book 1", "Test Author", date_shift='+0') add_book("Test Book 2", "Test Author", date_shift='+0') add_review("Test Book 1", '-3') add_review("Test Book 1", '-3') add_review("Test Book 2", '-3') assert len( list(Review.select().join(Book, on=(Review.book == Book.id)).where( Book.name == "Test Book 1"))) == 2 assert len( list(Review.select().join(Book, on=(Review.book == Book.id)).where( Book.name == "Test Book 2"))) == 1
def test_add_review_by_id(): add_book("Test Book 1", "Test Author", date_shift='+0') add_review('1', '+3') reviews = list(Review.select().join(Book, on=(Review.book == Book.id))) assert len(reviews) == 1 assert reviews[0].date_of_review == datetime.date.today( ) + datetime.timedelta(3) assert reviews[0].book.name == "Test Book 1"
def add_review(name_or_id, date_shift): 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_review date") book = _get_book(name_or_id) Review(book=book, date_of_review=adjusted_date).save() LOGGER.debug("Created Review(Book.name={}, date_of_review={})".format( book.name, adjusted_date))
def list_reviews(order_by, asc_or_desc): mapper = { 'id': Review.id, 'date_created': Review.date_created, 'date_of_review': Review.date_of_review, 'book_name': Book.name, 'book_id': Book.id } if asc_or_desc == 'asc': order_field = mapper[order_by].asc() else: order_field = mapper[order_by].desc() return list(Review.select().join( Book, on=(Review.book == Book.id)).order_by(order_field))
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)
def remove_review(id): r = Review.get_by_id(id) r.delete_instance()