Merge branch 'main' into progress_update
This commit is contained in:
26
bookwyrm/views/__init__.py
Normal file
26
bookwyrm/views/__init__.py
Normal file
@ -0,0 +1,26 @@
|
||||
''' make sure all our nice views are available '''
|
||||
from .authentication import Login, Register, Logout
|
||||
from .author import Author, EditAuthor
|
||||
from .books import Book, EditBook, Editions
|
||||
from .books import upload_cover, add_description, switch_edition, resolve_book
|
||||
from .direct_message import DirectMessage
|
||||
from .error import not_found_page, server_error_page
|
||||
from .follow import follow, unfollow
|
||||
from .follow import accept_follow_request, delete_follow_request, handle_accept
|
||||
from .goal import Goal
|
||||
from .import_data import Import, ImportStatus
|
||||
from .interaction import Favorite, Unfavorite, Boost, Unboost
|
||||
from .invite import ManageInvites, Invite
|
||||
from .landing import About, Home, Feed, Discover
|
||||
from .notifications import Notifications
|
||||
from .outbox import Outbox
|
||||
from .reading import edit_readthrough, create_readthrough, delete_readthrough
|
||||
from .reading import start_reading, finish_reading
|
||||
from .password import PasswordResetRequest, PasswordReset, ChangePassword
|
||||
from .tag import Tag, AddTag, RemoveTag
|
||||
from .search import Search
|
||||
from .shelf import Shelf
|
||||
from .shelf import user_shelves_page, create_shelf, delete_shelf
|
||||
from .shelf import shelve, unshelve
|
||||
from .status import Status, Replies, CreateStatus, DeleteStatus
|
||||
from .user import User, EditUser, Followers, Following
|
113
bookwyrm/views/authentication.py
Normal file
113
bookwyrm/views/authentication.py
Normal file
@ -0,0 +1,113 @@
|
||||
''' class views for login/register views '''
|
||||
from django.contrib.auth import authenticate, login, logout
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils import timezone
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import forms, models
|
||||
from bookwyrm.settings import DOMAIN
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
class Login(View):
|
||||
''' authenticate an existing user '''
|
||||
def get(self, request):
|
||||
''' login page '''
|
||||
if request.user.is_authenticated:
|
||||
return redirect('/')
|
||||
# sene user to the login page
|
||||
data = {
|
||||
'title': 'Login',
|
||||
'login_form': forms.LoginForm(),
|
||||
'register_form': forms.RegisterForm(),
|
||||
}
|
||||
return TemplateResponse(request, 'login.html', data)
|
||||
|
||||
def post(self, request):
|
||||
''' authentication action '''
|
||||
login_form = forms.LoginForm(request.POST)
|
||||
|
||||
localname = login_form.data['localname']
|
||||
username = '%s@%s' % (localname, DOMAIN)
|
||||
password = login_form.data['password']
|
||||
user = authenticate(request, username=username, password=password)
|
||||
if user is not None:
|
||||
# successful login
|
||||
login(request, user)
|
||||
user.last_active_date = timezone.now()
|
||||
return redirect(request.GET.get('next', '/'))
|
||||
|
||||
# login errors
|
||||
login_form.non_field_errors = 'Username or password are incorrect'
|
||||
register_form = forms.RegisterForm()
|
||||
data = {
|
||||
'login_form': login_form,
|
||||
'register_form': register_form
|
||||
}
|
||||
return TemplateResponse(request, 'login.html', data)
|
||||
|
||||
|
||||
class Register(View):
|
||||
''' register a user '''
|
||||
def post(self, request):
|
||||
''' join the server '''
|
||||
if not models.SiteSettings.get().allow_registration:
|
||||
invite_code = request.POST.get('invite_code')
|
||||
|
||||
if not invite_code:
|
||||
raise PermissionDenied
|
||||
|
||||
invite = get_object_or_404(models.SiteInvite, code=invite_code)
|
||||
if not invite.valid():
|
||||
raise PermissionDenied
|
||||
else:
|
||||
invite = None
|
||||
|
||||
form = forms.RegisterForm(request.POST)
|
||||
errors = False
|
||||
if not form.is_valid():
|
||||
errors = True
|
||||
|
||||
localname = form.data['localname'].strip()
|
||||
email = form.data['email']
|
||||
password = form.data['password']
|
||||
|
||||
# check localname and email uniqueness
|
||||
if models.User.objects.filter(localname=localname).first():
|
||||
form.errors['localname'] = [
|
||||
'User with this username already exists']
|
||||
errors = True
|
||||
|
||||
if errors:
|
||||
data = {
|
||||
'login_form': forms.LoginForm(),
|
||||
'register_form': form,
|
||||
'invite': invite,
|
||||
'valid': invite.valid() if invite else True,
|
||||
}
|
||||
if invite:
|
||||
return TemplateResponse(request, 'invite.html', data)
|
||||
return TemplateResponse(request, 'login.html', data)
|
||||
|
||||
username = '%s@%s' % (localname, DOMAIN)
|
||||
user = models.User.objects.create_user(
|
||||
username, email, password, localname=localname, local=True)
|
||||
if invite:
|
||||
invite.times_used += 1
|
||||
invite.save()
|
||||
|
||||
login(request, user)
|
||||
return redirect('/')
|
||||
|
||||
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class Logout(View):
|
||||
''' log out '''
|
||||
def get(self, request):
|
||||
''' done with this place! outa here! '''
|
||||
logout(request)
|
||||
return redirect('/')
|
66
bookwyrm/views/author.py
Normal file
66
bookwyrm/views/author.py
Normal file
@ -0,0 +1,66 @@
|
||||
''' the good people stuff! the authors! '''
|
||||
from django.contrib.auth.decorators import login_required, permission_required
|
||||
from django.db.models import Q
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import forms, models
|
||||
from bookwyrm.activitypub import ActivitypubResponse
|
||||
from bookwyrm.broadcast import broadcast
|
||||
from .helpers import is_api_request
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
class Author(View):
|
||||
''' this person wrote a book '''
|
||||
def get(self, request, author_id):
|
||||
''' landing page for an author '''
|
||||
author = get_object_or_404(models.Author, id=author_id)
|
||||
|
||||
if is_api_request(request):
|
||||
return ActivitypubResponse(author.to_activity())
|
||||
|
||||
books = models.Work.objects.filter(
|
||||
Q(authors=author) | Q(editions__authors=author)).distinct()
|
||||
data = {
|
||||
'title': author.name,
|
||||
'author': author,
|
||||
'books': [b.get_default_edition() for b in books],
|
||||
}
|
||||
return TemplateResponse(request, 'author.html', data)
|
||||
|
||||
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
@method_decorator(
|
||||
permission_required('bookwyrm.edit_book', raise_exception=True),
|
||||
name='dispatch')
|
||||
class EditAuthor(View):
|
||||
''' edit author info '''
|
||||
def get(self, request, author_id):
|
||||
''' info about a book '''
|
||||
author = get_object_or_404(models.Author, id=author_id)
|
||||
data = {
|
||||
'title': 'Edit Author',
|
||||
'author': author,
|
||||
'form': forms.AuthorForm(instance=author)
|
||||
}
|
||||
return TemplateResponse(request, 'edit_author.html', data)
|
||||
|
||||
def post(self, request, author_id):
|
||||
''' edit a author cool '''
|
||||
author = get_object_or_404(models.Author, id=author_id)
|
||||
|
||||
form = forms.AuthorForm(request.POST, request.FILES, instance=author)
|
||||
if not form.is_valid():
|
||||
data = {
|
||||
'title': 'Edit Author',
|
||||
'author': author,
|
||||
'form': form
|
||||
}
|
||||
return TemplateResponse(request, 'edit_author.html', data)
|
||||
author = form.save()
|
||||
|
||||
broadcast(request.user, author.to_update_activity(request.user))
|
||||
return redirect('/author/%s' % author.id)
|
233
bookwyrm/views/books.py
Normal file
233
bookwyrm/views/books.py
Normal file
@ -0,0 +1,233 @@
|
||||
''' the good stuff! the books! '''
|
||||
from django.core.paginator import Paginator
|
||||
from django.contrib.auth.decorators import login_required, permission_required
|
||||
from django.db import transaction
|
||||
from django.db.models import Avg, Q
|
||||
from django.http import HttpResponseNotFound
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
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.broadcast import broadcast
|
||||
from bookwyrm.connectors import connector_manager
|
||||
from bookwyrm.settings import PAGE_LENGTH
|
||||
from .helpers import is_api_request, get_activity_feed, get_edition
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
class Book(View):
|
||||
''' a book! this is the stuff '''
|
||||
def get(self, request, book_id):
|
||||
''' info about a book '''
|
||||
try:
|
||||
page = int(request.GET.get('page', 1))
|
||||
except ValueError:
|
||||
page = 1
|
||||
|
||||
try:
|
||||
book = models.Book.objects.select_subclasses().get(id=book_id)
|
||||
except models.Book.DoesNotExist:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
if is_api_request(request):
|
||||
return ActivitypubResponse(book.to_activity())
|
||||
|
||||
if isinstance(book, models.Work):
|
||||
book = book.get_default_edition()
|
||||
if not book:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
work = book.parent_work
|
||||
if not work:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
reviews = models.Review.objects.filter(
|
||||
book__in=work.editions.all(),
|
||||
)
|
||||
# all reviews for the book
|
||||
reviews = get_activity_feed(
|
||||
request.user,
|
||||
['public', 'unlisted', 'followers', 'direct'],
|
||||
queryset=reviews
|
||||
)
|
||||
|
||||
# the reviews to show
|
||||
paginated = Paginator(reviews.exclude(
|
||||
Q(content__isnull=True) | Q(content='')
|
||||
), PAGE_LENGTH)
|
||||
reviews_page = paginated.page(page)
|
||||
|
||||
user_tags = readthroughs = user_shelves = other_edition_shelves = []
|
||||
if request.user.is_authenticated:
|
||||
user_tags = models.UserTag.objects.filter(
|
||||
book=book, user=request.user
|
||||
).values_list('tag__identifier', flat=True)
|
||||
|
||||
readthroughs = models.ReadThrough.objects.filter(
|
||||
user=request.user,
|
||||
book=book,
|
||||
).order_by('start_date')
|
||||
|
||||
for readthrough in readthroughs:
|
||||
readthrough.progress_updates = \
|
||||
readthrough.progressupdate_set.all().order_by('-updated_date')
|
||||
|
||||
user_shelves = models.ShelfBook.objects.filter(
|
||||
added_by=request.user, book=book
|
||||
)
|
||||
|
||||
other_edition_shelves = models.ShelfBook.objects.filter(
|
||||
~Q(book=book),
|
||||
added_by=request.user,
|
||||
book__parent_work=book.parent_work,
|
||||
)
|
||||
|
||||
data = {
|
||||
'title': book.title,
|
||||
'book': book,
|
||||
'reviews': reviews_page,
|
||||
'review_count': reviews.count(),
|
||||
'ratings': reviews.filter(Q(content__isnull=True) | Q(content='')),
|
||||
'rating': reviews.aggregate(Avg('rating'))['rating__avg'],
|
||||
'tags': models.UserTag.objects.filter(book=book),
|
||||
'user_tags': user_tags,
|
||||
'user_shelves': user_shelves,
|
||||
'other_edition_shelves': other_edition_shelves,
|
||||
'readthroughs': readthroughs,
|
||||
'show_progress': ('showprogress' in request.GET),
|
||||
'path': '/book/%s' % book_id,
|
||||
}
|
||||
return TemplateResponse(request, 'book.html', data)
|
||||
|
||||
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
@method_decorator(
|
||||
permission_required('bookwyrm.edit_book', raise_exception=True),
|
||||
name='dispatch')
|
||||
class EditBook(View):
|
||||
''' edit a book '''
|
||||
def get(self, request, book_id):
|
||||
''' info about a book '''
|
||||
book = get_edition(book_id)
|
||||
if not book.description:
|
||||
book.description = book.parent_work.description
|
||||
data = {
|
||||
'title': 'Edit Book',
|
||||
'book': book,
|
||||
'form': forms.EditionForm(instance=book)
|
||||
}
|
||||
return TemplateResponse(request, 'edit_book.html', data)
|
||||
|
||||
def post(self, request, book_id):
|
||||
''' edit a book cool '''
|
||||
book = get_object_or_404(models.Edition, id=book_id)
|
||||
|
||||
form = forms.EditionForm(request.POST, request.FILES, instance=book)
|
||||
if not form.is_valid():
|
||||
data = {
|
||||
'title': 'Edit Book',
|
||||
'book': book,
|
||||
'form': form
|
||||
}
|
||||
return TemplateResponse(request, 'edit_book.html', data)
|
||||
book = form.save()
|
||||
|
||||
broadcast(request.user, book.to_update_activity(request.user))
|
||||
return redirect('/book/%s' % book.id)
|
||||
|
||||
|
||||
class Editions(View):
|
||||
''' list of editions '''
|
||||
def get(self, request, book_id):
|
||||
''' list of editions of a book '''
|
||||
work = get_object_or_404(models.Work, id=book_id)
|
||||
|
||||
if is_api_request(request):
|
||||
return ActivitypubResponse(work.to_edition_list(**request.GET))
|
||||
|
||||
data = {
|
||||
'title': 'Editions of %s' % work.title,
|
||||
'editions': work.editions.order_by('-edition_rank').all(),
|
||||
'work': work,
|
||||
}
|
||||
return TemplateResponse(request, 'editions.html', data)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def upload_cover(request, book_id):
|
||||
''' upload a new cover '''
|
||||
book = get_object_or_404(models.Edition, id=book_id)
|
||||
|
||||
form = forms.CoverForm(request.POST, request.FILES, instance=book)
|
||||
if not form.is_valid():
|
||||
return redirect('/book/%d' % book.id)
|
||||
|
||||
book.cover = form.files['cover']
|
||||
book.save()
|
||||
|
||||
broadcast(request.user, book.to_update_activity(request.user))
|
||||
return redirect('/book/%s' % book.id)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
@permission_required('bookwyrm.edit_book', raise_exception=True)
|
||||
def add_description(request, book_id):
|
||||
''' upload a new cover '''
|
||||
if not request.method == 'POST':
|
||||
return redirect('/')
|
||||
|
||||
book = get_object_or_404(models.Edition, id=book_id)
|
||||
|
||||
description = request.POST.get('description')
|
||||
|
||||
book.description = description
|
||||
book.save()
|
||||
|
||||
broadcast(request.user, book.to_update_activity(request.user))
|
||||
return redirect('/book/%s' % book.id)
|
||||
|
||||
|
||||
@require_POST
|
||||
def resolve_book(request):
|
||||
''' figure out the local path to a book from a remote_id '''
|
||||
remote_id = request.POST.get('remote_id')
|
||||
connector = connector_manager.get_or_create_connector(remote_id)
|
||||
book = connector.get_or_create_book(remote_id)
|
||||
|
||||
return redirect('/book/%d' % book.id)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
@transaction.atomic
|
||||
def switch_edition(request):
|
||||
''' switch your copy of a book to a different edition '''
|
||||
edition_id = request.POST.get('edition')
|
||||
new_edition = get_object_or_404(models.Edition, id=edition_id)
|
||||
shelfbooks = models.ShelfBook.objects.filter(
|
||||
book__parent_work=new_edition.parent_work,
|
||||
shelf__user=request.user
|
||||
)
|
||||
for shelfbook in shelfbooks.all():
|
||||
broadcast(request.user, shelfbook.to_remove_activity(request.user))
|
||||
|
||||
shelfbook.book = new_edition
|
||||
shelfbook.save()
|
||||
|
||||
broadcast(request.user, shelfbook.to_add_activity(request.user))
|
||||
|
||||
readthroughs = models.ReadThrough.objects.filter(
|
||||
book__parent_work=new_edition.parent_work,
|
||||
user=request.user
|
||||
)
|
||||
for readthrough in readthroughs.all():
|
||||
readthrough.book = new_edition
|
||||
readthrough.save()
|
||||
|
||||
return redirect('/book/%d' % new_edition.id)
|
26
bookwyrm/views/direct_message.py
Normal file
26
bookwyrm/views/direct_message.py
Normal file
@ -0,0 +1,26 @@
|
||||
''' non-interactive pages '''
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.paginator import Paginator
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm.settings import PAGE_LENGTH
|
||||
from .helpers import get_activity_feed
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class DirectMessage(View):
|
||||
''' dm view '''
|
||||
def get(self, request, page=1):
|
||||
''' like a feed but for dms only '''
|
||||
activities = get_activity_feed(request.user, 'direct')
|
||||
paginated = Paginator(activities, PAGE_LENGTH)
|
||||
activity_page = paginated.page(page)
|
||||
data = {
|
||||
'title': 'Direct Messages',
|
||||
'user': request.user,
|
||||
'activities': activity_page,
|
||||
}
|
||||
return TemplateResponse(request, 'direct_messages.html', data)
|
13
bookwyrm/views/error.py
Normal file
13
bookwyrm/views/error.py
Normal file
@ -0,0 +1,13 @@
|
||||
''' something has gone amiss '''
|
||||
from django.template.response import TemplateResponse
|
||||
|
||||
def server_error_page(request):
|
||||
''' 500 errors '''
|
||||
return TemplateResponse(
|
||||
request, 'error.html', {'title': 'Oops!'}, status=500)
|
||||
|
||||
|
||||
def not_found_page(request, _):
|
||||
''' 404s '''
|
||||
return TemplateResponse(
|
||||
request, 'notfound.html', {'title': 'Not found'}, status=404)
|
113
bookwyrm/views/follow.py
Normal file
113
bookwyrm/views/follow.py
Normal file
@ -0,0 +1,113 @@
|
||||
''' views for actions you can take in the application '''
|
||||
from django.db import transaction
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpResponseBadRequest
|
||||
from django.shortcuts import redirect
|
||||
from django.views.decorators.http import require_POST
|
||||
|
||||
from bookwyrm import models
|
||||
from bookwyrm.broadcast import broadcast
|
||||
from .helpers import get_user_from_username
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def follow(request):
|
||||
''' follow another user, here or abroad '''
|
||||
username = request.POST['user']
|
||||
try:
|
||||
to_follow = get_user_from_username(username)
|
||||
except models.User.DoesNotExist:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
relationship, _ = models.UserFollowRequest.objects.get_or_create(
|
||||
user_subject=request.user,
|
||||
user_object=to_follow,
|
||||
)
|
||||
activity = relationship.to_activity()
|
||||
broadcast(
|
||||
request.user, activity, privacy='direct', direct_recipients=[to_follow])
|
||||
return redirect(to_follow.local_path)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def unfollow(request):
|
||||
''' unfollow a user '''
|
||||
username = request.POST['user']
|
||||
try:
|
||||
to_unfollow = get_user_from_username(username)
|
||||
except models.User.DoesNotExist:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
relationship = models.UserFollows.objects.get(
|
||||
user_subject=request.user,
|
||||
user_object=to_unfollow
|
||||
)
|
||||
activity = relationship.to_undo_activity(request.user)
|
||||
broadcast(
|
||||
request.user, activity,
|
||||
privacy='direct', direct_recipients=[to_unfollow])
|
||||
|
||||
to_unfollow.followers.remove(request.user)
|
||||
return redirect(to_unfollow.local_path)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def accept_follow_request(request):
|
||||
''' a user accepts a follow request '''
|
||||
username = request.POST['user']
|
||||
try:
|
||||
requester = get_user_from_username(username)
|
||||
except models.User.DoesNotExist:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
try:
|
||||
follow_request = models.UserFollowRequest.objects.get(
|
||||
user_subject=requester,
|
||||
user_object=request.user
|
||||
)
|
||||
except models.UserFollowRequest.DoesNotExist:
|
||||
# Request already dealt with.
|
||||
return redirect(request.user.local_path)
|
||||
handle_accept(follow_request)
|
||||
|
||||
return redirect(request.user.local_path)
|
||||
|
||||
|
||||
def handle_accept(follow_request):
|
||||
''' send an acceptance message to a follow request '''
|
||||
user = follow_request.user_subject
|
||||
to_follow = follow_request.user_object
|
||||
with transaction.atomic():
|
||||
relationship = models.UserFollows.from_request(follow_request)
|
||||
follow_request.delete()
|
||||
relationship.save()
|
||||
|
||||
activity = relationship.to_accept_activity()
|
||||
broadcast(to_follow, activity, privacy='direct', direct_recipients=[user])
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def delete_follow_request(request):
|
||||
''' a user rejects a follow request '''
|
||||
username = request.POST['user']
|
||||
try:
|
||||
requester = get_user_from_username(username)
|
||||
except models.User.DoesNotExist:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
try:
|
||||
follow_request = models.UserFollowRequest.objects.get(
|
||||
user_subject=requester,
|
||||
user_object=request.user
|
||||
)
|
||||
except models.UserFollowRequest.DoesNotExist:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
activity = follow_request.to_reject_activity()
|
||||
follow_request.delete()
|
||||
broadcast(
|
||||
request.user, activity, privacy='direct', direct_recipients=[requester])
|
||||
return redirect('/user/%s' % request.user.localname)
|
79
bookwyrm/views/goal.py
Normal file
79
bookwyrm/views/goal.py
Normal file
@ -0,0 +1,79 @@
|
||||
''' non-interactive pages '''
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpResponseNotFound
|
||||
from django.shortcuts import redirect
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import forms, models
|
||||
from bookwyrm.broadcast import broadcast
|
||||
from bookwyrm.status import create_generated_note
|
||||
from .helpers import get_user_from_username, object_visible_to_user
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class Goal(View):
|
||||
''' track books for the year '''
|
||||
def get(self, request, username, year):
|
||||
''' reading goal page '''
|
||||
user = get_user_from_username(username)
|
||||
year = int(year)
|
||||
goal = models.AnnualGoal.objects.filter(
|
||||
year=year, user=user
|
||||
).first()
|
||||
if not goal and user != request.user:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
if goal and not object_visible_to_user(request.user, goal):
|
||||
return HttpResponseNotFound()
|
||||
|
||||
data = {
|
||||
'title': '%s\'s %d Reading' % (user.display_name, year),
|
||||
'goal_form': forms.GoalForm(instance=goal),
|
||||
'goal': goal,
|
||||
'user': user,
|
||||
'year': year,
|
||||
}
|
||||
return TemplateResponse(request, 'goal.html', data)
|
||||
|
||||
|
||||
def post(self, request, username, year):
|
||||
''' update or create an annual goal '''
|
||||
user = get_user_from_username(username)
|
||||
if user != request.user:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
year = int(year)
|
||||
goal = models.AnnualGoal.objects.filter(
|
||||
year=year, user=request.user
|
||||
).first()
|
||||
form = forms.GoalForm(request.POST, instance=goal)
|
||||
if not form.is_valid():
|
||||
data = {
|
||||
'title': '%s\'s %d Reading' % (goal.user.display_name, year),
|
||||
'goal_form': form,
|
||||
'goal': goal,
|
||||
'year': year,
|
||||
}
|
||||
return TemplateResponse(request, 'goal.html', data)
|
||||
goal = form.save()
|
||||
|
||||
if request.POST.get('post-status'):
|
||||
# create status, if appropraite
|
||||
status = create_generated_note(
|
||||
request.user,
|
||||
'set a goal to read %d books in %d' % (goal.goal, goal.year),
|
||||
privacy=goal.privacy
|
||||
)
|
||||
broadcast(
|
||||
request.user,
|
||||
status.to_create_activity(request.user),
|
||||
software='bookwyrm')
|
||||
|
||||
# re-format the activity for non-bookwyrm servers
|
||||
remote_activity = status.to_create_activity(request.user, pure=True)
|
||||
broadcast(request.user, remote_activity, software='other')
|
||||
|
||||
return redirect(request.headers.get('Referer', '/'))
|
174
bookwyrm/views/helpers.py
Normal file
174
bookwyrm/views/helpers.py
Normal file
@ -0,0 +1,174 @@
|
||||
''' helper functions used in various views '''
|
||||
import re
|
||||
from requests import HTTPError
|
||||
from django.db.models import Q
|
||||
|
||||
from bookwyrm import activitypub, models
|
||||
from bookwyrm.broadcast import broadcast
|
||||
from bookwyrm.connectors import ConnectorException, get_data
|
||||
from bookwyrm.status import create_generated_note
|
||||
from bookwyrm.utils import regex
|
||||
|
||||
|
||||
def get_user_from_username(username):
|
||||
''' helper function to resolve a localname or a username to a user '''
|
||||
# raises DoesNotExist if user is now found
|
||||
try:
|
||||
return models.User.objects.get(localname=username)
|
||||
except models.User.DoesNotExist:
|
||||
return models.User.objects.get(username=username)
|
||||
|
||||
|
||||
def is_api_request(request):
|
||||
''' check whether a request is asking for html or data '''
|
||||
return 'json' in request.headers.get('Accept') or \
|
||||
request.path[-5:] == '.json'
|
||||
|
||||
|
||||
def is_bookworm_request(request):
|
||||
''' check if the request is coming from another bookworm instance '''
|
||||
user_agent = request.headers.get('User-Agent')
|
||||
if user_agent is None or \
|
||||
re.search(regex.bookwyrm_user_agent, user_agent) is None:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def object_visible_to_user(viewer, obj):
|
||||
''' is a user authorized to view an object? '''
|
||||
if viewer == obj.user or obj.privacy in ['public', 'unlisted']:
|
||||
return True
|
||||
if obj.privacy == 'followers' and \
|
||||
obj.user.followers.filter(id=viewer.id).first():
|
||||
return True
|
||||
if isinstance(obj, models.Status):
|
||||
if obj.privacy == 'direct' and \
|
||||
obj.mention_users.filter(id=viewer.id).first():
|
||||
return True
|
||||
return False
|
||||
|
||||
def get_activity_feed(
|
||||
user, privacy, local_only=False, following_only=False,
|
||||
queryset=models.Status.objects):
|
||||
''' get a filtered queryset of statuses '''
|
||||
privacy = privacy if isinstance(privacy, list) else [privacy]
|
||||
# if we're looking at Status, we need this. We don't if it's Comment
|
||||
if hasattr(queryset, 'select_subclasses'):
|
||||
queryset = queryset.select_subclasses()
|
||||
|
||||
# exclude deleted
|
||||
queryset = queryset.exclude(deleted=True).order_by('-published_date')
|
||||
|
||||
# you can't see followers only or direct messages if you're not logged in
|
||||
if user.is_anonymous:
|
||||
privacy = [p for p in privacy if not p in ['followers', 'direct']]
|
||||
|
||||
# filter to only privided privacy levels
|
||||
queryset = queryset.filter(privacy__in=privacy)
|
||||
|
||||
# only include statuses the user follows
|
||||
if following_only:
|
||||
queryset = queryset.exclude(
|
||||
~Q(# remove everythign except
|
||||
Q(user__in=user.following.all()) | # user follwoing
|
||||
Q(user=user) |# is self
|
||||
Q(mention_users=user)# mentions user
|
||||
),
|
||||
)
|
||||
# exclude followers-only statuses the user doesn't follow
|
||||
elif 'followers' in privacy:
|
||||
queryset = queryset.exclude(
|
||||
~Q(# user isn't following and it isn't their own status
|
||||
Q(user__in=user.following.all()) | Q(user=user)
|
||||
),
|
||||
privacy='followers' # and the status is followers only
|
||||
)
|
||||
|
||||
# exclude direct messages not intended for the user
|
||||
if 'direct' in privacy:
|
||||
queryset = queryset.exclude(
|
||||
~Q(
|
||||
Q(user=user) | Q(mention_users=user)
|
||||
), privacy='direct'
|
||||
)
|
||||
|
||||
# filter for only local status
|
||||
if local_only:
|
||||
queryset = queryset.filter(user__local=True)
|
||||
|
||||
# remove statuses that have boosts in the same queryset
|
||||
try:
|
||||
queryset = queryset.filter(~Q(boosters__in=queryset))
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
return queryset
|
||||
|
||||
|
||||
def handle_remote_webfinger(query):
|
||||
''' webfingerin' other servers '''
|
||||
user = None
|
||||
|
||||
# usernames could be @user@domain or user@domain
|
||||
if not query:
|
||||
return None
|
||||
|
||||
if query[0] == '@':
|
||||
query = query[1:]
|
||||
|
||||
try:
|
||||
domain = query.split('@')[1]
|
||||
except IndexError:
|
||||
return None
|
||||
|
||||
try:
|
||||
user = models.User.objects.get(username=query)
|
||||
except models.User.DoesNotExist:
|
||||
url = 'https://%s/.well-known/webfinger?resource=acct:%s' % \
|
||||
(domain, query)
|
||||
try:
|
||||
data = get_data(url)
|
||||
except (ConnectorException, HTTPError):
|
||||
return None
|
||||
|
||||
for link in data.get('links'):
|
||||
if link.get('rel') == 'self':
|
||||
try:
|
||||
user = activitypub.resolve_remote_id(
|
||||
models.User, link['href']
|
||||
)
|
||||
except KeyError:
|
||||
return None
|
||||
return user
|
||||
|
||||
|
||||
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.get_default_edition()
|
||||
return book
|
||||
|
||||
|
||||
def handle_reading_status(user, shelf, book, privacy):
|
||||
''' post about a user reading a book '''
|
||||
# tell the world about this cool thing that happened
|
||||
try:
|
||||
message = {
|
||||
'to-read': 'wants to read',
|
||||
'reading': 'started reading',
|
||||
'read': 'finished reading'
|
||||
}[shelf.identifier]
|
||||
except KeyError:
|
||||
# it's a non-standard shelf, don't worry about it
|
||||
return
|
||||
|
||||
status = create_generated_note(
|
||||
user,
|
||||
message,
|
||||
mention_books=[book],
|
||||
privacy=privacy
|
||||
)
|
||||
status.save()
|
||||
|
||||
broadcast(user, status.to_create_activity(user))
|
83
bookwyrm/views/import_data.py
Normal file
83
bookwyrm/views/import_data.py
Normal file
@ -0,0 +1,83 @@
|
||||
''' import books from another app '''
|
||||
from io import TextIOWrapper
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.http import HttpResponseBadRequest
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import forms, goodreads_import, models
|
||||
from bookwyrm.tasks import app
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class Import(View):
|
||||
''' import view '''
|
||||
def get(self, request):
|
||||
''' load import page '''
|
||||
return TemplateResponse(request, 'import.html', {
|
||||
'title': 'Import Books',
|
||||
'import_form': forms.ImportForm(),
|
||||
'jobs': models.ImportJob.
|
||||
objects.filter(user=request.user).order_by('-created_date'),
|
||||
})
|
||||
|
||||
def post(self, request):
|
||||
''' ingest a goodreads csv '''
|
||||
form = forms.ImportForm(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
include_reviews = request.POST.get('include_reviews') == 'on'
|
||||
privacy = request.POST.get('privacy')
|
||||
try:
|
||||
job = goodreads_import.create_job(
|
||||
request.user,
|
||||
TextIOWrapper(
|
||||
request.FILES['csv_file'],
|
||||
encoding=request.encoding),
|
||||
include_reviews,
|
||||
privacy,
|
||||
)
|
||||
except (UnicodeDecodeError, ValueError):
|
||||
return HttpResponseBadRequest('Not a valid csv file')
|
||||
goodreads_import.start_import(job)
|
||||
return redirect('/import-status/%d' % job.id)
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class ImportStatus(View):
|
||||
''' status of an existing import '''
|
||||
def get(self, request, job_id):
|
||||
''' status of an import job '''
|
||||
job = models.ImportJob.objects.get(id=job_id)
|
||||
if job.user != request.user:
|
||||
raise PermissionDenied
|
||||
task = app.AsyncResult(job.task_id)
|
||||
items = job.items.order_by('index').all()
|
||||
failed_items = [i for i in items if i.fail_reason]
|
||||
items = [i for i in items if not i.fail_reason]
|
||||
return TemplateResponse(request, 'import_status.html', {
|
||||
'title': 'Import Status',
|
||||
'job': job,
|
||||
'items': items,
|
||||
'failed_items': failed_items,
|
||||
'task': task
|
||||
})
|
||||
|
||||
def post(self, request, job_id):
|
||||
''' retry lines from an import '''
|
||||
job = get_object_or_404(models.ImportJob, id=job_id)
|
||||
items = []
|
||||
for item in request.POST.getlist('import_item'):
|
||||
items.append(get_object_or_404(models.ImportItem, id=item))
|
||||
|
||||
job = goodreads_import.create_retry_job(
|
||||
request.user,
|
||||
job,
|
||||
items,
|
||||
)
|
||||
goodreads_import.start_import(job)
|
||||
return redirect('/import-status/%d' % job.id)
|
130
bookwyrm/views/interaction.py
Normal file
130
bookwyrm/views/interaction.py
Normal file
@ -0,0 +1,130 @@
|
||||
''' boosts and favs '''
|
||||
from django.db import IntegrityError
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpResponseBadRequest, HttpResponseNotFound
|
||||
from django.shortcuts import redirect
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import models
|
||||
from bookwyrm.broadcast import broadcast
|
||||
from bookwyrm.status import create_notification
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class Favorite(View):
|
||||
''' like a status '''
|
||||
def post(self, request, status_id):
|
||||
''' create a like '''
|
||||
status = models.Status.objects.get(id=status_id)
|
||||
try:
|
||||
favorite = models.Favorite.objects.create(
|
||||
status=status,
|
||||
user=request.user
|
||||
)
|
||||
except IntegrityError:
|
||||
# you already fav'ed that
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
fav_activity = favorite.to_activity()
|
||||
broadcast(
|
||||
request.user, fav_activity, privacy='direct',
|
||||
direct_recipients=[status.user])
|
||||
if status.user.local:
|
||||
create_notification(
|
||||
status.user,
|
||||
'FAVORITE',
|
||||
related_user=request.user,
|
||||
related_status=status
|
||||
)
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class Unfavorite(View):
|
||||
''' take back a fav '''
|
||||
def post(self, request, status_id):
|
||||
''' unlike a status '''
|
||||
status = models.Status.objects.get(id=status_id)
|
||||
try:
|
||||
favorite = models.Favorite.objects.get(
|
||||
status=status,
|
||||
user=request.user
|
||||
)
|
||||
except models.Favorite.DoesNotExist:
|
||||
# can't find that status, idk
|
||||
return HttpResponseNotFound()
|
||||
|
||||
fav_activity = favorite.to_undo_activity(request.user)
|
||||
favorite.delete()
|
||||
broadcast(request.user, fav_activity, direct_recipients=[status.user])
|
||||
|
||||
# check for notification
|
||||
if status.user.local:
|
||||
notification = models.Notification.objects.filter(
|
||||
user=status.user, related_user=request.user,
|
||||
related_status=status, notification_type='FAVORITE'
|
||||
).first()
|
||||
if notification:
|
||||
notification.delete()
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class Boost(View):
|
||||
''' boost a status '''
|
||||
def post(self, request, status_id):
|
||||
''' boost a status '''
|
||||
status = models.Status.objects.get(id=status_id)
|
||||
# is it boostable?
|
||||
if not status.boostable:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
if models.Boost.objects.filter(
|
||||
boosted_status=status, user=request.user).exists():
|
||||
# you already boosted that.
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
boost = models.Boost.objects.create(
|
||||
boosted_status=status,
|
||||
privacy=status.privacy,
|
||||
user=request.user,
|
||||
)
|
||||
|
||||
boost_activity = boost.to_activity()
|
||||
broadcast(request.user, boost_activity)
|
||||
|
||||
if status.user.local:
|
||||
create_notification(
|
||||
status.user,
|
||||
'BOOST',
|
||||
related_user=request.user,
|
||||
related_status=status
|
||||
)
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class Unboost(View):
|
||||
''' boost a status '''
|
||||
def post(self, request, status_id):
|
||||
''' boost a status '''
|
||||
status = models.Status.objects.get(id=status_id)
|
||||
boost = models.Boost.objects.filter(
|
||||
boosted_status=status, user=request.user
|
||||
).first()
|
||||
activity = boost.to_undo_activity(request.user)
|
||||
|
||||
boost.delete()
|
||||
broadcast(request.user, activity)
|
||||
|
||||
# delete related notification
|
||||
if status.user.local:
|
||||
notification = models.Notification.objects.filter(
|
||||
user=status.user, related_user=request.user,
|
||||
related_status=status, notification_type='BOOST'
|
||||
).first()
|
||||
if notification:
|
||||
notification.delete()
|
||||
return redirect(request.headers.get('Referer', '/'))
|
58
bookwyrm/views/invite.py
Normal file
58
bookwyrm/views/invite.py
Normal file
@ -0,0 +1,58 @@
|
||||
''' invites when registration is closed '''
|
||||
from django.contrib.auth.decorators import login_required, permission_required
|
||||
from django.http import HttpResponseBadRequest
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import forms, models
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
@method_decorator(
|
||||
permission_required('bookwyrm.create_invites', raise_exception=True),
|
||||
name='dispatch')
|
||||
class ManageInvites(View):
|
||||
''' create invites '''
|
||||
def get(self, request):
|
||||
''' invite management page '''
|
||||
data = {
|
||||
'title': 'Invitations',
|
||||
'invites': models.SiteInvite.objects.filter(
|
||||
user=request.user).order_by('-created_date'),
|
||||
'form': forms.CreateInviteForm(),
|
||||
}
|
||||
return TemplateResponse(request, 'manage_invites.html', data)
|
||||
|
||||
def post(self, request):
|
||||
''' creates an invite database entry '''
|
||||
form = forms.CreateInviteForm(request.POST)
|
||||
if not form.is_valid():
|
||||
return HttpResponseBadRequest("ERRORS : %s" % (form.errors,))
|
||||
|
||||
invite = form.save(commit=False)
|
||||
invite.user = request.user
|
||||
invite.save()
|
||||
|
||||
return redirect('/invite')
|
||||
|
||||
|
||||
class Invite(View):
|
||||
''' use an invite to register '''
|
||||
def get(self, request, code):
|
||||
''' endpoint for using an invites '''
|
||||
if request.user.is_authenticated:
|
||||
return redirect('/')
|
||||
invite = get_object_or_404(models.SiteInvite, code=code)
|
||||
|
||||
data = {
|
||||
'title': 'Join',
|
||||
'register_form': forms.RegisterForm(),
|
||||
'invite': invite,
|
||||
'valid': invite.valid() if invite else True,
|
||||
}
|
||||
return TemplateResponse(request, 'invite.html', data)
|
||||
|
||||
# post handling is in views.authentication.Register
|
129
bookwyrm/views/landing.py
Normal file
129
bookwyrm/views/landing.py
Normal file
@ -0,0 +1,129 @@
|
||||
''' non-interactive pages '''
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.paginator import Paginator
|
||||
from django.db.models import Avg, Max
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils import timezone
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import forms, models
|
||||
from bookwyrm.settings import PAGE_LENGTH
|
||||
from .helpers import get_activity_feed
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class About(View):
|
||||
''' create invites '''
|
||||
def get(self, request):
|
||||
''' more information about the instance '''
|
||||
data = {
|
||||
'title': 'About',
|
||||
}
|
||||
return TemplateResponse(request, 'about.html', data)
|
||||
|
||||
class Home(View):
|
||||
''' discover page or home feed depending on auth '''
|
||||
def get(self, request):
|
||||
''' this is the same as the feed on the home tab '''
|
||||
if request.user.is_authenticated:
|
||||
feed_view = Feed.as_view()
|
||||
return feed_view(request, 'home')
|
||||
discover_view = Discover.as_view()
|
||||
return discover_view(request)
|
||||
|
||||
class Discover(View):
|
||||
''' preview of recently reviewed books '''
|
||||
def get(self, request):
|
||||
''' tiled book activity page '''
|
||||
books = models.Edition.objects.filter(
|
||||
review__published_date__isnull=False,
|
||||
review__user__local=True,
|
||||
review__privacy__in=['public', 'unlisted'],
|
||||
).exclude(
|
||||
cover__exact=''
|
||||
).annotate(
|
||||
Max('review__published_date')
|
||||
).order_by('-review__published_date__max')[:6]
|
||||
|
||||
ratings = {}
|
||||
for book in books:
|
||||
reviews = models.Review.objects.filter(
|
||||
book__in=book.parent_work.editions.all()
|
||||
)
|
||||
reviews = get_activity_feed(
|
||||
request.user, ['public', 'unlisted'], queryset=reviews)
|
||||
ratings[book.id] = reviews.aggregate(Avg('rating'))['rating__avg']
|
||||
data = {
|
||||
'title': 'Discover',
|
||||
'register_form': forms.RegisterForm(),
|
||||
'books': list(set(books)),
|
||||
'ratings': ratings
|
||||
}
|
||||
return TemplateResponse(request, 'discover.html', data)
|
||||
|
||||
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class Feed(View):
|
||||
''' activity stream '''
|
||||
def get(self, request, tab):
|
||||
''' user's homepage with activity feed '''
|
||||
try:
|
||||
page = int(request.GET.get('page', 1))
|
||||
except ValueError:
|
||||
page = 1
|
||||
|
||||
suggested_books = get_suggested_books(request.user)
|
||||
|
||||
if tab == 'home':
|
||||
activities = get_activity_feed(
|
||||
request.user, ['public', 'unlisted', 'followers'],
|
||||
following_only=True)
|
||||
elif tab == 'local':
|
||||
activities = get_activity_feed(
|
||||
request.user, ['public', 'followers'], local_only=True)
|
||||
else:
|
||||
activities = get_activity_feed(
|
||||
request.user, ['public', 'followers'])
|
||||
paginated = Paginator(activities, PAGE_LENGTH)
|
||||
|
||||
goal = models.AnnualGoal.objects.filter(
|
||||
user=request.user, year=timezone.now().year
|
||||
).first()
|
||||
data = {
|
||||
'title': 'Updates Feed',
|
||||
'user': request.user,
|
||||
'suggested_books': suggested_books,
|
||||
'activities': paginated.page(page),
|
||||
'tab': tab,
|
||||
'goal': goal,
|
||||
'goal_form': forms.GoalForm(),
|
||||
}
|
||||
return TemplateResponse(request, 'feed.html', data)
|
||||
|
||||
|
||||
def get_suggested_books(user, max_books=5):
|
||||
''' helper to get a user's recent books '''
|
||||
book_count = 0
|
||||
preset_shelves = [
|
||||
('reading', max_books), ('read', 2), ('to-read', max_books)
|
||||
]
|
||||
suggested_books = []
|
||||
for (preset, shelf_max) in preset_shelves:
|
||||
limit = shelf_max if shelf_max < (max_books - book_count) \
|
||||
else max_books - book_count
|
||||
shelf = user.shelf_set.get(identifier=preset)
|
||||
|
||||
shelf_books = shelf.shelfbook_set.order_by(
|
||||
'-updated_date'
|
||||
).all()[:limit]
|
||||
if not shelf_books:
|
||||
continue
|
||||
shelf_preview = {
|
||||
'name': shelf.name,
|
||||
'books': [s.book for s in shelf_books]
|
||||
}
|
||||
suggested_books.append(shelf_preview)
|
||||
book_count += len(shelf_preview['books'])
|
||||
return suggested_books
|
29
bookwyrm/views/notifications.py
Normal file
29
bookwyrm/views/notifications.py
Normal file
@ -0,0 +1,29 @@
|
||||
''' non-interactive pages '''
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.shortcuts import redirect
|
||||
from django.views import View
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class Notifications(View):
|
||||
''' notifications view '''
|
||||
def get(self, request):
|
||||
''' people are interacting with you, get hyped '''
|
||||
notifications = request.user.notification_set.all() \
|
||||
.order_by('-created_date')
|
||||
unread = [n.id for n in notifications.filter(read=False)]
|
||||
data = {
|
||||
'title': 'Notifications',
|
||||
'notifications': notifications,
|
||||
'unread': unread,
|
||||
}
|
||||
notifications.update(read=True)
|
||||
return TemplateResponse(request, 'notifications.html', data)
|
||||
|
||||
def post(self, request):
|
||||
''' permanently delete notification for user '''
|
||||
request.user.notification_set.filter(read=True).delete()
|
||||
return redirect('/notifications')
|
22
bookwyrm/views/outbox.py
Normal file
22
bookwyrm/views/outbox.py
Normal file
@ -0,0 +1,22 @@
|
||||
''' the good stuff! the books! '''
|
||||
from django.http import JsonResponse
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import activitypub, models
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
class Outbox(View):
|
||||
''' outbox '''
|
||||
def get(self, request, username):
|
||||
''' outbox for the requested user '''
|
||||
user = get_object_or_404(models.User, localname=username)
|
||||
filter_type = request.GET.get('type')
|
||||
if filter_type not in models.status_models:
|
||||
filter_type = None
|
||||
|
||||
return JsonResponse(
|
||||
user.to_outbox(**request.GET, filter_type=filter_type),
|
||||
encoder=activitypub.ActivityEncoder
|
||||
)
|
102
bookwyrm/views/password.py
Normal file
102
bookwyrm/views/password.py
Normal file
@ -0,0 +1,102 @@
|
||||
''' class views for password management '''
|
||||
from django.contrib.auth import login
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.shortcuts import redirect
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import models
|
||||
from bookwyrm.emailing import password_reset_email
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
class PasswordResetRequest(View):
|
||||
''' forgot password flow '''
|
||||
def get(self, request):
|
||||
''' password reset page '''
|
||||
return TemplateResponse(
|
||||
request,
|
||||
'password_reset_request.html',
|
||||
{'title': 'Reset Password'}
|
||||
)
|
||||
|
||||
def post(self, request):
|
||||
''' create a password reset token '''
|
||||
email = request.POST.get('email')
|
||||
try:
|
||||
user = models.User.objects.get(email=email)
|
||||
except models.User.DoesNotExist:
|
||||
return redirect('/password-reset')
|
||||
|
||||
# remove any existing password reset cods for this user
|
||||
models.PasswordReset.objects.filter(user=user).all().delete()
|
||||
|
||||
# create a new reset code
|
||||
code = models.PasswordReset.objects.create(user=user)
|
||||
password_reset_email(code)
|
||||
data = {'message': 'Password reset link sent to %s' % email}
|
||||
return TemplateResponse(request, 'password_reset_request.html', data)
|
||||
|
||||
|
||||
class PasswordReset(View):
|
||||
''' set new password '''
|
||||
def get(self, request, code):
|
||||
''' endpoint for sending invites '''
|
||||
if request.user.is_authenticated:
|
||||
return redirect('/')
|
||||
try:
|
||||
reset_code = models.PasswordReset.objects.get(code=code)
|
||||
if not reset_code.valid():
|
||||
raise PermissionDenied
|
||||
except models.PasswordReset.DoesNotExist:
|
||||
raise PermissionDenied
|
||||
|
||||
return TemplateResponse(
|
||||
request,
|
||||
'password_reset.html',
|
||||
{'title': 'Reset Password', 'code': reset_code.code}
|
||||
)
|
||||
|
||||
def post(self, request, code):
|
||||
''' allow a user to change their password through an emailed token '''
|
||||
try:
|
||||
reset_code = models.PasswordReset.objects.get(
|
||||
code=code
|
||||
)
|
||||
except models.PasswordReset.DoesNotExist:
|
||||
data = {'errors': ['Invalid password reset link']}
|
||||
return TemplateResponse(request, 'password_reset.html', data)
|
||||
|
||||
user = reset_code.user
|
||||
|
||||
new_password = request.POST.get('password')
|
||||
confirm_password = request.POST.get('confirm-password')
|
||||
|
||||
if new_password != confirm_password:
|
||||
data = {'errors': ['Passwords do not match']}
|
||||
return TemplateResponse(request, 'password_reset.html', data)
|
||||
|
||||
user.set_password(new_password)
|
||||
user.save()
|
||||
login(request, user)
|
||||
reset_code.delete()
|
||||
return redirect('/')
|
||||
|
||||
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class ChangePassword(View):
|
||||
''' change password as logged in user '''
|
||||
def post(self, request):
|
||||
''' allow a user to change their password '''
|
||||
new_password = request.POST.get('password')
|
||||
confirm_password = request.POST.get('confirm-password')
|
||||
|
||||
if new_password != confirm_password:
|
||||
return redirect('/edit-profile')
|
||||
|
||||
request.user.set_password(new_password)
|
||||
request.user.save()
|
||||
login(request, request.user)
|
||||
return redirect('/user/%s' % request.user.localname)
|
208
bookwyrm/views/reading.py
Normal file
208
bookwyrm/views/reading.py
Normal file
@ -0,0 +1,208 @@
|
||||
''' the good stuff! the books! '''
|
||||
import dateutil.parser
|
||||
from dateutil.parser import ParserError
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpResponseBadRequest, HttpResponseNotFound
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.utils import timezone
|
||||
from django.views.decorators.http import require_POST
|
||||
|
||||
from bookwyrm import models
|
||||
from bookwyrm.broadcast import broadcast
|
||||
from .helpers import get_edition, handle_reading_status
|
||||
from .shelf import handle_unshelve
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
@login_required
|
||||
@require_POST
|
||||
def start_reading(request, book_id):
|
||||
''' begin reading a book '''
|
||||
book = get_edition(book_id)
|
||||
shelf = models.Shelf.objects.filter(
|
||||
identifier='reading',
|
||||
user=request.user
|
||||
).first()
|
||||
|
||||
# create a readthrough
|
||||
readthrough = update_readthrough(request, book=book)
|
||||
if readthrough:
|
||||
readthrough.save()
|
||||
|
||||
# create a progress update if we have a page
|
||||
readthrough.create_update()
|
||||
|
||||
# shelve the book
|
||||
if request.POST.get('reshelve', True):
|
||||
try:
|
||||
current_shelf = models.Shelf.objects.get(
|
||||
user=request.user,
|
||||
edition=book
|
||||
)
|
||||
handle_unshelve(request.user, book, current_shelf)
|
||||
except models.Shelf.DoesNotExist:
|
||||
# this just means it isn't currently on the user's shelves
|
||||
pass
|
||||
shelfbook = models.ShelfBook.objects.create(
|
||||
book=book, shelf=shelf, added_by=request.user)
|
||||
broadcast(request.user, shelfbook.to_add_activity(request.user))
|
||||
|
||||
# post about it (if you want)
|
||||
if request.POST.get('post-status'):
|
||||
privacy = request.POST.get('privacy')
|
||||
handle_reading_status(request.user, shelf, book, privacy)
|
||||
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def finish_reading(request, book_id):
|
||||
''' a user completed a book, yay '''
|
||||
book = get_edition(book_id)
|
||||
shelf = models.Shelf.objects.filter(
|
||||
identifier='read',
|
||||
user=request.user
|
||||
).first()
|
||||
|
||||
# update or create a readthrough
|
||||
readthrough = update_readthrough(request, book=book)
|
||||
if readthrough:
|
||||
readthrough.save()
|
||||
|
||||
# shelve the book
|
||||
if request.POST.get('reshelve', True):
|
||||
try:
|
||||
current_shelf = models.Shelf.objects.get(
|
||||
user=request.user,
|
||||
edition=book
|
||||
)
|
||||
handle_unshelve(request.user, book, current_shelf)
|
||||
except models.Shelf.DoesNotExist:
|
||||
# this just means it isn't currently on the user's shelves
|
||||
pass
|
||||
shelfbook = models.ShelfBook.objects.create(
|
||||
book=book, shelf=shelf, added_by=request.user)
|
||||
broadcast(request.user, shelfbook.to_add_activity(request.user))
|
||||
|
||||
# post about it (if you want)
|
||||
if request.POST.get('post-status'):
|
||||
privacy = request.POST.get('privacy')
|
||||
handle_reading_status(request.user, shelf, book, privacy)
|
||||
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def edit_readthrough(request):
|
||||
''' can't use the form because the dates are too finnicky '''
|
||||
readthrough = update_readthrough(request, create=False)
|
||||
if not readthrough:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
# don't let people edit other people's data
|
||||
if request.user != readthrough.user:
|
||||
return HttpResponseBadRequest()
|
||||
readthrough.save()
|
||||
|
||||
# record the progress update individually
|
||||
# use default now for date field
|
||||
readthrough.create_update()
|
||||
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def delete_readthrough(request):
|
||||
''' remove a readthrough '''
|
||||
readthrough = get_object_or_404(
|
||||
models.ReadThrough, id=request.POST.get('id'))
|
||||
|
||||
# don't let people edit other people's data
|
||||
if request.user != readthrough.user:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
readthrough.delete()
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def create_readthrough(request):
|
||||
''' can't use the form because the dates are too finnicky '''
|
||||
book = get_object_or_404(models.Edition, id=request.POST.get('book'))
|
||||
readthrough = update_readthrough(request, create=True, book=book)
|
||||
if not readthrough:
|
||||
return redirect(book.local_path)
|
||||
readthrough.save()
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
def update_readthrough(request, book=None, create=True):
|
||||
''' updates but does not save dates on a readthrough '''
|
||||
try:
|
||||
read_id = request.POST.get('id')
|
||||
if not read_id:
|
||||
raise models.ReadThrough.DoesNotExist
|
||||
readthrough = models.ReadThrough.objects.get(id=read_id)
|
||||
except models.ReadThrough.DoesNotExist:
|
||||
if not create or not book:
|
||||
return None
|
||||
readthrough = models.ReadThrough(
|
||||
user=request.user,
|
||||
book=book,
|
||||
)
|
||||
|
||||
start_date = request.POST.get('start_date')
|
||||
if start_date:
|
||||
try:
|
||||
start_date = timezone.make_aware(dateutil.parser.parse(start_date))
|
||||
readthrough.start_date = start_date
|
||||
except ParserError:
|
||||
pass
|
||||
|
||||
finish_date = request.POST.get('finish_date')
|
||||
if finish_date:
|
||||
try:
|
||||
finish_date = timezone.make_aware(
|
||||
dateutil.parser.parse(finish_date))
|
||||
readthrough.finish_date = finish_date
|
||||
except ParserError:
|
||||
pass
|
||||
|
||||
progress = request.POST.get('progress')
|
||||
if progress:
|
||||
try:
|
||||
progress = int(progress)
|
||||
readthrough.progress = progress
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
progress_mode = request.POST.get('progress_mode')
|
||||
if progress_mode:
|
||||
try:
|
||||
progress_mode = models.ProgressMode(progress_mode)
|
||||
readthrough.progress_mode = progress_mode
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if not readthrough.start_date and not readthrough.finish_date:
|
||||
return None
|
||||
|
||||
return readthrough
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def delete_progressupdate(request):
|
||||
''' remove a progress update '''
|
||||
update = get_object_or_404(models.ProgressUpdate, id=request.POST.get('id'))
|
||||
|
||||
# don't let people edit other people's data
|
||||
if request.user != update.user:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
update.delete()
|
||||
return redirect(request.headers.get('Referer', '/'))
|
53
bookwyrm/views/search.py
Normal file
53
bookwyrm/views/search.py
Normal file
@ -0,0 +1,53 @@
|
||||
''' search views'''
|
||||
import re
|
||||
|
||||
from django.contrib.postgres.search import TrigramSimilarity
|
||||
from django.db.models.functions import Greatest
|
||||
from django.http import JsonResponse
|
||||
from django.template.response import TemplateResponse
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import models
|
||||
from bookwyrm.connectors import connector_manager
|
||||
from bookwyrm.utils import regex
|
||||
from .helpers import is_api_request
|
||||
from .helpers import handle_remote_webfinger
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
class Search(View):
|
||||
''' search users or books '''
|
||||
def get(self, request):
|
||||
''' that search bar up top '''
|
||||
query = request.GET.get('q')
|
||||
min_confidence = request.GET.get('min_confidence', 0.1)
|
||||
|
||||
if is_api_request(request):
|
||||
# only return local book results via json so we don't cascade
|
||||
book_results = connector_manager.local_search(
|
||||
query, min_confidence=min_confidence)
|
||||
return JsonResponse([r.json() for r in book_results], safe=False)
|
||||
|
||||
# use webfinger for mastodon style account@domain.com username
|
||||
if re.match(r'\B%s' % regex.full_username, query):
|
||||
handle_remote_webfinger(query)
|
||||
|
||||
# do a local user search
|
||||
user_results = models.User.objects.annotate(
|
||||
similarity=Greatest(
|
||||
TrigramSimilarity('username', query),
|
||||
TrigramSimilarity('localname', query),
|
||||
)
|
||||
).filter(
|
||||
similarity__gt=0.5,
|
||||
).order_by('-similarity')[:10]
|
||||
|
||||
book_results = connector_manager.search(
|
||||
query, min_confidence=min_confidence)
|
||||
data = {
|
||||
'title': 'Search Results',
|
||||
'book_results': book_results,
|
||||
'user_results': user_results,
|
||||
'query': query,
|
||||
}
|
||||
return TemplateResponse(request, 'search_results.html', data)
|
170
bookwyrm/views/shelf.py
Normal file
170
bookwyrm/views/shelf.py
Normal file
@ -0,0 +1,170 @@
|
||||
''' shelf views'''
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpResponseBadRequest, HttpResponseNotFound
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
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.broadcast import broadcast
|
||||
from .helpers import is_api_request, get_edition, get_user_from_username
|
||||
from .helpers import handle_reading_status
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
class Shelf(View):
|
||||
''' shelf page '''
|
||||
def get(self, request, username, shelf_identifier):
|
||||
''' display a shelf '''
|
||||
try:
|
||||
user = get_user_from_username(username)
|
||||
except models.User.DoesNotExist:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
if shelf_identifier:
|
||||
shelf = user.shelf_set.get(identifier=shelf_identifier)
|
||||
else:
|
||||
shelf = user.shelf_set.first()
|
||||
|
||||
is_self = request.user == user
|
||||
|
||||
shelves = user.shelf_set
|
||||
if not is_self:
|
||||
follower = user.followers.filter(id=request.user.id).exists()
|
||||
# make sure the user has permission to view the shelf
|
||||
if shelf.privacy == 'direct' or \
|
||||
(shelf.privacy == 'followers' and not follower):
|
||||
return HttpResponseNotFound()
|
||||
|
||||
# only show other shelves that should be visible
|
||||
if follower:
|
||||
shelves = shelves.filter(privacy__in=['public', 'followers'])
|
||||
else:
|
||||
shelves = shelves.filter(privacy='public')
|
||||
|
||||
|
||||
if is_api_request(request):
|
||||
return ActivitypubResponse(shelf.to_activity(**request.GET))
|
||||
|
||||
books = models.ShelfBook.objects.filter(
|
||||
added_by=user, shelf=shelf
|
||||
).order_by('-updated_date').all()
|
||||
|
||||
data = {
|
||||
'title': '%s\'s %s shelf' % (user.display_name, shelf.name),
|
||||
'user': user,
|
||||
'is_self': is_self,
|
||||
'shelves': shelves.all(),
|
||||
'shelf': shelf,
|
||||
'books': [b.book for b in books],
|
||||
}
|
||||
|
||||
return TemplateResponse(request, 'shelf.html', data)
|
||||
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
def post(self, request, username, shelf_id):
|
||||
''' user generated shelves '''
|
||||
if not request.user.username == username:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
shelf = get_object_or_404(models.Shelf, id=shelf_id)
|
||||
if request.user != shelf.user:
|
||||
return HttpResponseBadRequest()
|
||||
if not shelf.editable and request.POST.get('name') != shelf.name:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
form = forms.ShelfForm(request.POST, instance=shelf)
|
||||
if not form.is_valid():
|
||||
return redirect(shelf.local_path)
|
||||
shelf = form.save()
|
||||
return redirect(shelf.local_path)
|
||||
|
||||
|
||||
def user_shelves_page(request, username):
|
||||
''' default shelf '''
|
||||
return Shelf.as_view()(request, username, None)
|
||||
|
||||
|
||||
@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('/user/%s/shelf/%s' % \
|
||||
(request.user.localname, shelf.identifier))
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def delete_shelf(request, shelf_id):
|
||||
''' user generated shelves '''
|
||||
shelf = get_object_or_404(models.Shelf, id=shelf_id)
|
||||
if request.user != shelf.user or not shelf.editable:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
shelf.delete()
|
||||
return redirect('/user/%s/shelves' % request.user.localname)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def shelve(request):
|
||||
''' put a on a user's shelf '''
|
||||
book = get_edition(request.POST.get('book'))
|
||||
|
||||
desired_shelf = models.Shelf.objects.filter(
|
||||
identifier=request.POST.get('shelf'),
|
||||
user=request.user
|
||||
).first()
|
||||
|
||||
if request.POST.get('reshelve', True):
|
||||
try:
|
||||
current_shelf = models.Shelf.objects.get(
|
||||
user=request.user,
|
||||
edition=book
|
||||
)
|
||||
handle_unshelve(request.user, book, current_shelf)
|
||||
except models.Shelf.DoesNotExist:
|
||||
# this just means it isn't currently on the user's shelves
|
||||
pass
|
||||
shelfbook = models.ShelfBook.objects.create(
|
||||
book=book, shelf=desired_shelf, added_by=request.user)
|
||||
broadcast(request.user, shelfbook.to_add_activity(request.user))
|
||||
|
||||
# post about "want to read" shelves
|
||||
if desired_shelf.identifier == 'to-read':
|
||||
handle_reading_status(
|
||||
request.user,
|
||||
desired_shelf,
|
||||
book,
|
||||
privacy=desired_shelf.privacy
|
||||
)
|
||||
|
||||
return redirect('/')
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def unshelve(request):
|
||||
''' put a on a user's shelf '''
|
||||
book = models.Edition.objects.get(id=request.POST['book'])
|
||||
current_shelf = models.Shelf.objects.get(id=request.POST['shelf'])
|
||||
|
||||
handle_unshelve(request.user, book, current_shelf)
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
def handle_unshelve(user, book, shelf):
|
||||
''' unshelve a book '''
|
||||
row = models.ShelfBook.objects.get(book=book, shelf=shelf)
|
||||
activity = row.to_remove_activity(user)
|
||||
row.delete()
|
||||
|
||||
broadcast(user, activity)
|
195
bookwyrm/views/status.py
Normal file
195
bookwyrm/views/status.py
Normal file
@ -0,0 +1,195 @@
|
||||
''' what are we here for if not for posting '''
|
||||
import re
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpResponseBadRequest, HttpResponseNotFound
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
from markdown import markdown
|
||||
|
||||
from bookwyrm import forms, models
|
||||
from bookwyrm.activitypub import ActivitypubResponse
|
||||
from bookwyrm.broadcast import broadcast
|
||||
from bookwyrm.sanitize_html import InputHtmlParser
|
||||
from bookwyrm.settings import DOMAIN
|
||||
from bookwyrm.status import create_notification, delete_status
|
||||
from bookwyrm.utils import regex
|
||||
from .helpers import get_user_from_username, handle_remote_webfinger
|
||||
from .helpers import is_api_request, is_bookworm_request, object_visible_to_user
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
class Status(View):
|
||||
''' the view for *posting* '''
|
||||
def get(self, request, username, status_id):
|
||||
''' display a particular status (and replies, etc) '''
|
||||
try:
|
||||
user = get_user_from_username(username)
|
||||
status = models.Status.objects.select_subclasses().get(id=status_id)
|
||||
except ValueError:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
# the url should have the poster's username in it
|
||||
if user != status.user:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
# make sure the user is authorized to see the status
|
||||
if not object_visible_to_user(request.user, status):
|
||||
return HttpResponseNotFound()
|
||||
|
||||
if is_api_request(request):
|
||||
return ActivitypubResponse(
|
||||
status.to_activity(pure=not is_bookworm_request(request)))
|
||||
|
||||
data = {
|
||||
'title': 'Status by %s' % user.username,
|
||||
'status': status,
|
||||
}
|
||||
return TemplateResponse(request, 'status.html', data)
|
||||
|
||||
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class CreateStatus(View):
|
||||
''' get posting '''
|
||||
def post(self, request, status_type):
|
||||
''' create status of whatever type '''
|
||||
status_type = status_type[0].upper() + status_type[1:]
|
||||
try:
|
||||
form = getattr(forms, '%sForm' % status_type)(request.POST)
|
||||
except AttributeError:
|
||||
return HttpResponseBadRequest()
|
||||
if not form.is_valid():
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
status = form.save(commit=False)
|
||||
if not status.sensitive and status.content_warning:
|
||||
# the cw text field remains populated when you click "remove"
|
||||
status.content_warning = None
|
||||
status.save()
|
||||
|
||||
# inspect the text for user tags
|
||||
content = status.content
|
||||
for (mention_text, mention_user) in find_mentions(content):
|
||||
# add them to status mentions fk
|
||||
status.mention_users.add(mention_user)
|
||||
|
||||
# turn the mention into a link
|
||||
content = re.sub(
|
||||
r'%s([^@]|$)' % mention_text,
|
||||
r'<a href="%s">%s</a>\g<1>' % \
|
||||
(mention_user.remote_id, mention_text),
|
||||
content)
|
||||
|
||||
# add reply parent to mentions and notify
|
||||
if status.reply_parent:
|
||||
status.mention_users.add(status.reply_parent.user)
|
||||
for mention_user in status.reply_parent.mention_users.all():
|
||||
status.mention_users.add(mention_user)
|
||||
|
||||
if status.reply_parent.user.local:
|
||||
create_notification(
|
||||
status.reply_parent.user,
|
||||
'REPLY',
|
||||
related_user=request.user,
|
||||
related_status=status
|
||||
)
|
||||
|
||||
# deduplicate mentions
|
||||
status.mention_users.set(set(status.mention_users.all()))
|
||||
# create mention notifications
|
||||
for mention_user in status.mention_users.all():
|
||||
if status.reply_parent and mention_user == status.reply_parent.user:
|
||||
continue
|
||||
if mention_user.local:
|
||||
create_notification(
|
||||
mention_user,
|
||||
'MENTION',
|
||||
related_user=request.user,
|
||||
related_status=status
|
||||
)
|
||||
|
||||
# don't apply formatting to generated notes
|
||||
if not isinstance(status, models.GeneratedNote):
|
||||
status.content = to_markdown(content)
|
||||
# do apply formatting to quotes
|
||||
if hasattr(status, 'quote'):
|
||||
status.quote = to_markdown(status.quote)
|
||||
|
||||
status.save()
|
||||
|
||||
broadcast(
|
||||
request.user,
|
||||
status.to_create_activity(request.user),
|
||||
software='bookwyrm')
|
||||
|
||||
# re-format the activity for non-bookwyrm servers
|
||||
remote_activity = status.to_create_activity(request.user, pure=True)
|
||||
broadcast(request.user, remote_activity, software='other')
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
class DeleteStatus(View):
|
||||
''' tombstone that bad boy '''
|
||||
def post(self, request, status_id):
|
||||
''' delete and tombstone a status '''
|
||||
status = get_object_or_404(models.Status, id=status_id)
|
||||
|
||||
# don't let people delete other people's statuses
|
||||
if status.user != request.user:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
# perform deletion
|
||||
delete_status(status)
|
||||
broadcast(request.user, status.to_delete_activity(request.user))
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
class Replies(View):
|
||||
''' replies page (a json view of status) '''
|
||||
def get(self, request, username, status_id):
|
||||
''' ordered collection of replies to a status '''
|
||||
# the html view is the same as Status
|
||||
if not is_api_request(request):
|
||||
status_view = Status.as_view()
|
||||
return status_view(request, username, status_id)
|
||||
|
||||
# the json view is different than Status
|
||||
status = models.Status.objects.get(id=status_id)
|
||||
if status.user.localname != username:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
return ActivitypubResponse(status.to_replies(**request.GET))
|
||||
|
||||
def find_mentions(content):
|
||||
''' detect @mentions in raw status content '''
|
||||
for match in re.finditer(regex.strict_username, content):
|
||||
username = match.group().strip().split('@')[1:]
|
||||
if len(username) == 1:
|
||||
# this looks like a local user (@user), fill in the domain
|
||||
username.append(DOMAIN)
|
||||
username = '@'.join(username)
|
||||
|
||||
mention_user = handle_remote_webfinger(username)
|
||||
if not mention_user:
|
||||
# we can ignore users we don't know about
|
||||
continue
|
||||
yield (match.group(), mention_user)
|
||||
|
||||
|
||||
def format_links(content):
|
||||
''' detect and format links '''
|
||||
return re.sub(
|
||||
r'([^(href=")]|^|\()(https?:\/\/(%s([\w\.\-_\/+&\?=:;,])*))' % \
|
||||
regex.domain,
|
||||
r'\g<1><a href="\g<2>">\g<3></a>',
|
||||
content)
|
||||
|
||||
def to_markdown(content):
|
||||
''' catch links and convert to markdown '''
|
||||
content = format_links(content)
|
||||
content = markdown(content)
|
||||
# sanitize resulting html
|
||||
sanitizer = InputHtmlParser()
|
||||
sanitizer.feed(content)
|
||||
return sanitizer.get_output()
|
78
bookwyrm/views/tag.py
Normal file
78
bookwyrm/views/tag.py
Normal file
@ -0,0 +1,78 @@
|
||||
''' tagging views'''
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpResponseNotFound
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import models
|
||||
from bookwyrm.activitypub import ActivitypubResponse
|
||||
from bookwyrm.broadcast import broadcast
|
||||
from .helpers import is_api_request
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
class Tag(View):
|
||||
''' tag page '''
|
||||
def get(self, request, tag_id):
|
||||
''' see books related to a tag '''
|
||||
tag_obj = models.Tag.objects.filter(identifier=tag_id).first()
|
||||
if not tag_obj:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
if is_api_request(request):
|
||||
return ActivitypubResponse(tag_obj.to_activity(**request.GET))
|
||||
|
||||
books = models.Edition.objects.filter(
|
||||
usertag__tag__identifier=tag_id
|
||||
).distinct()
|
||||
data = {
|
||||
'title': tag_obj.name,
|
||||
'books': books,
|
||||
'tag': tag_obj,
|
||||
}
|
||||
return TemplateResponse(request, 'tag.html', data)
|
||||
|
||||
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class AddTag(View):
|
||||
''' add a tag to a book '''
|
||||
def post(self, request):
|
||||
''' tag a book '''
|
||||
# I'm not using a form here because sometimes "name" is sent as a hidden
|
||||
# field which doesn't validate
|
||||
name = request.POST.get('name')
|
||||
book_id = request.POST.get('book')
|
||||
book = get_object_or_404(models.Edition, id=book_id)
|
||||
tag_obj, created = models.Tag.objects.get_or_create(
|
||||
name=name,
|
||||
)
|
||||
user_tag, _ = models.UserTag.objects.get_or_create(
|
||||
user=request.user,
|
||||
book=book,
|
||||
tag=tag_obj,
|
||||
)
|
||||
|
||||
if created:
|
||||
broadcast(request.user, user_tag.to_add_activity(request.user))
|
||||
return redirect('/book/%s' % book_id)
|
||||
|
||||
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class RemoveTag(View):
|
||||
''' remove a user's tag from a book '''
|
||||
def post(self, request):
|
||||
''' untag a book '''
|
||||
name = request.POST.get('name')
|
||||
tag_obj = get_object_or_404(models.Tag, name=name)
|
||||
book_id = request.POST.get('book')
|
||||
book = get_object_or_404(models.Edition, id=book_id)
|
||||
|
||||
user_tag = get_object_or_404(
|
||||
models.UserTag, tag=tag_obj, book=book, user=request.user)
|
||||
tag_activity = user_tag.to_remove_activity(request.user)
|
||||
user_tag.delete()
|
||||
|
||||
broadcast(request.user, tag_activity)
|
||||
return redirect('/book/%s' % book_id)
|
188
bookwyrm/views/user.py
Normal file
188
bookwyrm/views/user.py
Normal file
@ -0,0 +1,188 @@
|
||||
''' non-interactive pages '''
|
||||
from io import BytesIO
|
||||
from uuid import uuid4
|
||||
from PIL import Image
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.files.base import ContentFile
|
||||
from django.core.paginator import Paginator
|
||||
from django.http import HttpResponseNotFound
|
||||
from django.shortcuts import redirect
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils import timezone
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import forms, models
|
||||
from bookwyrm.activitypub import ActivitypubResponse
|
||||
from bookwyrm.broadcast import broadcast
|
||||
from bookwyrm.settings import PAGE_LENGTH
|
||||
from .helpers import get_activity_feed, get_user_from_username, is_api_request
|
||||
from .helpers import object_visible_to_user
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
class User(View):
|
||||
''' user profile page '''
|
||||
def get(self, request, username):
|
||||
''' profile page for a user '''
|
||||
try:
|
||||
user = get_user_from_username(username)
|
||||
except models.User.DoesNotExist:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
if is_api_request(request):
|
||||
# we have a json request
|
||||
return ActivitypubResponse(user.to_activity())
|
||||
# otherwise we're at a UI view
|
||||
|
||||
try:
|
||||
page = int(request.GET.get('page', 1))
|
||||
except ValueError:
|
||||
page = 1
|
||||
|
||||
shelf_preview = []
|
||||
|
||||
# only show other shelves that should be visible
|
||||
shelves = user.shelf_set
|
||||
is_self = request.user.id == user.id
|
||||
if not is_self:
|
||||
follower = user.followers.filter(id=request.user.id).exists()
|
||||
if follower:
|
||||
shelves = shelves.filter(privacy__in=['public', 'followers'])
|
||||
else:
|
||||
shelves = shelves.filter(privacy='public')
|
||||
|
||||
for user_shelf in shelves.all():
|
||||
if not user_shelf.books.count():
|
||||
continue
|
||||
shelf_preview.append({
|
||||
'name': user_shelf.name,
|
||||
'local_path': user_shelf.local_path,
|
||||
'books': user_shelf.books.all()[:3],
|
||||
'size': user_shelf.books.count(),
|
||||
})
|
||||
if len(shelf_preview) > 2:
|
||||
break
|
||||
|
||||
# user's posts
|
||||
activities = get_activity_feed(
|
||||
request.user,
|
||||
['public', 'unlisted', 'followers'],
|
||||
queryset=models.Status.objects.filter(user=user)
|
||||
)
|
||||
paginated = Paginator(activities, PAGE_LENGTH)
|
||||
goal = models.AnnualGoal.objects.filter(
|
||||
user=user, year=timezone.now().year).first()
|
||||
if not object_visible_to_user(request.user, goal):
|
||||
goal = None
|
||||
data = {
|
||||
'title': user.name,
|
||||
'user': user,
|
||||
'is_self': is_self,
|
||||
'shelves': shelf_preview,
|
||||
'shelf_count': shelves.count(),
|
||||
'activities': paginated.page(page),
|
||||
'goal': goal,
|
||||
}
|
||||
|
||||
return TemplateResponse(request, 'user.html', data)
|
||||
|
||||
class Followers(View):
|
||||
''' list of followers view '''
|
||||
def get(self, request, username):
|
||||
''' list of followers '''
|
||||
try:
|
||||
user = get_user_from_username(username)
|
||||
except models.User.DoesNotExist:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
if is_api_request(request):
|
||||
return ActivitypubResponse(
|
||||
user.to_followers_activity(**request.GET))
|
||||
|
||||
data = {
|
||||
'title': '%s: followers' % user.name,
|
||||
'user': user,
|
||||
'is_self': request.user.id == user.id,
|
||||
'followers': user.followers.all(),
|
||||
}
|
||||
return TemplateResponse(request, 'followers.html', data)
|
||||
|
||||
class Following(View):
|
||||
''' list of following view '''
|
||||
def get(self, request, username):
|
||||
''' list of followers '''
|
||||
try:
|
||||
user = get_user_from_username(username)
|
||||
except models.User.DoesNotExist:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
if is_api_request(request):
|
||||
return ActivitypubResponse(
|
||||
user.to_following_activity(**request.GET))
|
||||
|
||||
data = {
|
||||
'title': '%s: following' % user.name,
|
||||
'user': user,
|
||||
'is_self': request.user.id == user.id,
|
||||
'following': user.following.all(),
|
||||
}
|
||||
return TemplateResponse(request, 'following.html', data)
|
||||
|
||||
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class EditUser(View):
|
||||
''' edit user view '''
|
||||
def get(self, request):
|
||||
''' profile page for a user '''
|
||||
user = request.user
|
||||
|
||||
form = forms.EditUserForm(instance=request.user)
|
||||
data = {
|
||||
'title': 'Edit profile',
|
||||
'form': form,
|
||||
'user': user,
|
||||
}
|
||||
return TemplateResponse(request, 'edit_user.html', data)
|
||||
|
||||
def post(self, request):
|
||||
''' les get fancy with images '''
|
||||
form = forms.EditUserForm(
|
||||
request.POST, request.FILES, instance=request.user)
|
||||
if not form.is_valid():
|
||||
data = {'form': form, 'user': request.user}
|
||||
return TemplateResponse(request, 'edit_user.html', data)
|
||||
|
||||
user = form.save(commit=False)
|
||||
|
||||
if 'avatar' in form.files:
|
||||
# crop and resize avatar upload
|
||||
image = Image.open(form.files['avatar'])
|
||||
target_size = 120
|
||||
width, height = image.size
|
||||
thumbnail_scale = height / (width / target_size) if height > width \
|
||||
else width / (height / target_size)
|
||||
image.thumbnail([thumbnail_scale, thumbnail_scale])
|
||||
width, height = image.size
|
||||
|
||||
width_diff = width - target_size
|
||||
height_diff = height - target_size
|
||||
cropped = image.crop((
|
||||
int(width_diff / 2),
|
||||
int(height_diff / 2),
|
||||
int(width - (width_diff / 2)),
|
||||
int(height - (height_diff / 2))
|
||||
))
|
||||
output = BytesIO()
|
||||
cropped.save(output, format=image.format)
|
||||
ContentFile(output.getvalue())
|
||||
|
||||
# set the name to a hash
|
||||
extension = form.files['avatar'].name.split('.')[-1]
|
||||
filename = '%s.%s' % (uuid4(), extension)
|
||||
user.avatar.save(filename, ContentFile(output.getvalue()))
|
||||
user.save()
|
||||
|
||||
broadcast(user, user.to_update_activity(user))
|
||||
return redirect('/user/%s' % request.user.localname)
|
Reference in New Issue
Block a user