Python formatting

This commit is contained in:
Mouse Reeve 2022-01-08 16:38:52 -08:00
parent c822048509
commit 82294909a8
5 changed files with 39 additions and 34 deletions

View file

@ -1,5 +1,6 @@
""" progress in a book """ """ progress in a book """
from django.core import validators from django.core import validators
from django.core.cache import cache
from django.db import models from django.db import models
from django.db.models import F, Q from django.db.models import F, Q
@ -30,6 +31,7 @@ class ReadThrough(BookWyrmModel):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
"""update user active time""" """update user active time"""
cache.delete(f"latest_read_through-{self.user.id}-{self.book.id}")
self.user.update_active_date() self.user.update_active_date()
# an active readthrough must have an unset finish date # an active readthrough must have an unset finish date
if self.finish_date: if self.finish_date:

View file

@ -40,10 +40,12 @@ class UserRelationship(BookWyrmModel):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
"""clear the template cache""" """clear the template cache"""
# invalidate the template cache # invalidate the template cache
cache.delete_many([ cache.delete_many(
f"relationship-{self.user_subject.id}-{self.user_object.id}", [
f"relationship-{self.user_object.id}-{self.user_subject.id}", f"relationship-{self.user_subject.id}-{self.user_object.id}",
]) f"relationship-{self.user_object.id}-{self.user_subject.id}",
]
)
super().save(*args, **kwargs) super().save(*args, **kwargs)
class Meta: class Meta:

View file

@ -3,6 +3,7 @@ from django import template
from django.db.models import Avg from django.db.models import Avg
from bookwyrm import models from bookwyrm import models
from bookwyrm.utils import cache
from bookwyrm.views.feed import get_suggested_books from bookwyrm.views.feed import get_suggested_books
@ -79,35 +80,35 @@ def related_status(notification):
@register.simple_tag(takes_context=True) @register.simple_tag(takes_context=True)
def active_shelf(context, book): def active_shelf(context, book):
"""check what shelf a user has a book on, if any""" """check what shelf a user has a book on, if any"""
if hasattr(book, "current_shelves"): user = context["request"].user
read_shelves = [ return cache.get_or_set(
s f"active_shelf-{user.id}-{book.id}",
for s in book.current_shelves lambda u, b: (
if s.shelf.identifier in models.Shelf.READ_STATUS_IDENTIFIERS models.ShelfBook.objects.filter(
] shelf__user=u,
return read_shelves[0] if len(read_shelves) else {"book": book} book__parent_work__editions=b,
).first()
shelf = (
models.ShelfBook.objects.filter(
shelf__user=context["request"].user,
book__parent_work__editions=book,
) )
.select_related("book", "shelf") or {"book": book},
.first() user,
book,
timeout=15552000,
) )
return shelf if shelf else {"book": book}
@register.simple_tag(takes_context=False) @register.simple_tag(takes_context=False)
def latest_read_through(book, user): def latest_read_through(book, user):
"""the most recent read activity""" """the most recent read activity"""
if hasattr(book, "active_readthroughs"): return cache.get_or_set(
return book.active_readthroughs[0] if len(book.active_readthroughs) else None f"latest_read_through-{user.id}-{book.id}",
lambda u, b: (
return ( models.ReadThrough.objects.filter(user=u, book=b, is_active=True)
models.ReadThrough.objects.filter(user=user, book=book, is_active=True) .order_by("-start_date")
.order_by("-start_date") .first()
.first() ),
user,
book,
timeout=15552000,
) )

View file

@ -16,7 +16,7 @@ def get_user_liked(user, status):
lambda u, s: models.Favorite.objects.filter(user=u, status=s).exists(), lambda u, s: models.Favorite.objects.filter(user=u, status=s).exists(),
user, user,
status, status,
timeout=259200 timeout=259200,
) )

View file

@ -1,7 +1,6 @@
""" the good stuff! the books! """ """ the good stuff! the books! """
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.cache import cache from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key
from django.db import transaction from django.db import transaction
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
@ -46,12 +45,13 @@ class ReadingStatus(View):
if not identifier: if not identifier:
return HttpResponseBadRequest() return HttpResponseBadRequest()
# invalidate the template cache # invalidate related caches
cache_keys = [ cache.delete_many(
make_template_fragment_key("shelve_button", [request.user.id, book_id]), [
make_template_fragment_key("suggested_books", [request.user.id]), f"suggested_books-{request.user.id}",
] f"active_shelf-{request.user.id}-{book_id}",
cache.delete_many(cache_keys) ]
)
desired_shelf = get_object_or_404( desired_shelf = get_object_or_404(
models.Shelf, identifier=identifier, user=request.user models.Shelf, identifier=identifier, user=request.user