Skip to content

Commit

Permalink
Merge pull request #3378 from hughrun/get-books-for-user
Browse files Browse the repository at this point in the history
possible fix for #3372 - user export timeouts
  • Loading branch information
mouse-reeve authored Jun 23, 2024
2 parents 3545a1c + 1d4119e commit 3236003
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions bookwyrm/models/bookwyrm_export_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from s3_tar import S3Tar

from django.db.models import BooleanField, FileField, JSONField
from django.db.models import Q
from django.core.serializers.json import DjangoJSONEncoder
from django.core.files.base import ContentFile
from django.core.files.storage import storages
Expand Down Expand Up @@ -315,19 +314,28 @@ def export_book(user: User, edition: Edition):


def get_books_for_user(user):
"""Get all the books and editions related to a user"""

editions = (
Edition.objects.select_related("parent_work")
.filter(
Q(shelves__user=user)
| Q(readthrough__user=user)
| Q(review__user=user)
| Q(list__user=user)
| Q(comment__user=user)
| Q(quotation__user=user)
)
.distinct()
"""
Get all the books and editions related to a user.
We use union() instead of Q objects because it creates
multiple simple queries in stead of a much more complex DB query
that can time out.
"""

shelf_eds = Edition.objects.select_related("parent_work").filter(shelves__user=user)
rt_eds = Edition.objects.select_related("parent_work").filter(
readthrough__user=user
)
review_eds = Edition.objects.select_related("parent_work").filter(review__user=user)
list_eds = Edition.objects.select_related("parent_work").filter(list__user=user)
comment_eds = Edition.objects.select_related("parent_work").filter(
comment__user=user
)
quote_eds = Edition.objects.select_related("parent_work").filter(
quotation__user=user
)

editions = shelf_eds.union(rt_eds, review_eds, list_eds, comment_eds, quote_eds)

return editions

0 comments on commit 3236003

Please sign in to comment.