mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-06-13 18:59:24 +00:00
Merge pull request #3085 from bookwyrm-social/migration-explicit-imports
Uses explicit imports to avoid circular import in migrations code
This commit is contained in:
commit
8d3c2d9bd2
|
@ -27,6 +27,7 @@ from .group import Group, GroupMember, GroupMemberInvitation
|
||||||
|
|
||||||
from .import_job import ImportJob, ImportItem
|
from .import_job import ImportJob, ImportItem
|
||||||
from .bookwyrm_import_job import BookwyrmImportJob
|
from .bookwyrm_import_job import BookwyrmImportJob
|
||||||
|
from .bookwyrm_export_job import BookwyrmExportJob
|
||||||
|
|
||||||
from .move import MoveUser
|
from .move import MoveUser
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,10 @@ from django.db.models import Q
|
||||||
from django.core.serializers.json import DjangoJSONEncoder
|
from django.core.serializers.json import DjangoJSONEncoder
|
||||||
from django.core.files.base import ContentFile
|
from django.core.files.base import ContentFile
|
||||||
|
|
||||||
from bookwyrm import models
|
from bookwyrm.models import AnnualGoal, ReadThrough, ShelfBook, Shelf, List, ListItem
|
||||||
|
from bookwyrm.models import Review, Comment, Quotation
|
||||||
|
from bookwyrm.models import Edition, Book
|
||||||
|
from bookwyrm.models import UserFollows, User, UserBlocks
|
||||||
from bookwyrm.models.job import ParentJob, ParentTask
|
from bookwyrm.models.job import ParentJob, ParentTask
|
||||||
from bookwyrm.settings import DOMAIN
|
from bookwyrm.settings import DOMAIN
|
||||||
from bookwyrm.tasks import app, IMPORTS
|
from bookwyrm.tasks import app, IMPORTS
|
||||||
|
@ -91,7 +94,7 @@ def json_export(user): # pylint: disable=too-many-locals, too-many-statements
|
||||||
exported_user["avatar"] = f'https://{DOMAIN}{getattr(user, "avatar").url}'
|
exported_user["avatar"] = f'https://{DOMAIN}{getattr(user, "avatar").url}'
|
||||||
|
|
||||||
# reading goals
|
# reading goals
|
||||||
reading_goals = models.AnnualGoal.objects.filter(user=user).distinct()
|
reading_goals = AnnualGoal.objects.filter(user=user).distinct()
|
||||||
goals_list = []
|
goals_list = []
|
||||||
# TODO: either error checking should be more sophisticated
|
# TODO: either error checking should be more sophisticated
|
||||||
# or maybe we don't need this try/except
|
# or maybe we don't need this try/except
|
||||||
|
@ -104,7 +107,7 @@ def json_export(user): # pylint: disable=too-many-locals, too-many-statements
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
readthroughs = models.ReadThrough.objects.filter(user=user).distinct().values()
|
readthroughs = ReadThrough.objects.filter(user=user).distinct().values()
|
||||||
readthroughs = list(readthroughs)
|
readthroughs = list(readthroughs)
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # pylint: disable=broad-except
|
||||||
readthroughs = []
|
readthroughs = []
|
||||||
|
@ -120,16 +123,16 @@ def json_export(user): # pylint: disable=too-many-locals, too-many-statements
|
||||||
book["authors"] = list(edition.first().authors.all().values())
|
book["authors"] = list(edition.first().authors.all().values())
|
||||||
# readthroughs
|
# readthroughs
|
||||||
book_readthroughs = (
|
book_readthroughs = (
|
||||||
models.ReadThrough.objects.filter(user=user, book=book["id"])
|
ReadThrough.objects.filter(user=user, book=book["id"])
|
||||||
.distinct()
|
.distinct()
|
||||||
.values()
|
.values()
|
||||||
)
|
)
|
||||||
book["readthroughs"] = list(book_readthroughs)
|
book["readthroughs"] = list(book_readthroughs)
|
||||||
# shelves
|
# shelves
|
||||||
shelf_books = models.ShelfBook.objects.filter(
|
shelf_books = ShelfBook.objects.filter(
|
||||||
user=user, book=book["id"]
|
user=user, book=book["id"]
|
||||||
).distinct()
|
).distinct()
|
||||||
shelves_from_books = models.Shelf.objects.filter(
|
shelves_from_books = Shelf.objects.filter(
|
||||||
shelfbook__in=shelf_books, user=user
|
shelfbook__in=shelf_books, user=user
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -137,34 +140,34 @@ def json_export(user): # pylint: disable=too-many-locals, too-many-statements
|
||||||
book["shelf_books"] = {}
|
book["shelf_books"] = {}
|
||||||
|
|
||||||
for shelf in shelves_from_books:
|
for shelf in shelves_from_books:
|
||||||
shelf_contents = models.ShelfBook.objects.filter(
|
shelf_contents = ShelfBook.objects.filter(
|
||||||
user=user, shelf=shelf
|
user=user, shelf=shelf
|
||||||
).distinct()
|
).distinct()
|
||||||
|
|
||||||
book["shelf_books"][shelf.identifier] = list(shelf_contents.values())
|
book["shelf_books"][shelf.identifier] = list(shelf_contents.values())
|
||||||
|
|
||||||
# book lists
|
# book lists
|
||||||
book_lists = models.List.objects.filter(
|
book_lists = List.objects.filter(
|
||||||
books__in=[book["id"]], user=user
|
books__in=[book["id"]], user=user
|
||||||
).distinct()
|
).distinct()
|
||||||
book["lists"] = list(book_lists.values())
|
book["lists"] = list(book_lists.values())
|
||||||
book["list_items"] = {}
|
book["list_items"] = {}
|
||||||
for blist in book_lists:
|
for blist in book_lists:
|
||||||
list_items = models.ListItem.objects.filter(book_list=blist).distinct()
|
list_items = ListItem.objects.filter(book_list=blist).distinct()
|
||||||
book["list_items"][blist.name] = list(list_items.values())
|
book["list_items"][blist.name] = list(list_items.values())
|
||||||
|
|
||||||
# reviews
|
# reviews
|
||||||
reviews = models.Review.objects.filter(user=user, book=book["id"]).distinct()
|
reviews = Review.objects.filter(user=user, book=book["id"]).distinct()
|
||||||
|
|
||||||
book["reviews"] = list(reviews.values())
|
book["reviews"] = list(reviews.values())
|
||||||
|
|
||||||
# comments
|
# comments
|
||||||
comments = models.Comment.objects.filter(user=user, book=book["id"]).distinct()
|
comments = Comment.objects.filter(user=user, book=book["id"]).distinct()
|
||||||
|
|
||||||
book["comments"] = list(comments.values())
|
book["comments"] = list(comments.values())
|
||||||
|
|
||||||
# quotes
|
# quotes
|
||||||
quotes = models.Quotation.objects.filter(user=user, book=book["id"]).distinct()
|
quotes = Quotation.objects.filter(user=user, book=book["id"]).distinct()
|
||||||
|
|
||||||
book["quotes"] = list(quotes.values())
|
book["quotes"] = list(quotes.values())
|
||||||
|
|
||||||
|
@ -172,19 +175,19 @@ def json_export(user): # pylint: disable=too-many-locals, too-many-statements
|
||||||
final_books.append(book)
|
final_books.append(book)
|
||||||
|
|
||||||
# saved book lists
|
# saved book lists
|
||||||
saved_lists = models.List.objects.filter(id__in=user.saved_lists.all()).distinct()
|
saved_lists = List.objects.filter(id__in=user.saved_lists.all()).distinct()
|
||||||
saved_lists = [l.remote_id for l in saved_lists]
|
saved_lists = [l.remote_id for l in saved_lists]
|
||||||
|
|
||||||
# follows
|
# follows
|
||||||
follows = models.UserFollows.objects.filter(user_subject=user).distinct()
|
follows = UserFollows.objects.filter(user_subject=user).distinct()
|
||||||
following = models.User.objects.filter(
|
following = User.objects.filter(
|
||||||
userfollows_user_object__in=follows
|
userfollows_user_object__in=follows
|
||||||
).distinct()
|
).distinct()
|
||||||
follows = [f.remote_id for f in following]
|
follows = [f.remote_id for f in following]
|
||||||
|
|
||||||
# blocks
|
# blocks
|
||||||
blocks = models.UserBlocks.objects.filter(user_subject=user).distinct()
|
blocks = UserBlocks.objects.filter(user_subject=user).distinct()
|
||||||
blocking = models.User.objects.filter(userblocks_user_object__in=blocks).distinct()
|
blocking = User.objects.filter(userblocks_user_object__in=blocks).distinct()
|
||||||
|
|
||||||
blocks = [b.remote_id for b in blocking]
|
blocks = [b.remote_id for b in blocking]
|
||||||
|
|
||||||
|
@ -204,7 +207,7 @@ def get_books_for_user(user):
|
||||||
"""Get all the books and editions related to a user
|
"""Get all the books and editions related to a user
|
||||||
:returns: tuple of editions, books
|
:returns: tuple of editions, books
|
||||||
"""
|
"""
|
||||||
all_books = models.Edition.viewer_aware_objects(user)
|
all_books = Edition.viewer_aware_objects(user)
|
||||||
editions = all_books.filter(
|
editions = all_books.filter(
|
||||||
Q(shelves__user=user)
|
Q(shelves__user=user)
|
||||||
| Q(readthrough__user=user)
|
| Q(readthrough__user=user)
|
||||||
|
@ -213,5 +216,5 @@ def get_books_for_user(user):
|
||||||
| Q(comment__user=user)
|
| Q(comment__user=user)
|
||||||
| Q(quotation__user=user)
|
| Q(quotation__user=user)
|
||||||
).distinct()
|
).distinct()
|
||||||
books = models.Book.objects.filter(id__in=editions).distinct()
|
books = Book.objects.filter(id__in=editions).distinct()
|
||||||
return editions, books
|
return editions, books
|
||||||
|
|
Loading…
Reference in a new issue