2020-01-25 23:25:19 +00:00
|
|
|
''' application views/pages '''
|
|
|
|
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-01-28 19:45:27 +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-28 03:57:17 +00:00
|
|
|
import re
|
2020-01-25 06:32:41 +00:00
|
|
|
|
2020-01-29 19:45:19 +00:00
|
|
|
from fedireads import forms, models, openlibrary, outgoing, incoming
|
2020-01-29 03:05:59 +00:00
|
|
|
from fedireads.settings import DOMAIN
|
2020-01-28 19:45:27 +00:00
|
|
|
|
|
|
|
|
2020-01-25 06:32:41 +00:00
|
|
|
@login_required
|
2020-01-25 23:25:19 +00:00
|
|
|
def home(request):
|
2020-01-28 19:45:27 +00:00
|
|
|
''' user's homepage with activity feed '''
|
2020-02-07 23:29:11 +00:00
|
|
|
# user's shelves for display
|
2020-01-29 08:05:58 +00:00
|
|
|
reading = models.Shelf.objects.get(
|
|
|
|
user=request.user,
|
|
|
|
shelf_type='reading'
|
|
|
|
)
|
|
|
|
to_read = models.Shelf.objects.get(
|
|
|
|
user=request.user,
|
|
|
|
shelf_type='to-read'
|
|
|
|
)
|
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-01-29 09:25:56 +00:00
|
|
|
'-added_date'
|
|
|
|
)[: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)
|
|
|
|
)
|
2020-02-15 05:00:11 +00:00
|
|
|
# TODO: this is fundamentally not how the feed should work I think? it
|
|
|
|
# should do something smart with inboxes. (in this implementation it would
|
|
|
|
# show DMs meant for other local users)
|
2020-01-28 02:47:54 +00:00
|
|
|
activities = models.Activity.objects.filter(
|
2020-01-28 23:23:49 +00:00
|
|
|
user__in=following,
|
|
|
|
).select_subclasses().order_by(
|
|
|
|
'-created_date'
|
|
|
|
)[:10]
|
2020-01-28 02:47:54 +00:00
|
|
|
|
2020-01-25 23:25:19 +00:00
|
|
|
data = {
|
|
|
|
'user': request.user,
|
2020-01-29 08:05:58 +00:00
|
|
|
'reading': reading,
|
|
|
|
'to_read': to_read,
|
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,
|
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-01-26 20:14:27 +00:00
|
|
|
def user_profile(request, username):
|
|
|
|
''' 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:
|
|
|
|
user = models.User.objects.get(localname=username)
|
|
|
|
except models.User.DoesNotExist:
|
|
|
|
try:
|
|
|
|
user = models.User.objects.get(username=username)
|
|
|
|
except models.User.DoesNotExist:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
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-01-28 06:49:56 +00:00
|
|
|
@login_required
|
|
|
|
def user_profile_edit(request, username):
|
|
|
|
''' profile page for a user '''
|
|
|
|
try:
|
|
|
|
user = models.User.objects.get(localname=username)
|
|
|
|
except models.User.DoesNotExist:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
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-02-07 23:29:11 +00:00
|
|
|
# TODO: there oughta be clear naming between endpoints and pages
|
2020-01-28 06:49:56 +00:00
|
|
|
@login_required
|
2020-01-29 07:23:05 +00:00
|
|
|
def edit_profile(request):
|
2020-01-28 06:49:56 +00:00
|
|
|
''' les get fancy with images '''
|
|
|
|
if not request.method == 'POST':
|
2020-01-29 07:23:05 +00:00
|
|
|
return redirect('/user/%s' % request.user.localname)
|
2020-01-29 20:24:50 +00:00
|
|
|
|
2020-01-29 07:23:05 +00:00
|
|
|
form = forms.EditUserForm(request.POST, request.FILES)
|
|
|
|
if not form.is_valid():
|
2020-01-28 06:49:56 +00:00
|
|
|
return redirect('/')
|
2020-01-29 20:24:50 +00:00
|
|
|
|
2020-01-29 07:23:05 +00:00
|
|
|
request.user.name = form.data['name']
|
|
|
|
if 'avatar' in form.files:
|
|
|
|
request.user.avatar = form.files['avatar']
|
|
|
|
request.user.summary = form.data['summary']
|
2020-01-28 06:49:56 +00:00
|
|
|
request.user.save()
|
|
|
|
return redirect('/user/%s' % request.user.localname)
|
|
|
|
|
|
|
|
|
2020-01-28 02:47:54 +00:00
|
|
|
@login_required
|
|
|
|
def book_page(request, book_identifier):
|
|
|
|
''' info about a book '''
|
2020-02-11 05:09:04 +00:00
|
|
|
book = openlibrary.get_or_create_book(book_identifier)
|
2020-02-07 23:29:11 +00:00
|
|
|
# TODO: again, post privacy?
|
2020-01-29 09:05:27 +00:00
|
|
|
reviews = models.Review.objects.filter(book=book)
|
2020-01-28 02:47:54 +00:00
|
|
|
rating = reviews.aggregate(Avg('rating'))
|
2020-01-29 07:23:05 +00:00
|
|
|
review_form = forms.ReviewForm()
|
2020-01-28 02:47:54 +00:00
|
|
|
data = {
|
|
|
|
'book': book,
|
|
|
|
'reviews': reviews,
|
|
|
|
'rating': rating['rating__avg'],
|
2020-01-29 07:23:05 +00:00
|
|
|
'review_form': review_form,
|
2020-01-28 02:47:54 +00:00
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'book.html', data)
|
|
|
|
|
|
|
|
|
2020-02-11 06:32:03 +00:00
|
|
|
@login_required
|
|
|
|
def author_page(request, author_identifier):
|
|
|
|
''' landing page for an author '''
|
|
|
|
try:
|
|
|
|
author = models.Author.objects.get(openlibrary_key=author_identifier)
|
|
|
|
except ValueError:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
books = models.Book.objects.filter(authors=author)
|
|
|
|
data = {
|
|
|
|
'author': author,
|
|
|
|
'books': books,
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'author.html', data)
|
|
|
|
|
|
|
|
|
2020-01-27 01:55:02 +00:00
|
|
|
@login_required
|
2020-01-29 08:22:48 +00:00
|
|
|
def shelve(request, shelf_id, book_id, reshelve=True):
|
2020-01-27 01:55:02 +00:00
|
|
|
''' put a book on a user's shelf '''
|
|
|
|
book = models.Book.objects.get(id=book_id)
|
2020-01-29 08:22:48 +00:00
|
|
|
desired_shelf = models.Shelf.objects.get(identifier=shelf_id)
|
|
|
|
if reshelve:
|
|
|
|
try:
|
2020-01-29 19:45:19 +00:00
|
|
|
current_shelf = models.Shelf.objects.get(
|
|
|
|
user=request.user,
|
|
|
|
book=book
|
|
|
|
)
|
|
|
|
outgoing.handle_unshelve(request.user, book, current_shelf)
|
2020-01-29 08:22:48 +00:00
|
|
|
except models.Shelf.DoesNotExist:
|
|
|
|
pass
|
2020-01-29 19:45:19 +00:00
|
|
|
outgoing.handle_shelve(request.user, book, desired_shelf)
|
2020-01-27 01:55:02 +00:00
|
|
|
return redirect('/')
|
|
|
|
|
2020-01-28 19:45:27 +00:00
|
|
|
|
2020-01-28 02:47:54 +00:00
|
|
|
@login_required
|
|
|
|
def review(request):
|
|
|
|
''' create a book review note '''
|
2020-01-29 07:23:05 +00:00
|
|
|
form = forms.ReviewForm(request.POST)
|
|
|
|
# TODO: better failure behavior
|
|
|
|
if not form.is_valid():
|
|
|
|
return redirect('/')
|
2020-01-28 02:47:54 +00:00
|
|
|
book_identifier = request.POST.get('book')
|
2020-01-28 19:45:27 +00:00
|
|
|
|
|
|
|
# TODO: validation, htmlification
|
2020-01-29 07:23:05 +00:00
|
|
|
name = form.data.get('name')
|
2020-02-15 19:13:49 +00:00
|
|
|
content = form.data.get('content')
|
|
|
|
rating = int(form.data.get('rating'))
|
2020-01-28 19:45:27 +00:00
|
|
|
|
2020-02-15 19:13:49 +00:00
|
|
|
outgoing.handle_review(request.user, book_identifier, name, content, rating)
|
2020-02-11 05:09:04 +00:00
|
|
|
return redirect('/book/%s' % book_identifier)
|
2020-01-28 02:47:54 +00:00
|
|
|
|
2020-01-27 01:55:02 +00:00
|
|
|
|
2020-01-26 20:14:27 +00:00
|
|
|
@login_required
|
|
|
|
def follow(request):
|
2020-01-27 01:55:02 +00:00
|
|
|
''' follow another user, here or abroad '''
|
2020-01-27 03:50:22 +00:00
|
|
|
to_follow = request.POST.get('user')
|
2020-01-28 19:45:27 +00:00
|
|
|
# should this be an actor rather than an id? idk
|
2020-01-27 03:50:22 +00:00
|
|
|
to_follow = models.User.objects.get(id=to_follow)
|
2020-01-26 20:14:27 +00:00
|
|
|
|
2020-01-29 19:45:19 +00:00
|
|
|
outgoing.handle_outgoing_follow(request.user, to_follow)
|
2020-01-27 03:50:22 +00:00
|
|
|
return redirect('/user/%s' % to_follow.username)
|
2020-01-26 20:14:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def unfollow(request):
|
2020-01-27 01:55:02 +00:00
|
|
|
''' unfollow a user '''
|
2020-01-28 19:45:27 +00:00
|
|
|
# TODO: this is not an implementation!!
|
2020-01-26 20:14:27 +00:00
|
|
|
followed = request.POST.get('user')
|
|
|
|
followed = models.User.objects.get(id=followed)
|
|
|
|
followed.followers.remove(request.user)
|
|
|
|
return redirect('/user/%s' % followed.username)
|
|
|
|
|
2020-01-28 03:57:17 +00:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
def search(request):
|
|
|
|
''' that search bar up top '''
|
|
|
|
query = request.GET.get('q')
|
|
|
|
if re.match(r'\w+@\w+.\w+', query):
|
2020-01-29 19:45:19 +00:00
|
|
|
# if something looks like a username, search with webfinger
|
|
|
|
results = [outgoing.handle_account_search(query)]
|
2020-01-29 09:05:27 +00:00
|
|
|
template = 'user_results.html'
|
2020-01-28 03:57:17 +00:00
|
|
|
else:
|
2020-01-29 19:45:19 +00:00
|
|
|
# just send the question over to openlibrary for book search
|
2020-01-29 09:05:27 +00:00
|
|
|
results = openlibrary.book_search(query)
|
|
|
|
template = 'book_results.html'
|
2020-01-28 03:57:17 +00:00
|
|
|
|
2020-01-29 09:05:27 +00:00
|
|
|
return TemplateResponse(request, template, {'results': results})
|
2020-01-28 03:57:17 +00:00
|
|
|
|