mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-27 18:40:31 +00:00
Merge pull request #1715 from bookwyrm-social/summary-queries
Simplifies query for earliest year
This commit is contained in:
commit
0dcd7bee10
2 changed files with 13 additions and 50 deletions
|
@ -1,7 +1,7 @@
|
|||
"""testing the annual summary page"""
|
||||
from datetime import datetime
|
||||
import pytz
|
||||
from unittest.mock import patch
|
||||
import pytz
|
||||
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
from django.http import Http404
|
||||
|
@ -100,13 +100,8 @@ class AnnualSummary(TestCase):
|
|||
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
|
||||
def test_annual_summary_page(self, *_):
|
||||
"""there are so many views, this just makes sure it LOADS"""
|
||||
|
||||
shelf = self.local_user.shelf_set.filter(identifier="read").first()
|
||||
models.ShelfBook.objects.create(
|
||||
book=self.book,
|
||||
user=self.local_user,
|
||||
shelf=shelf,
|
||||
shelved_date=make_date(2020, 1, 1),
|
||||
models.ReadThrough.objects.create(
|
||||
user=self.local_user, book=self.book, finish_date=make_date(2020, 1, 1)
|
||||
)
|
||||
|
||||
view = views.AnnualSummary.as_view()
|
||||
|
@ -124,7 +119,7 @@ class AnnualSummary(TestCase):
|
|||
def test_annual_summary_page_with_review(self, *_):
|
||||
"""there are so many views, this just makes sure it LOADS"""
|
||||
|
||||
self.review = models.Review.objects.create(
|
||||
models.Review.objects.create(
|
||||
name="Review name",
|
||||
content="test content",
|
||||
rating=3.0,
|
||||
|
@ -132,12 +127,8 @@ class AnnualSummary(TestCase):
|
|||
book=self.book,
|
||||
)
|
||||
|
||||
shelf = self.local_user.shelf_set.filter(identifier="read").first()
|
||||
models.ShelfBook.objects.create(
|
||||
book=self.book,
|
||||
user=self.local_user,
|
||||
shelf=shelf,
|
||||
shelved_date=make_date(2020, 1, 1),
|
||||
models.ReadThrough.objects.create(
|
||||
user=self.local_user, book=self.book, finish_date=make_date(2020, 1, 1)
|
||||
)
|
||||
|
||||
view = views.AnnualSummary.as_view()
|
||||
|
|
|
@ -3,7 +3,7 @@ from datetime import date
|
|||
from uuid import uuid4
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.db.models import Case, When, Avg, Sum
|
||||
from django.db.models import Avg, Sum, Min, Case, When
|
||||
from django.http import Http404
|
||||
from django.shortcuts import redirect
|
||||
from django.template.response import TemplateResponse
|
||||
|
@ -185,7 +185,12 @@ def privacy_verification(request, user, year, year_key):
|
|||
def is_year_available(user, year):
|
||||
"""return boolean"""
|
||||
|
||||
earliest_year = int(get_earliest_year(user, year))
|
||||
earliest_year = user.readthrough_set.filter(finish_date__isnull=False).aggregate(
|
||||
Min("finish_date")
|
||||
)["finish_date__min"]
|
||||
if not earliest_year:
|
||||
return True
|
||||
earliest_year = earliest_year.year
|
||||
today = date.today()
|
||||
year = int(year)
|
||||
if earliest_year <= year < today.year:
|
||||
|
@ -196,39 +201,6 @@ def is_year_available(user, year):
|
|||
return False
|
||||
|
||||
|
||||
def get_earliest_year(user, year):
|
||||
"""return the earliest finish_date or shelved_date year for user books in read shelf"""
|
||||
|
||||
read_shelfbooks = models.ShelfBook.objects.filter(user__id=user.id).filter(
|
||||
shelf__identifier__exact="read"
|
||||
)
|
||||
read_shelfbooks_list = list(read_shelfbooks.values("book", "shelved_date"))
|
||||
|
||||
book_dates = []
|
||||
|
||||
for book in read_shelfbooks_list:
|
||||
earliest_finished = (
|
||||
models.ReadThrough.objects.filter(user__id=user.id)
|
||||
.filter(book_id=book["book"])
|
||||
.exclude(finish_date__exact=None)
|
||||
.order_by("finish_date")
|
||||
.values("finish_date")
|
||||
.first()
|
||||
)
|
||||
|
||||
if earliest_finished:
|
||||
book_dates.append(
|
||||
min(earliest_finished["finish_date"], book["shelved_date"])
|
||||
)
|
||||
else:
|
||||
book_dates.append(book["shelved_date"])
|
||||
|
||||
if book_dates:
|
||||
return min(book_dates).year
|
||||
|
||||
return year
|
||||
|
||||
|
||||
def get_books_from_shelfbooks(books_ids):
|
||||
"""return an ordered QuerySet of books from a list"""
|
||||
|
||||
|
|
Loading…
Reference in a new issue