bookwyrm/fedireads/views.py

326 lines
9.5 KiB
Python
Raw Normal View History

2020-02-22 22:02:03 +00:00
''' views for pages you can go to in the application '''
2020-01-25 23:25:19 +00:00
from django.contrib.auth import authenticate, login, logout
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-02-22 22:02:03 +00:00
from django.http import HttpResponseNotFound
2020-01-25 23:25:19 +00:00
from django.shortcuts import redirect
from django.template.response import TemplateResponse
2020-01-25 06:32:41 +00:00
2020-03-07 20:22:28 +00:00
from fedireads import forms, models, books_manager, incoming
2020-01-29 03:05:59 +00:00
from fedireads.settings import DOMAIN
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-01-25 06:32:41 +00:00
@login_required
2020-01-25 23:25:19 +00:00
def home(request):
''' 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-02-21 23:39:25 +00:00
shelves = []
for identifier in ['reading', 'to-read']:
shelf = models.Shelf.objects.get(
user=request.user,
identifier=identifier,
)
if not shelf.books.count():
continue
shelves.append({
'name': shelf.name,
'identifier': shelf.identifier,
'books': shelf.books.all()[:3],
'size': shelf.books.count(),
})
2020-02-07 23:29:11 +00:00
# allows us to check if a user has shelved a book
2020-01-29 01:23:38 +00:00
user_books = models.Book.objects.filter(shelves__user=request.user).all()
2020-02-07 23:29:11 +00:00
# books new to the instance, for discovery
2020-01-27 01:55:02 +00:00
recent_books = models.Book.objects.order_by(
2020-02-17 03:17:11 +00:00
'-created_date'
2020-01-29 09:25:56 +00:00
)[:5]
2020-01-28 02:47:54 +00:00
2020-02-07 23:29:11 +00:00
# status updates for your follow network
2020-01-28 02:47:54 +00:00
following = models.User.objects.filter(
2020-01-28 19:45:27 +00:00
Q(followers=request.user) | Q(id=request.user.id)
)
activities = models.Status.objects.order_by(
2020-01-28 23:23:49 +00:00
'-created_date'
).select_subclasses()
if tab == 'home':
# people you follow and direct mentions
activities = activities.filter(
Q(user__in=following, privacy='public') | \
Q(mention_users=request.user)
)
elif tab == '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')
activities = activities[:10]
2020-01-28 02:47:54 +00:00
2020-01-25 23:25:19 +00:00
data = {
'user': request.user,
2020-02-21 23:39:25 +00:00
'shelves': shelves,
2020-01-27 01:55:02 +00:00
'recent_books': recent_books,
2020-01-29 01:23:38 +00:00
'user_books': user_books,
2020-01-28 02:47:54 +00:00
'activities': activities,
'feed_tabs': ['home', 'local', 'federated'],
'active_tab': tab,
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-01-25 23:25:19 +00:00
def user_login(request):
''' authentication '''
# send user to the login page
if request.method == 'GET':
2020-01-29 07:23:05 +00:00
form = forms.LoginForm()
return TemplateResponse(request, 'login.html', {'login_form': form})
2020-01-25 06:32:41 +00:00
2020-01-25 23:25:19 +00:00
# authenticate user
2020-01-29 07:23:05 +00:00
form = forms.LoginForm(request.POST)
if not form.is_valid():
2020-02-11 05:49:18 +00:00
return TemplateResponse(request, 'login.html', {'login_form': form})
2020-01-29 07:23:05 +00:00
username = form.data['username']
2020-01-29 03:05:59 +00:00
username = '%s@%s' % (username, DOMAIN)
2020-01-29 07:23:05 +00:00
password = form.data['password']
2020-01-25 23:25:19 +00:00
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect(request.GET.get('next', '/'))
2020-02-11 05:49:18 +00:00
return TemplateResponse(request, 'login.html', {'login_form': form})
2020-01-25 23:25:19 +00:00
2020-01-28 02:47:54 +00:00
2020-01-25 23:25:19 +00:00
@login_required
def user_logout(request):
2020-01-26 20:14:27 +00:00
''' done with this place! outa here! '''
2020-01-25 23:25:19 +00:00
logout(request)
return redirect('/')
2020-01-26 20:14:27 +00:00
2020-01-29 03:05:59 +00:00
def register(request):
''' join the server '''
if request.method == 'GET':
2020-01-29 07:23:05 +00:00
form = forms.RegisterForm()
return TemplateResponse(
request,
'register.html',
{'register_form': form}
)
2020-01-29 03:05:59 +00:00
2020-01-29 07:23:05 +00:00
form = forms.RegisterForm(request.POST)
if not form.is_valid():
return redirect('/register/')
username = form.data['username']
email = form.data['email']
password = form.data['password']
2020-01-29 03:05:59 +00:00
user = models.User.objects.create_user(username, email, password)
login(request, user)
return redirect('/')
2020-03-07 22:50:29 +00:00
def notifications_page(request):
''' list notitications '''
data = {
'notifications': request.user.notification_set.all().order_by('-created_date')
}
request.user.notification_set.update(read=True)
return TemplateResponse(request, 'notifications.html', data)
2020-03-07 23:28:11 +00:00
@login_required
2020-02-22 22:02:03 +00:00
def user_page(request, username):
2020-01-26 20:14:27 +00:00
''' profile page for a user '''
2020-01-29 19:45:19 +00:00
content = request.headers.get('Accept')
2020-02-07 23:29:11 +00:00
# TODO: this should probably be the full content type? maybe?
2020-01-29 19:45:19 +00:00
if 'json' in content:
# we have a json request
return incoming.get_actor(request, username)
# otherwise we're at a UI view
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-02-07 23:29:11 +00:00
# TODO: change display with privacy and authentication considerations
2020-01-29 23:10:32 +00:00
shelves = models.Shelf.objects.filter(user=user)
2020-02-07 23:29:11 +00:00
ratings = {r.book.id: r.rating for r in \
models.Review.objects.filter(user=user, book__shelves__user=user)}
2020-01-29 23:10:32 +00:00
2020-01-26 20:14:27 +00:00
data = {
'user': user,
2020-01-29 23:10:32 +00:00
'shelves': shelves,
'ratings': ratings,
2020-01-26 20:14:27 +00:00
'is_self': request.user.id == user.id,
}
return TemplateResponse(request, 'user.html', data)
2020-03-07 23:28:11 +00:00
@login_required
def status_page(request, username, status_id):
''' display a particular status (and replies, etc) '''
content = request.headers.get('Accept')
if 'json' in content:
# we have a json request
return incoming.get_status(request, username, status_id)
try:
user = models.User.objects.get(localname=username)
status = models.Status.objects.select_subclasses().get(id=status_id)
except ValueError:
return HttpResponseNotFound()
if user != status.user:
return HttpResponseNotFound()
data = {
'status': status,
}
return TemplateResponse(request, 'status.html', data)
2020-01-28 06:49:56 +00:00
@login_required
def edit_profile_page(request):
2020-01-28 06:49:56 +00:00
''' profile page for a user '''
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)
@login_required
def book_page(request, book_identifier, tab='friends'):
2020-01-28 02:47:54 +00:00
''' info about a book '''
2020-03-07 20:22:28 +00:00
book = books_manager.get_or_create_book(book_identifier)
2020-03-07 06:56:44 +00:00
if isinstance(book, models.Work):
book_reviews = models.Review.objects.filter(
Q(book=book) | Q(book__parent_work=book),
)
else:
book_reviews = models.Review.objects.filter(book=book)
user_reviews = book_reviews.filter(
user=request.user,
).all()
if tab == 'friends':
2020-03-07 06:56:44 +00:00
reviews = book_reviews.filter(
Q(user__followers=request.user, privacy='public') | \
2020-03-07 06:56:44 +00:00
Q(user=request.user) | \
Q(mention_users=request.user),
)
elif tab == 'local':
2020-03-07 06:56:44 +00:00
reviews = book_reviews.filter(
Q(privacy='public') | \
Q(mention_users=request.user),
user__local=True,
)
else:
2020-03-07 06:56:44 +00:00
reviews = book_reviews.filter(
Q(privacy='public') | \
Q(mention_users=request.user),
)
try:
shelf = models.Shelf.objects.get(user=request.user, book=book)
except models.Shelf.DoesNotExist:
shelf = None
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()
user_tags = models.Tag.objects.filter(
book=book, user=request.user
).all()
2020-02-21 06:19:19 +00:00
2020-01-29 07:23:05 +00:00
review_form = forms.ReviewForm()
2020-02-21 06:19:19 +00:00
tag_form = forms.TagForm()
2020-01-28 02:47:54 +00:00
data = {
'book': book,
'shelf': shelf,
'user_reviews': user_reviews,
'user_rating': user_reviews.aggregate(Avg('rating')),
'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,
'user_tag_names': user_tags.values_list('identifier', flat=True),
2020-01-29 07:23:05 +00:00
'review_form': review_form,
2020-02-21 06:19:19 +00:00
'tag_form': tag_form,
'feed_tabs': ['friends', 'local', 'federated'],
'active_tab': tab,
'path': '/book/%s' % book_identifier,
2020-01-28 02:47:54 +00:00
}
return TemplateResponse(request, 'book.html', data)
2020-02-11 06:32:03 +00:00
def author_page(request, author_identifier):
''' landing page for an author '''
try:
2020-03-07 20:22:28 +00:00
author = models.Author.objects.get(books_manager_key=author_identifier)
2020-02-11 06:32:03 +00:00
except ValueError:
return HttpResponseNotFound()
books = models.Book.objects.filter(authors=author)
data = {
'author': author,
'books': books,
}
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-02-21 17:32:24 +00:00
books = models.Book.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 '''
# TODO: json view
try:
user = get_user_from_username(username)
except models.User.DoesNotExist:
return HttpResponseNotFound()
shelf = models.Shelf.objects.get(user=user, identifier=shelf_identifier)
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