2020-02-22 22:02:03 +00:00
|
|
|
''' views for pages you can go to in the application '''
|
2020-05-04 04:13:43 +00:00
|
|
|
import re
|
|
|
|
|
2020-01-28 19:45:27 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2020-02-07 23:29:11 +00:00
|
|
|
from django.db.models import Avg, Q
|
2020-04-21 14:09:21 +00:00
|
|
|
from django.http import HttpResponseBadRequest, HttpResponseNotFound,\
|
2020-03-14 00:57:36 +00:00
|
|
|
JsonResponse
|
2020-04-21 14:09:21 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2020-01-25 23:25:19 +00:00
|
|
|
from django.template.response import TemplateResponse
|
2020-03-14 00:57:36 +00:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2020-01-25 06:32:41 +00:00
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import outgoing
|
|
|
|
from bookwyrm.activitypub import ActivityEncoder
|
|
|
|
from bookwyrm import forms, models, books_manager
|
|
|
|
from bookwyrm import goodreads_import
|
|
|
|
from bookwyrm.tasks import app
|
2020-01-28 19:45:27 +00:00
|
|
|
|
|
|
|
|
2020-02-22 22:02:03 +00:00
|
|
|
def get_user_from_username(username):
|
|
|
|
''' helper function to resolve a localname or a username to a user '''
|
|
|
|
try:
|
|
|
|
user = models.User.objects.get(localname=username)
|
|
|
|
except models.User.DoesNotExist:
|
|
|
|
user = models.User.objects.get(username=username)
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
2020-03-14 00:57:36 +00:00
|
|
|
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'
|
|
|
|
|
|
|
|
|
2020-03-29 23:05:33 +00:00
|
|
|
def server_error_page(request):
|
|
|
|
''' 500 errors '''
|
|
|
|
return TemplateResponse(request, 'error.html')
|
|
|
|
|
|
|
|
|
|
|
|
def not_found_page(request, _):
|
|
|
|
''' 404s '''
|
|
|
|
return TemplateResponse(request, 'notfound.html')
|
|
|
|
|
|
|
|
|
2020-01-25 06:32:41 +00:00
|
|
|
@login_required
|
2020-01-25 23:25:19 +00:00
|
|
|
def home(request):
|
2020-02-20 02:02:11 +00:00
|
|
|
''' this is the same as the feed on the home tab '''
|
|
|
|
return home_tab(request, 'home')
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def home_tab(request, tab):
|
2020-01-28 19:45:27 +00:00
|
|
|
''' user's homepage with activity feed '''
|
2020-04-01 22:36:35 +00:00
|
|
|
page_size = 15
|
|
|
|
try:
|
|
|
|
page = int(request.GET.get('page', 1))
|
|
|
|
except ValueError:
|
|
|
|
page = 1
|
|
|
|
|
2020-02-21 23:39:25 +00:00
|
|
|
shelves = []
|
2020-03-16 23:10:59 +00:00
|
|
|
shelves = get_user_shelf_preview(
|
|
|
|
request.user,
|
|
|
|
[('reading', 3), ('read', 1), ('to-read', 3)]
|
|
|
|
)
|
|
|
|
size = sum(len(s['books']) for s in shelves)
|
2020-03-15 21:15:36 +00:00
|
|
|
# books new to the instance, for discovery
|
2020-03-16 23:10:59 +00:00
|
|
|
if size < 6:
|
2020-03-30 22:03:21 +00:00
|
|
|
recent_books = models.Work.objects.order_by(
|
|
|
|
'-created_date'
|
|
|
|
)[:6 - size]
|
|
|
|
recent_books = [b.default_edition for b in recent_books]
|
2020-03-15 21:15:36 +00:00
|
|
|
shelves.append({
|
|
|
|
'name': 'Recently added',
|
|
|
|
'identifier': None,
|
2020-03-30 22:03:21 +00:00
|
|
|
'books': recent_books,
|
2020-03-16 23:10:59 +00:00
|
|
|
'count': 6 - size,
|
2020-03-15 21:15:36 +00:00
|
|
|
})
|
2020-02-07 23:29:11 +00:00
|
|
|
|
2020-03-30 22:03:21 +00:00
|
|
|
|
2020-02-07 23:29:11 +00:00
|
|
|
# allows us to check if a user has shelved a book
|
2020-03-30 21:12:18 +00:00
|
|
|
user_books = models.Edition.objects.filter(shelves__user=request.user).all()
|
2020-02-07 23:29:11 +00:00
|
|
|
|
2020-04-01 23:02:39 +00:00
|
|
|
activities = get_activity_feed(request.user, tab)
|
2020-02-20 02:02:11 +00:00
|
|
|
|
2020-04-01 22:36:35 +00:00
|
|
|
activity_count = activities.count()
|
|
|
|
activities = activities[(page - 1) * page_size:page * page_size]
|
2020-01-28 02:47:54 +00:00
|
|
|
|
2020-04-01 22:36:35 +00:00
|
|
|
next_page = '/?page=%d' % (page + 1)
|
|
|
|
prev_page = '/?page=%d' % (page - 1)
|
2020-01-25 23:25:19 +00:00
|
|
|
data = {
|
|
|
|
'user': request.user,
|
2020-02-21 23:39:25 +00:00
|
|
|
'shelves': shelves,
|
2020-01-29 01:23:38 +00:00
|
|
|
'user_books': user_books,
|
2020-01-28 02:47:54 +00:00
|
|
|
'activities': activities,
|
2020-03-15 21:15:36 +00:00
|
|
|
'feed_tabs': [
|
|
|
|
{'id': 'home', 'display': 'Home'},
|
|
|
|
{'id': 'local', 'display': 'Local'},
|
|
|
|
{'id': 'federated', 'display': 'Federated'}
|
|
|
|
],
|
2020-02-20 02:02:11 +00:00
|
|
|
'active_tab': tab,
|
2020-03-15 21:15:36 +00:00
|
|
|
'review_form': forms.ReviewForm(),
|
2020-04-08 16:40:47 +00:00
|
|
|
'quotation_form': forms.QuotationForm(),
|
2020-03-21 23:50:49 +00:00
|
|
|
'comment_form': forms.CommentForm(),
|
2020-04-01 22:36:35 +00:00
|
|
|
'next': next_page if activity_count > (page_size * page) else None,
|
|
|
|
'prev': prev_page if page > 1 else None,
|
2020-01-25 23:25:19 +00:00
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'feed.html', data)
|
2020-01-25 06:32:41 +00:00
|
|
|
|
2020-01-28 02:47:54 +00:00
|
|
|
|
2020-04-01 23:02:39 +00:00
|
|
|
def get_activity_feed(user, filter_level, model=models.Status):
|
|
|
|
''' get a filtered queryset of statuses '''
|
|
|
|
# status updates for your follow network
|
|
|
|
following = models.User.objects.filter(
|
|
|
|
Q(followers=user) | Q(id=user.id)
|
|
|
|
)
|
|
|
|
|
|
|
|
activities = model
|
|
|
|
if hasattr(model, 'objects'):
|
|
|
|
activities = model.objects
|
|
|
|
|
|
|
|
activities = activities.order_by(
|
|
|
|
'-created_date'
|
|
|
|
)
|
|
|
|
if hasattr(activities, 'select_subclasses'):
|
|
|
|
activities = activities.select_subclasses()
|
|
|
|
|
|
|
|
# TODO: privacy relationshup between request.user and user
|
|
|
|
if filter_level in ['friends', 'home']:
|
|
|
|
# people you follow and direct mentions
|
|
|
|
activities = activities.filter(
|
|
|
|
Q(user__in=following, privacy='public') | \
|
|
|
|
Q(mention_users=user)
|
|
|
|
)
|
|
|
|
elif filter_level == 'self':
|
|
|
|
activities = activities.filter(user=user, privacy='public')
|
|
|
|
elif filter_level == 'local':
|
|
|
|
# everyone on this instance
|
|
|
|
activities = activities.filter(user__local=True, privacy='public')
|
|
|
|
else:
|
|
|
|
# all activities from everyone you federate with
|
|
|
|
activities = activities.filter(privacy='public')
|
|
|
|
|
|
|
|
return activities
|
|
|
|
|
|
|
|
|
2020-05-04 04:13:43 +00:00
|
|
|
def search(request):
|
|
|
|
''' that search bar up top '''
|
|
|
|
query = request.GET.get('q')
|
|
|
|
if re.match(r'\w+@\w+.\w+', query):
|
|
|
|
# if something looks like a username, search with webfinger
|
|
|
|
results = [outgoing.handle_account_search(query)]
|
2020-05-04 17:15:41 +00:00
|
|
|
return TemplateResponse(
|
|
|
|
request, 'user_results.html', {'results': results}
|
|
|
|
)
|
|
|
|
|
|
|
|
# or just send the question over to book search
|
|
|
|
|
|
|
|
if is_api_request(request):
|
|
|
|
# only return local results via json so we don't cause a cascade
|
|
|
|
results = books_manager.local_search(query)
|
|
|
|
return JsonResponse([r.__dict__ for r in results], safe=False)
|
2020-05-04 04:13:43 +00:00
|
|
|
|
2020-05-04 17:15:41 +00:00
|
|
|
results = books_manager.search(query)
|
|
|
|
return TemplateResponse(request, 'book_results.html', {'results': results})
|
2020-05-04 04:13:43 +00:00
|
|
|
|
|
|
|
|
2020-03-17 00:45:34 +00:00
|
|
|
def books_page(request):
|
|
|
|
''' discover books '''
|
2020-03-30 22:03:21 +00:00
|
|
|
recent_books = models.Work.objects
|
|
|
|
recent_books = recent_books.order_by('-created_date')[:50]
|
|
|
|
recent_books = [b.default_edition for b in recent_books]
|
2020-03-27 15:10:04 +00:00
|
|
|
if request.user.is_authenticated:
|
2020-03-30 22:03:21 +00:00
|
|
|
recent_books = models.Edition.objects.filter(
|
|
|
|
~Q(shelfbook__shelf__user=request.user),
|
2020-04-01 23:21:11 +00:00
|
|
|
id__in=[b.id for b in recent_books if b],
|
2020-03-27 22:42:44 +00:00
|
|
|
)
|
2020-03-27 15:10:04 +00:00
|
|
|
|
2020-03-17 00:45:34 +00:00
|
|
|
data = {
|
|
|
|
'books': recent_books,
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'books.html', data)
|
|
|
|
|
2020-03-30 22:03:21 +00:00
|
|
|
|
2020-03-23 16:40:09 +00:00
|
|
|
@login_required
|
|
|
|
def import_page(request):
|
|
|
|
''' import history from goodreads '''
|
|
|
|
return TemplateResponse(request, 'import.html', {
|
|
|
|
'import_form': forms.ImportForm(),
|
2020-04-21 14:09:21 +00:00
|
|
|
'jobs': models.ImportJob.
|
|
|
|
objects.filter(user=request.user).order_by('-created_date'),
|
2020-04-28 13:59:48 +00:00
|
|
|
'limit': goodreads_import.MAX_ENTRIES,
|
2020-03-23 16:40:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2020-04-21 14:09:21 +00:00
|
|
|
@login_required
|
|
|
|
def import_status(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)
|
|
|
|
return TemplateResponse(request, 'import_status.html', {
|
|
|
|
'job': job,
|
|
|
|
'items': job.items.order_by('index').all(),
|
|
|
|
'task': task
|
|
|
|
})
|
|
|
|
|
2020-03-17 00:45:34 +00:00
|
|
|
|
2020-03-15 21:15:36 +00:00
|
|
|
def login_page(request):
|
2020-01-25 23:25:19 +00:00
|
|
|
''' authentication '''
|
|
|
|
# send user to the login page
|
2020-03-15 21:15:36 +00:00
|
|
|
data = {
|
2020-06-01 18:54:08 +00:00
|
|
|
'site_settings': models.SiteSettings.get(),
|
2020-03-15 21:15:36 +00:00
|
|
|
'login_form': forms.LoginForm(),
|
|
|
|
'register_form': forms.RegisterForm(),
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'login.html', data)
|
2020-01-29 03:05:59 +00:00
|
|
|
|
|
|
|
|
2020-06-01 18:54:08 +00:00
|
|
|
def about_page(request):
|
|
|
|
''' more information about the instance '''
|
|
|
|
data = {
|
|
|
|
'site_settings': models.SiteSettings.get(),
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'about.html', data)
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
|
2020-06-01 21:34:45 +00:00
|
|
|
def invite_page(request, code):
|
2020-09-17 20:02:52 +00:00
|
|
|
''' endpoint for sending invites '''
|
2020-06-01 21:34:45 +00:00
|
|
|
try:
|
|
|
|
invite = models.SiteInvite.objects.get(code=code)
|
|
|
|
if not invite.valid():
|
|
|
|
raise PermissionDenied
|
|
|
|
except models.SiteInvite.DoesNotExist:
|
|
|
|
raise PermissionDenied
|
|
|
|
|
|
|
|
data = {
|
|
|
|
'site_settings': models.SiteSettings.get(),
|
|
|
|
'register_form': forms.RegisterForm(),
|
|
|
|
'invite': invite,
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'invite.html', data)
|
|
|
|
|
2020-06-03 16:38:30 +00:00
|
|
|
@login_required
|
|
|
|
def manage_invites(request):
|
2020-09-17 20:02:52 +00:00
|
|
|
''' invite management page '''
|
2020-06-03 16:38:30 +00:00
|
|
|
data = {
|
|
|
|
'invites': models.SiteInvite.objects.filter(user=request.user),
|
|
|
|
'form': forms.CreateInviteForm(),
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'manage_invites.html', data)
|
|
|
|
|
2020-06-01 18:54:08 +00:00
|
|
|
|
2020-03-10 20:31:57 +00:00
|
|
|
@login_required
|
2020-03-07 22:50:29 +00:00
|
|
|
def notifications_page(request):
|
|
|
|
''' list notitications '''
|
2020-03-10 18:23:23 +00:00
|
|
|
notifications = request.user.notification_set.all() \
|
|
|
|
.order_by('-created_date')
|
2020-03-10 20:31:57 +00:00
|
|
|
unread = [n.id for n in notifications.filter(read=False)]
|
2020-03-07 22:50:29 +00:00
|
|
|
data = {
|
2020-03-10 18:23:23 +00:00
|
|
|
'notifications': notifications,
|
2020-03-10 20:31:57 +00:00
|
|
|
'unread': unread,
|
2020-03-07 22:50:29 +00:00
|
|
|
}
|
2020-03-10 18:23:23 +00:00
|
|
|
notifications.update(read=True)
|
2020-03-07 22:50:29 +00:00
|
|
|
return TemplateResponse(request, 'notifications.html', data)
|
|
|
|
|
2020-03-14 00:57:36 +00:00
|
|
|
@csrf_exempt
|
2020-03-17 00:09:45 +00:00
|
|
|
def user_page(request, username, subpage=None):
|
2020-01-26 20:14:27 +00:00
|
|
|
''' profile page for a user '''
|
2020-01-28 06:49:56 +00:00
|
|
|
try:
|
2020-02-22 00:03:05 +00:00
|
|
|
user = get_user_from_username(username)
|
2020-01-28 06:49:56 +00:00
|
|
|
except models.User.DoesNotExist:
|
2020-02-22 00:03:05 +00:00
|
|
|
return HttpResponseNotFound()
|
2020-01-28 06:49:56 +00:00
|
|
|
|
2020-03-14 00:57:36 +00:00
|
|
|
if is_api_request(request):
|
|
|
|
# we have a json request
|
2020-09-17 20:02:52 +00:00
|
|
|
return JsonResponse(user.to_activity(), encoder=ActivityEncoder)
|
2020-03-14 00:57:36 +00:00
|
|
|
# otherwise we're at a UI view
|
|
|
|
|
2020-02-07 23:29:11 +00:00
|
|
|
# TODO: change display with privacy and authentication considerations
|
2020-03-16 23:10:59 +00:00
|
|
|
|
2020-01-26 20:14:27 +00:00
|
|
|
data = {
|
|
|
|
'user': user,
|
|
|
|
'is_self': request.user.id == user.id,
|
|
|
|
}
|
2020-03-17 00:09:45 +00:00
|
|
|
if subpage == 'followers':
|
|
|
|
data['followers'] = user.followers.all()
|
|
|
|
return TemplateResponse(request, 'followers.html', data)
|
2020-04-22 13:53:22 +00:00
|
|
|
if subpage == 'following':
|
2020-03-17 00:09:45 +00:00
|
|
|
data['following'] = user.following.all()
|
|
|
|
return TemplateResponse(request, 'following.html', data)
|
2020-04-22 13:53:22 +00:00
|
|
|
if subpage == 'shelves':
|
2020-03-17 00:29:37 +00:00
|
|
|
data['shelves'] = user.shelf_set.all()
|
|
|
|
return TemplateResponse(request, 'user_shelves.html', data)
|
2020-04-22 13:53:22 +00:00
|
|
|
|
|
|
|
shelves = get_user_shelf_preview(user)
|
|
|
|
data['shelves'] = shelves
|
|
|
|
activities = get_activity_feed(user, 'self')[:15]
|
|
|
|
data['activities'] = activities
|
|
|
|
return TemplateResponse(request, 'user.html', data)
|
2020-01-26 20:14:27 +00:00
|
|
|
|
|
|
|
|
2020-03-14 00:57:36 +00:00
|
|
|
@csrf_exempt
|
|
|
|
def followers_page(request, username):
|
|
|
|
''' list of followers '''
|
|
|
|
if request.method != 'GET':
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
try:
|
|
|
|
user = get_user_from_username(username)
|
|
|
|
except models.User.DoesNotExist:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
if is_api_request(request):
|
2020-09-17 20:02:52 +00:00
|
|
|
return JsonResponse(user.to_followers_activity(**request.GET))
|
2020-03-14 00:57:36 +00:00
|
|
|
|
2020-03-17 00:09:45 +00:00
|
|
|
return user_page(request, username, subpage='followers')
|
2020-03-14 00:57:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def following_page(request, username):
|
|
|
|
''' list of followers '''
|
|
|
|
if request.method != 'GET':
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
try:
|
|
|
|
user = get_user_from_username(username)
|
|
|
|
except models.User.DoesNotExist:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
if is_api_request(request):
|
2020-09-17 20:02:52 +00:00
|
|
|
return JsonResponse(user.to_following_activity(**request.GET))
|
2020-03-14 00:57:36 +00:00
|
|
|
|
2020-03-17 00:09:45 +00:00
|
|
|
return user_page(request, username, subpage='following')
|
2020-03-14 00:57:36 +00:00
|
|
|
|
|
|
|
|
2020-03-17 00:29:37 +00:00
|
|
|
@csrf_exempt
|
|
|
|
def user_shelves_page(request, username):
|
|
|
|
''' list of followers '''
|
|
|
|
if request.method != 'GET':
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
return user_page(request, username, subpage='shelves')
|
|
|
|
|
|
|
|
|
2020-03-14 00:57:36 +00:00
|
|
|
@csrf_exempt
|
2020-03-07 23:28:11 +00:00
|
|
|
def status_page(request, username, status_id):
|
|
|
|
''' display a particular status (and replies, etc) '''
|
2020-03-14 00:57:36 +00:00
|
|
|
if request.method != 'GET':
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
2020-03-07 23:28:11 +00:00
|
|
|
try:
|
2020-03-10 18:28:11 +00:00
|
|
|
user = get_user_from_username(username)
|
2020-03-07 23:28:11 +00:00
|
|
|
status = models.Status.objects.select_subclasses().get(id=status_id)
|
|
|
|
except ValueError:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
if user != status.user:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
2020-03-14 00:57:36 +00:00
|
|
|
if is_api_request(request):
|
2020-09-17 20:02:52 +00:00
|
|
|
return JsonResponse(status.to_activity(), encoder=ActivityEncoder)
|
2020-03-14 00:57:36 +00:00
|
|
|
|
2020-03-07 23:28:11 +00:00
|
|
|
data = {
|
|
|
|
'status': status,
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'status.html', data)
|
|
|
|
|
|
|
|
|
2020-03-14 00:57:36 +00:00
|
|
|
@csrf_exempt
|
|
|
|
def replies_page(request, username, status_id):
|
|
|
|
''' ordered collection of replies to a status '''
|
|
|
|
if request.method != 'GET':
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
if not is_api_request(request):
|
|
|
|
return status_page(request, username, status_id)
|
|
|
|
|
|
|
|
status = models.Status.objects.get(id=status_id)
|
|
|
|
if status.user.localname != username:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
return JsonResponse(
|
|
|
|
status.to_replies(**request.GET),
|
|
|
|
encoder=ActivityEncoder
|
|
|
|
)
|
2020-03-14 00:57:36 +00:00
|
|
|
|
|
|
|
|
2020-01-28 06:49:56 +00:00
|
|
|
@login_required
|
2020-03-08 16:39:59 +00:00
|
|
|
def edit_profile_page(request):
|
2020-01-28 06:49:56 +00:00
|
|
|
''' profile page for a user '''
|
2020-03-08 16:39:59 +00:00
|
|
|
user = request.user
|
2020-01-28 06:49:56 +00:00
|
|
|
|
2020-01-29 20:24:50 +00:00
|
|
|
form = forms.EditUserForm(instance=request.user)
|
2020-01-28 06:49:56 +00:00
|
|
|
data = {
|
2020-01-29 07:23:05 +00:00
|
|
|
'form': form,
|
2020-01-28 06:49:56 +00:00
|
|
|
'user': user,
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'edit_user.html', data)
|
|
|
|
|
|
|
|
|
2020-05-04 00:53:14 +00:00
|
|
|
def book_page(request, book_id, tab='friends'):
|
2020-01-28 02:47:54 +00:00
|
|
|
''' info about a book '''
|
2020-05-10 21:53:45 +00:00
|
|
|
book = models.Book.objects.select_subclasses().get(id=book_id)
|
2020-03-28 02:52:05 +00:00
|
|
|
if is_api_request(request):
|
2020-09-17 20:02:52 +00:00
|
|
|
return JsonResponse(book.to_activity(), encoder=ActivityEncoder)
|
2020-03-28 02:52:05 +00:00
|
|
|
|
2020-03-07 06:56:44 +00:00
|
|
|
if isinstance(book, models.Work):
|
2020-03-30 22:03:21 +00:00
|
|
|
book = book.default_edition
|
2020-04-06 00:00:01 +00:00
|
|
|
if not book:
|
|
|
|
return HttpResponseNotFound()
|
2020-03-30 22:03:21 +00:00
|
|
|
|
|
|
|
work = book.parent_work
|
2020-04-04 20:12:15 +00:00
|
|
|
if not work:
|
|
|
|
return HttpResponseNotFound()
|
2020-03-30 21:12:18 +00:00
|
|
|
|
2020-03-30 22:03:21 +00:00
|
|
|
book_reviews = models.Review.objects.filter(book__in=work.edition_set.all())
|
2020-03-07 06:56:44 +00:00
|
|
|
|
2020-03-27 15:05:27 +00:00
|
|
|
if request.user.is_authenticated:
|
|
|
|
user_reviews = book_reviews.filter(
|
|
|
|
user=request.user,
|
|
|
|
).all()
|
|
|
|
|
2020-04-01 23:02:39 +00:00
|
|
|
reviews = get_activity_feed(request.user, tab, model=book_reviews)
|
2020-03-27 15:05:27 +00:00
|
|
|
|
|
|
|
try:
|
2020-03-30 00:40:51 +00:00
|
|
|
# TODO: books can be on multiple shelves
|
|
|
|
shelf = models.Shelf.objects.filter(
|
|
|
|
user=request.user,
|
2020-03-30 22:03:21 +00:00
|
|
|
edition=book
|
2020-03-30 00:40:51 +00:00
|
|
|
).first()
|
2020-03-27 15:05:27 +00:00
|
|
|
except models.Shelf.DoesNotExist:
|
|
|
|
shelf = None
|
|
|
|
|
|
|
|
user_tags = models.Tag.objects.filter(
|
|
|
|
book=book, user=request.user
|
2020-04-04 20:12:15 +00:00
|
|
|
).values_list('identifier', flat=True)
|
2020-02-23 22:26:03 +00:00
|
|
|
else:
|
2020-03-27 15:05:27 +00:00
|
|
|
tab = 'public'
|
|
|
|
reviews = book_reviews.filter(privacy='public')
|
2020-02-23 22:26:03 +00:00
|
|
|
shelf = None
|
2020-03-27 15:05:27 +00:00
|
|
|
user_reviews = []
|
|
|
|
user_tags = []
|
2020-02-23 22:26:03 +00:00
|
|
|
|
2020-01-28 02:47:54 +00:00
|
|
|
rating = reviews.aggregate(Avg('rating'))
|
2020-02-21 06:19:19 +00:00
|
|
|
tags = models.Tag.objects.filter(
|
|
|
|
book=book
|
|
|
|
).values(
|
2020-02-21 17:10:27 +00:00
|
|
|
'book', 'name', 'identifier'
|
2020-02-21 06:19:19 +00:00
|
|
|
).distinct().all()
|
|
|
|
|
2020-01-28 02:47:54 +00:00
|
|
|
data = {
|
|
|
|
'book': book,
|
2020-02-23 22:26:03 +00:00
|
|
|
'shelf': shelf,
|
|
|
|
'user_reviews': user_reviews,
|
2020-03-07 21:21:46 +00:00
|
|
|
'reviews': reviews.distinct(),
|
2020-01-28 02:47:54 +00:00
|
|
|
'rating': rating['rating__avg'],
|
2020-02-21 06:19:19 +00:00
|
|
|
'tags': tags,
|
|
|
|
'user_tags': user_tags,
|
2020-03-29 07:05:09 +00:00
|
|
|
'review_form': forms.ReviewForm(),
|
|
|
|
'tag_form': forms.TagForm(),
|
2020-03-17 00:19:38 +00:00
|
|
|
'feed_tabs': [
|
|
|
|
{'id': 'friends', 'display': 'Friends'},
|
|
|
|
{'id': 'local', 'display': 'Local'},
|
|
|
|
{'id': 'federated', 'display': 'Federated'}
|
|
|
|
],
|
2020-02-23 22:26:03 +00:00
|
|
|
'active_tab': tab,
|
2020-05-04 00:53:14 +00:00
|
|
|
'path': '/book/%s' % book_id,
|
2020-03-28 22:06:16 +00:00
|
|
|
'cover_form': forms.CoverForm(instance=book),
|
2020-04-02 02:38:07 +00:00
|
|
|
'info_fields': [
|
2020-04-29 17:09:14 +00:00
|
|
|
{'name': 'ISBN', 'value': book.isbn_13},
|
2020-04-02 02:38:07 +00:00
|
|
|
{'name': 'OCLC number', 'value': book.oclc_number},
|
|
|
|
{'name': 'OpenLibrary ID', 'value': book.openlibrary_key},
|
|
|
|
{'name': 'Goodreads ID', 'value': book.goodreads_key},
|
|
|
|
{'name': 'Format', 'value': book.physical_format},
|
|
|
|
{'name': 'Pages', 'value': book.pages},
|
|
|
|
],
|
2020-01-28 02:47:54 +00:00
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'book.html', data)
|
|
|
|
|
|
|
|
|
2020-03-28 22:06:16 +00:00
|
|
|
@login_required
|
2020-05-04 00:53:14 +00:00
|
|
|
def edit_book_page(request, book_id):
|
2020-03-28 22:06:16 +00:00
|
|
|
''' info about a book '''
|
2020-05-11 01:08:18 +00:00
|
|
|
book = books_manager.get_edition(book_id)
|
2020-04-04 20:46:10 +00:00
|
|
|
if not book.description:
|
|
|
|
book.description = book.parent_work.description
|
2020-03-28 22:06:16 +00:00
|
|
|
data = {
|
|
|
|
'book': book,
|
2020-04-02 15:44:53 +00:00
|
|
|
'form': forms.EditionForm(instance=book)
|
2020-03-28 22:06:16 +00:00
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'edit_book.html', data)
|
|
|
|
|
|
|
|
|
2020-03-30 22:03:21 +00:00
|
|
|
def editions_page(request, work_id):
|
|
|
|
''' list of editions of a book '''
|
|
|
|
work = models.Work.objects.get(id=work_id)
|
|
|
|
editions = models.Edition.objects.filter(parent_work=work).all()
|
|
|
|
data = {
|
|
|
|
'editions': editions,
|
|
|
|
'work': work,
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'editions.html', data)
|
|
|
|
|
|
|
|
|
2020-05-04 00:53:14 +00:00
|
|
|
def author_page(request, author_id):
|
2020-02-11 06:32:03 +00:00
|
|
|
''' landing page for an author '''
|
|
|
|
try:
|
2020-05-04 00:53:14 +00:00
|
|
|
author = models.Author.objects.get(id=author_id)
|
2020-02-11 06:32:03 +00:00
|
|
|
except ValueError:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
2020-05-09 19:09:40 +00:00
|
|
|
if is_api_request(request):
|
2020-09-17 20:02:52 +00:00
|
|
|
return JsonResponse(author.to_activity(), encoder=ActivityEncoder)
|
2020-05-09 19:09:40 +00:00
|
|
|
|
2020-03-30 22:03:21 +00:00
|
|
|
books = models.Work.objects.filter(authors=author)
|
2020-02-11 06:32:03 +00:00
|
|
|
data = {
|
|
|
|
'author': author,
|
2020-03-30 22:03:21 +00:00
|
|
|
'books': [b.default_edition for b in books],
|
2020-02-11 06:32:03 +00:00
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'author.html', data)
|
|
|
|
|
|
|
|
|
2020-02-21 17:10:27 +00:00
|
|
|
def tag_page(request, tag_id):
|
|
|
|
''' books related to a tag '''
|
2020-02-21 17:15:20 +00:00
|
|
|
tag_obj = models.Tag.objects.filter(identifier=tag_id).first()
|
2020-09-17 20:02:52 +00:00
|
|
|
if not tag_obj:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
if is_api_request(request):
|
|
|
|
return JsonResponse(
|
|
|
|
tag_obj.to_activity(**request.GET), encoder=ActivityEncoder)
|
|
|
|
|
2020-03-30 22:03:21 +00:00
|
|
|
books = models.Edition.objects.filter(tag__identifier=tag_id).distinct()
|
2020-02-21 17:10:27 +00:00
|
|
|
data = {
|
|
|
|
'books': books,
|
2020-02-21 17:15:20 +00:00
|
|
|
'tag': tag_obj,
|
2020-02-21 17:10:27 +00:00
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'tag.html', data)
|
|
|
|
|
|
|
|
|
2020-02-22 00:03:05 +00:00
|
|
|
def shelf_page(request, username, shelf_identifier):
|
|
|
|
''' display a shelf '''
|
|
|
|
try:
|
|
|
|
user = get_user_from_username(username)
|
|
|
|
except models.User.DoesNotExist:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
shelf = models.Shelf.objects.get(user=user, identifier=shelf_identifier)
|
2020-03-28 02:52:05 +00:00
|
|
|
|
|
|
|
if is_api_request(request):
|
2020-09-17 20:02:52 +00:00
|
|
|
return JsonResponse(shelf.to_activity(**request.GET))
|
2020-03-28 02:52:05 +00:00
|
|
|
|
2020-02-22 00:40:49 +00:00
|
|
|
data = {
|
|
|
|
'shelf': shelf,
|
|
|
|
'user': user,
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'shelf.html', data)
|
2020-02-22 00:03:05 +00:00
|
|
|
|
2020-03-16 23:10:59 +00:00
|
|
|
|
|
|
|
def get_user_shelf_preview(user, shelf_proportions=None):
|
|
|
|
''' data for the covers shelf (user page and feed page) '''
|
|
|
|
shelves = []
|
|
|
|
shelf_max = 6
|
|
|
|
if not shelf_proportions:
|
|
|
|
shelf_proportions = [('reading', 3), ('read', 2), ('to-read', -1)]
|
|
|
|
for (identifier, count) in shelf_proportions:
|
|
|
|
if shelf_max <= 0:
|
|
|
|
break
|
|
|
|
if count > shelf_max or count < 0:
|
|
|
|
count = shelf_max
|
2020-03-17 00:09:45 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
shelf = models.Shelf.objects.get(
|
|
|
|
user=user,
|
|
|
|
identifier=identifier,
|
|
|
|
)
|
|
|
|
except models.Shelf.DoesNotExist:
|
|
|
|
continue
|
|
|
|
|
2020-03-16 23:10:59 +00:00
|
|
|
if not shelf.books.count():
|
|
|
|
continue
|
|
|
|
books = models.ShelfBook.objects.filter(
|
|
|
|
shelf=shelf,
|
|
|
|
).order_by(
|
|
|
|
'-updated_date'
|
|
|
|
)[:count]
|
|
|
|
|
|
|
|
shelf_max -= len(books)
|
|
|
|
|
|
|
|
shelves.append({
|
|
|
|
'name': shelf.name,
|
|
|
|
'identifier': shelf.identifier,
|
|
|
|
'books': [b.book for b in books],
|
|
|
|
'size': shelf.books.count(),
|
|
|
|
})
|
|
|
|
return shelves
|