forked from mirrors/bookwyrm
Split shelf view into multiple files
This commit is contained in:
parent
baba2e2057
commit
6a2f962f8d
3 changed files with 106 additions and 101 deletions
|
@ -40,8 +40,8 @@ from .landing.password import PasswordResetRequest, PasswordReset
|
|||
|
||||
# shelves
|
||||
from .shelf.shelf import Shelf
|
||||
from .shelf.shelf import create_shelf, delete_shelf
|
||||
from .shelf.shelf import shelve, unshelve
|
||||
from .shelf.shelf_actions import create_shelf, delete_shelf
|
||||
from .shelf.shelf_actions import shelve, unshelve
|
||||
|
||||
# misc views
|
||||
from .author import Author, EditAuthor
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
""" shelf views """
|
||||
from collections import namedtuple
|
||||
|
||||
from django.db import IntegrityError, transaction
|
||||
from django.db.models import OuterRef, Subquery, F
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.paginator import Paginator
|
||||
|
@ -11,12 +10,11 @@ from django.template.response import TemplateResponse
|
|||
from django.utils.decorators import method_decorator
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.views import View
|
||||
from django.views.decorators.http import require_POST
|
||||
|
||||
from bookwyrm import forms, models
|
||||
from bookwyrm.activitypub import ActivitypubResponse
|
||||
from bookwyrm.settings import PAGE_LENGTH
|
||||
from .helpers import is_api_request, get_user_from_username
|
||||
from bookwyrm.views.helpers import is_api_request, get_user_from_username
|
||||
|
||||
|
||||
# pylint: disable=no-self-use
|
||||
|
@ -128,102 +126,6 @@ class Shelf(View):
|
|||
return redirect(shelf.local_path)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def create_shelf(request):
|
||||
"""user generated shelves"""
|
||||
form = forms.ShelfForm(request.POST)
|
||||
if not form.is_valid():
|
||||
return redirect(request.headers.get("Referer", "/"))
|
||||
|
||||
shelf = form.save()
|
||||
return redirect(shelf.local_path)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def delete_shelf(request, shelf_id):
|
||||
"""user generated shelves"""
|
||||
shelf = get_object_or_404(models.Shelf, id=shelf_id)
|
||||
shelf.raise_not_deletable(request.user)
|
||||
|
||||
shelf.delete()
|
||||
return redirect("user-shelves", request.user.localname)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
@transaction.atomic
|
||||
def shelve(request):
|
||||
"""put a book on a user's shelf"""
|
||||
book = get_object_or_404(models.Edition, id=request.POST.get("book"))
|
||||
desired_shelf = get_object_or_404(
|
||||
request.user.shelf_set, identifier=request.POST.get("shelf")
|
||||
)
|
||||
|
||||
# first we need to remove from the specified shelf
|
||||
change_from_current_identifier = request.POST.get("change-shelf-from")
|
||||
if change_from_current_identifier:
|
||||
# find the shelfbook obj and delete it
|
||||
get_object_or_404(
|
||||
models.ShelfBook,
|
||||
book=book,
|
||||
user=request.user,
|
||||
shelf__identifier=change_from_current_identifier,
|
||||
).delete()
|
||||
|
||||
# A book can be on multiple shelves, but only on one read status shelf at a time
|
||||
if desired_shelf.identifier in models.Shelf.READ_STATUS_IDENTIFIERS:
|
||||
# figure out where state shelf it's currently on (if any)
|
||||
current_read_status_shelfbook = (
|
||||
models.ShelfBook.objects.select_related("shelf")
|
||||
.filter(
|
||||
shelf__identifier__in=models.Shelf.READ_STATUS_IDENTIFIERS,
|
||||
user=request.user,
|
||||
book=book,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if current_read_status_shelfbook is not None:
|
||||
if (
|
||||
current_read_status_shelfbook.shelf.identifier
|
||||
!= desired_shelf.identifier
|
||||
):
|
||||
current_read_status_shelfbook.delete()
|
||||
else: # It is already on the shelf
|
||||
return redirect(request.headers.get("Referer", "/"))
|
||||
|
||||
# create the new shelf-book entry
|
||||
models.ShelfBook.objects.create(
|
||||
book=book, shelf=desired_shelf, user=request.user
|
||||
)
|
||||
else:
|
||||
# we're putting it on a custom shelf
|
||||
try:
|
||||
models.ShelfBook.objects.create(
|
||||
book=book, shelf=desired_shelf, user=request.user
|
||||
)
|
||||
# The book is already on this shelf.
|
||||
# Might be good to alert, or reject the action?
|
||||
except IntegrityError:
|
||||
pass
|
||||
return redirect(request.headers.get("Referer", "/"))
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def unshelve(request):
|
||||
"""put a on a user's shelf"""
|
||||
book = get_object_or_404(models.Edition, id=request.POST.get("book"))
|
||||
shelf_book = get_object_or_404(
|
||||
models.ShelfBook, book=book, shelf__id=request.POST["shelf"]
|
||||
)
|
||||
shelf_book.raise_not_deletable(request.user)
|
||||
|
||||
shelf_book.delete()
|
||||
return redirect(request.headers.get("Referer", "/"))
|
||||
|
||||
|
||||
def sort_books(books, sort):
|
||||
"""Books in shelf sorting"""
|
||||
sort_fields = [
|
||||
|
|
103
bookwyrm/views/shelf/shelf_actions.py
Normal file
103
bookwyrm/views/shelf/shelf_actions.py
Normal file
|
@ -0,0 +1,103 @@
|
|||
""" shelf views """
|
||||
from django.db import IntegrityError, transaction
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.views.decorators.http import require_POST
|
||||
|
||||
from bookwyrm import forms, models
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def create_shelf(request):
|
||||
"""user generated shelves"""
|
||||
form = forms.ShelfForm(request.POST)
|
||||
if not form.is_valid():
|
||||
return redirect(request.headers.get("Referer", "/"))
|
||||
|
||||
shelf = form.save()
|
||||
return redirect(shelf.local_path)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def delete_shelf(request, shelf_id):
|
||||
"""user generated shelves"""
|
||||
shelf = get_object_or_404(models.Shelf, id=shelf_id)
|
||||
shelf.raise_not_deletable(request.user)
|
||||
|
||||
shelf.delete()
|
||||
return redirect("user-shelves", request.user.localname)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
@transaction.atomic
|
||||
def shelve(request):
|
||||
"""put a book on a user's shelf"""
|
||||
book = get_object_or_404(models.Edition, id=request.POST.get("book"))
|
||||
desired_shelf = get_object_or_404(
|
||||
request.user.shelf_set, identifier=request.POST.get("shelf")
|
||||
)
|
||||
|
||||
# first we need to remove from the specified shelf
|
||||
change_from_current_identifier = request.POST.get("change-shelf-from")
|
||||
if change_from_current_identifier:
|
||||
# find the shelfbook obj and delete it
|
||||
get_object_or_404(
|
||||
models.ShelfBook,
|
||||
book=book,
|
||||
user=request.user,
|
||||
shelf__identifier=change_from_current_identifier,
|
||||
).delete()
|
||||
|
||||
# A book can be on multiple shelves, but only on one read status shelf at a time
|
||||
if desired_shelf.identifier in models.Shelf.READ_STATUS_IDENTIFIERS:
|
||||
# figure out where state shelf it's currently on (if any)
|
||||
current_read_status_shelfbook = (
|
||||
models.ShelfBook.objects.select_related("shelf")
|
||||
.filter(
|
||||
shelf__identifier__in=models.Shelf.READ_STATUS_IDENTIFIERS,
|
||||
user=request.user,
|
||||
book=book,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if current_read_status_shelfbook is not None:
|
||||
if (
|
||||
current_read_status_shelfbook.shelf.identifier
|
||||
!= desired_shelf.identifier
|
||||
):
|
||||
current_read_status_shelfbook.delete()
|
||||
else: # It is already on the shelf
|
||||
return redirect(request.headers.get("Referer", "/"))
|
||||
|
||||
# create the new shelf-book entry
|
||||
models.ShelfBook.objects.create(
|
||||
book=book, shelf=desired_shelf, user=request.user
|
||||
)
|
||||
else:
|
||||
# we're putting it on a custom shelf
|
||||
try:
|
||||
models.ShelfBook.objects.create(
|
||||
book=book, shelf=desired_shelf, user=request.user
|
||||
)
|
||||
# The book is already on this shelf.
|
||||
# Might be good to alert, or reject the action?
|
||||
except IntegrityError:
|
||||
pass
|
||||
return redirect(request.headers.get("Referer", "/"))
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def unshelve(request):
|
||||
"""put a on a user's shelf"""
|
||||
book = get_object_or_404(models.Edition, id=request.POST.get("book"))
|
||||
shelf_book = get_object_or_404(
|
||||
models.ShelfBook, book=book, shelf__id=request.POST["shelf"]
|
||||
)
|
||||
shelf_book.raise_not_deletable(request.user)
|
||||
|
||||
shelf_book.delete()
|
||||
return redirect(request.headers.get("Referer", "/"))
|
Loading…
Reference in a new issue