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 """
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:

View file

@ -40,10 +40,12 @@ class UserRelationship(BookWyrmModel):
def save(self, *args, **kwargs):
"""clear 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}",
])
]
)
super().save(*args, **kwargs)
class Meta:

View file

@ -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 = (
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=context["request"].user,
book__parent_work__editions=book,
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)
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,
)

View file

@ -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,
)

View file

@ -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]),
# invalidate related caches
cache.delete_many(
[
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(
models.Shelf, identifier=identifier, user=request.user