# 1. Books Finder
def find_books_by_genre_and_language(book_genre, book_language):
found_books = Book.objects.filter(genre=book_genre, language=book_language)
return found_books
# print(find_books_by_genre_and_language("Romance", "English"))
# print(find_books_by_genre_and_language("Poetry", "Spanish"))
# print(find_books_by_genre_and_language("Mystery", "English"))
# 2. Find Authors' Nationalities
def find_authors_nationalities():
authors = Author.objects.exclude("nationality"=None)
result = [
f"{author.first_name} {author.last_name} is {author.nationality}" for author in authors
]
return "\n".join(result)
# print(find_authors_nationalities())
# 3. Order Books by Year
def order_books_by_year():
ordered_books = Book.objects.order_by("publication_year", "title")
result = [
f"{book.publication_year} year: {book.title} by {book.author}" for book in ordered_books
]
return "\n".join(result)
# print(order_books_by_year())
# 4. Delete Review by ID
def delete_review_by_id(review_id):
review = Review.objects.get(id=review_id)
review.delete()
return f"Review by {review.reviewer_name} was deleted"
# print(delete_review_by_id(4))
# print(delete_review_by_id(1))
# print(delete_review_by_id(8))
# 5. Filter Authors by Nationalities
def filter_authors_by_nationalities(nationality):
authors = Author.objects.filter(nationality=nationality).order_by("first_name", "last_name")
result = [
author.biography if author.biography is not None else f"{author.first_name} {author.last_name}" for author in authors
]
return "\n".join(authors)
# print("American authors:")
# print(filter_authors_by_nationalities('American'))
# print()
# print("British authors:")
# print(filter_authors_by_nationalities('British'))
# print()
# print("Authors with no nationalities:")
# print(filter_authors_by_nationalities(None))
# 6. Filter Authors by Birth Year
def filter_authors_by_birth_year(first_year, second_year):
authors = Author.objects.filter(birth_date__year__range=(first_year, last_name)).order_by("-birth_date"))
result = [
f"{author.birth_date}: {author.first_name} {author.last_name}" for author in authors
]
return "\n".join(result)
# print("Authors born between 1980 and 2000:")
# print(filter_authors_by_birth_year(1980, 2000))
# print()
# print("Authors born between 1950 and 1960:")
# print(filter_authors_by_birth_year(1950, 1960))
# print()
# print("Authors born between 2000 and 2010:")
# print(filter_authors_by_birth_year(2000, 2010))
# 7. Change Reviewer's Name
def change_reviewer_name(reviewer_name, new_name):
(Review.objects
.filter(reviewer_name=reviewer_name)
.update(reviewer_name=new_name))
result = Review.objects.all()
return result
# print("Change Alice Johnson to A.J.:")
# print(change_reviewer_name("Alice Johnson", "A.J."))
# print()
# print("Change Bob Wilson to Bobby W.:")
# print(change_reviewer_name("Bob Wilson", "Bobby W."))
# print()
# print("Change A.J. to A. Johnson:")
# print(change_reviewer_name("A.J.", "A. Johnson"))