From 82294909a8403e05e1d7b60407230f4ab956a588 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 8 Jan 2022 16:38:52 -0800 Subject: [PATCH] Python formatting --- bookwyrm/models/readthrough.py | 2 ++ bookwyrm/models/relationship.py | 10 +++--- bookwyrm/templatetags/bookwyrm_tags.py | 45 +++++++++++++------------- bookwyrm/templatetags/interaction.py | 2 +- bookwyrm/views/reading.py | 14 ++++---- 5 files changed, 39 insertions(+), 34 deletions(-) diff --git a/bookwyrm/models/readthrough.py b/bookwyrm/models/readthrough.py index f75918ac1..ceb8e0b6e 100644 --- a/bookwyrm/models/readthrough.py +++ b/bookwyrm/models/readthrough.py @@ -1,5 +1,6 @@ """ progress in a book """ from django.core import validators +from django.core.cache import cache from django.db import models from django.db.models import F, Q @@ -30,6 +31,7 @@ class ReadThrough(BookWyrmModel): def save(self, *args, **kwargs): """update user active time""" + cache.delete(f"latest_read_through-{self.user.id}-{self.book.id}") self.user.update_active_date() # an active readthrough must have an unset finish date if self.finish_date: diff --git a/bookwyrm/models/relationship.py b/bookwyrm/models/relationship.py index 2b8f240db..e95c38fa5 100644 --- a/bookwyrm/models/relationship.py +++ b/bookwyrm/models/relationship.py @@ -40,10 +40,12 @@ class UserRelationship(BookWyrmModel): def save(self, *args, **kwargs): """clear the template cache""" # invalidate the template cache - cache.delete_many([ - f"relationship-{self.user_subject.id}-{self.user_object.id}", - f"relationship-{self.user_object.id}-{self.user_subject.id}", - ]) + cache.delete_many( + [ + f"relationship-{self.user_subject.id}-{self.user_object.id}", + f"relationship-{self.user_object.id}-{self.user_subject.id}", + ] + ) super().save(*args, **kwargs) class Meta: diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index f173e052c..22f4225b2 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -3,6 +3,7 @@ from django import template from django.db.models import Avg from bookwyrm import models +from bookwyrm.utils import cache from bookwyrm.views.feed import get_suggested_books @@ -79,35 +80,35 @@ def related_status(notification): @register.simple_tag(takes_context=True) def active_shelf(context, book): """check what shelf a user has a book on, if any""" - if hasattr(book, "current_shelves"): - read_shelves = [ - s - for s in book.current_shelves - if s.shelf.identifier in models.Shelf.READ_STATUS_IDENTIFIERS - ] - return read_shelves[0] if len(read_shelves) else {"book": book} - - shelf = ( - models.ShelfBook.objects.filter( - shelf__user=context["request"].user, - book__parent_work__editions=book, + 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() ) - .select_related("book", "shelf") - .first() + or {"book": book}, + user, + book, + timeout=15552000, ) - return shelf if shelf else {"book": book} @register.simple_tag(takes_context=False) def latest_read_through(book, user): """the most recent read activity""" - if hasattr(book, "active_readthroughs"): - return book.active_readthroughs[0] if len(book.active_readthroughs) else None - - return ( - models.ReadThrough.objects.filter(user=user, book=book, is_active=True) - .order_by("-start_date") - .first() + 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, ) diff --git a/bookwyrm/templatetags/interaction.py b/bookwyrm/templatetags/interaction.py index c9f08fb31..89a25420a 100644 --- a/bookwyrm/templatetags/interaction.py +++ b/bookwyrm/templatetags/interaction.py @@ -16,7 +16,7 @@ def get_user_liked(user, status): lambda u, s: models.Favorite.objects.filter(user=u, status=s).exists(), user, status, - timeout=259200 + timeout=259200, ) diff --git a/bookwyrm/views/reading.py b/bookwyrm/views/reading.py index c7eda10e1..77e527f39 100644 --- a/bookwyrm/views/reading.py +++ b/bookwyrm/views/reading.py @@ -1,7 +1,6 @@ """ the good stuff! the books! """ from django.contrib.auth.decorators import login_required from django.core.cache import cache -from django.core.cache.utils import make_template_fragment_key from django.db import transaction from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound from django.shortcuts import get_object_or_404, redirect @@ -46,12 +45,13 @@ class ReadingStatus(View): if not identifier: return HttpResponseBadRequest() - # invalidate the template cache - cache_keys = [ - make_template_fragment_key("shelve_button", [request.user.id, book_id]), - make_template_fragment_key("suggested_books", [request.user.id]), - ] - cache.delete_many(cache_keys) + # invalidate related caches + cache.delete_many( + [ + f"suggested_books-{request.user.id}", + f"active_shelf-{request.user.id}-{book_id}", + ] + ) desired_shelf = get_object_or_404( models.Shelf, identifier=identifier, user=request.user