def get_user_boosted(user, status): """did the given user fav a status?""" return get_or_set( f"boost-{user.id}-{status.id}", lambda u: status.boosters.filter(user=u).exists(), user, timeout=259200, )
def get_user_liked(user, status): """did the given user fav a status?""" return get_or_set( f"fav-{user.id}-{status.id}", lambda u, s: models.Favorite.objects.filter(user=u, status=s).exists(), user, status, timeout=259200, )
def get_is_book_on_shelf(book, shelf): """is a book on a shelf""" return cache.get_or_set( f"book-on-shelf-{book.id}-{shelf.id}", lambda b, s: s.books.filter(id=b.id).exists(), book, shelf, timeout=15552000, )
def get_relationship(context, user_object): """caches the relationship between the logged in user and another user""" user = context["request"].user return get_or_set( f"relationship-{user.id}-{user_object.id}", get_relationship_name, user, user_object, timeout=259200, )
def latest_read_through(book, user): """the most recent read activity""" return cache.get_or_set( f"latest_read_through-{user.id}-{book.id}", lambda u, b: (models.ReadThrough.objects.filter( user=u, book=b, is_active=True).order_by("-start_date").first()), user, book, timeout=15552000, )
def get_rating(book, user): """get the overall rating of a book""" return cache.get_or_set( f"book-rating-{book.parent_work.id}-{user.id}", lambda u, b: models.Review.privacy_filter(u).filter( book__parent_work__editions=b, rating__gt=0).aggregate( Avg("rating"))["rating__avg"] or 0, user, book, timeout=15552000, )
def active_shelf(context, book): """check what shelf a user has a book on, if any""" user = context["request"].user return cache.get_or_set( f"active_shelf-{user.id}-{book.id}", lambda u, b: (models.ShelfBook.objects.filter( shelf__user=u, book__parent_work__editions=b, ).first() or False), user, book, timeout=15552000, ) or { "book": book }