Adds helper function for loading editions

This commit is contained in:
Mouse Reeve 2020-05-10 18:08:18 -07:00
parent e11e1cc560
commit d8aadb4587
4 changed files with 14 additions and 8 deletions

View file

@ -8,6 +8,14 @@ from fedireads import models, settings
from fedireads.tasks import app
def get_edition(book_id):
''' look up a book in the db and return an edition '''
book = models.Book.objects.select_subclasses().get(id=book_id)
if isinstance(book, models.Work):
book = book.default_edition
return book
def get_or_create_book(remote_id):
''' pull up a book record by whatever means possible '''
book = get_by_absolute_id(remote_id, models.Book)

View file

@ -263,7 +263,7 @@ def handle_comment(user, book, content):
def handle_status(user, book_id, \
builder, fr_serializer, ap_serializer, *args):
''' generic handler for statuses '''
book = models.Book.objects.get(id=book_id)
book = models.Edition.objects.get(id=book_id)
status = builder(user, book, *args)
activity = fr_serializer(status)

View file

@ -9,11 +9,11 @@ from django.http import HttpResponseBadRequest, HttpResponseNotFound
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from fedireads import books_manager
from fedireads import forms, models, outgoing
from fedireads import goodreads_import
from fedireads.settings import DOMAIN
from fedireads.views import get_user_from_username
from fedireads.books_manager import get_or_create_book
def user_login(request):
@ -118,7 +118,7 @@ def edit_profile(request):
def resolve_book(request):
''' figure out the local path to a book from a remote_id '''
remote_id = request.POST.get('remote_id')
book = get_or_create_book(remote_id)
book = books_manager.get_or_create_book(remote_id)
return redirect('/book/%d' % book.id)
@ -151,7 +151,7 @@ def upload_cover(request, book_id):
try:
book = models.Edition.objects.get(id=book_id)
except models.Book.DoesNotExist:
except models.Edition.DoesNotExist:
return HttpResponseNotFound()
form = forms.CoverForm(request.POST, request.FILES, instance=book)
@ -169,9 +169,7 @@ def upload_cover(request, book_id):
@login_required
def shelve(request):
''' put a on a user's shelf '''
book = models.Book.objects.select_subclasses().get(id=request.POST['book'])
if isinstance(book, models.Work):
book = book.default_edition
book = books_manager.get_edition(request.POST['book'])
desired_shelf = models.Shelf.objects.filter(
identifier=request.POST['shelf'],

View file

@ -469,7 +469,7 @@ def book_page(request, book_id, tab='friends'):
@login_required
def edit_book_page(request, book_id):
''' info about a book '''
book = models.Book.objects.get(id=book_id)
book = books_manager.get_edition(book_id)
if not book.description:
book.description = book.parent_work.description
data = {