moviewyrm/fedireads/views.py

197 lines
5.5 KiB
Python
Raw Normal View History

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-01-28 02:47:54 +00:00
from django.db.models import Avg, FilteredRelation, 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
from django.views.decorators.csrf import csrf_exempt
2020-01-28 03:57:17 +00:00
import re
2020-01-25 06:32:41 +00:00
2020-01-28 19:45:27 +00:00
from fedireads import models, openlibrary, outgoing as api
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-01-26 20:14:27 +00:00
shelves = models.Shelf.objects.filter(user=request.user.id)
2020-01-27 01:55:02 +00:00
recent_books = models.Book.objects.order_by(
'added_date'
).annotate(
user_shelves=FilteredRelation(
'shelves',
condition=Q(shelves__user_id=request.user.id)
)
2020-01-28 19:45:27 +00:00
).values(
'id', 'authors', 'data', 'user_shelves', 'openlibrary_key'
).distinct()
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-01-28 02:47:54 +00:00
2020-01-28 19:45:27 +00:00
# TODO: handle post privacy
2020-01-28 02:47:54 +00:00
activities = models.Activity.objects.filter(
user__in=following
).order_by('-created_date')[:10]
2020-01-25 23:25:19 +00:00
data = {
'user': request.user,
'shelves': shelves,
2020-01-27 01:55:02 +00:00
'recent_books': recent_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
@csrf_exempt
def user_login(request):
''' authentication '''
# send user to the login page
2020-01-28 20:13:28 +00:00
# TODO: login with localname or email
2020-01-25 23:25:19 +00:00
if request.method == 'GET':
return TemplateResponse(request, 'login.html')
2020-01-25 06:32:41 +00:00
2020-01-25 23:25:19 +00:00
# authenticate user
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect(request.GET.get('next', '/'))
return TemplateResponse(request, 'login.html')
2020-01-28 02:47:54 +00:00
2020-01-25 23:25:19 +00:00
@csrf_exempt
@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
@login_required
def user_profile(request, username):
''' profile page for a user '''
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-01-26 20:14:27 +00:00
books = models.Book.objects.filter(shelves__user=user)
data = {
'user': user,
'books': books,
'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()
data = {
'user': user,
}
return TemplateResponse(request, 'edit_user.html', data)
@csrf_exempt
@login_required
def upload_avatar(request):
''' les get fancy with images '''
if not request.method == 'POST':
return redirect('/')
request.user.avatar = request.FILES['avatar']
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 '''
book = openlibrary.get_or_create_book('/book/' + book_identifier)
reviews = models.Review.objects.filter(
Q(work=book.works.first()) | Q(book=book)
)
rating = reviews.aggregate(Avg('rating'))
data = {
'book': book,
'reviews': reviews,
'rating': rating['rating__avg'],
}
return TemplateResponse(request, 'book.html', data)
2020-01-27 01:55:02 +00:00
@csrf_exempt
@login_required
def shelve(request, shelf_id, book_id):
''' put a book on a user's shelf '''
book = models.Book.objects.get(id=book_id)
shelf = models.Shelf.objects.get(identifier=shelf_id)
2020-01-28 02:47:54 +00:00
api.handle_shelve(request.user, book, 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
@csrf_exempt
@login_required
def review(request):
''' create a book review note '''
2020-01-28 19:45:27 +00:00
# TODO: error handling
2020-01-28 02:47:54 +00:00
book_identifier = request.POST.get('book')
book = openlibrary.get_or_create_book(book_identifier)
2020-01-28 19:45:27 +00:00
# TODO: validation, htmlification
2020-01-28 02:47:54 +00:00
name = request.POST.get('name')
content = request.POST.get('content')
rating = request.POST.get('rating')
2020-01-28 19:45:27 +00:00
2020-01-28 02:47:54 +00:00
api.handle_review(request.user, book, name, content, rating)
return redirect(book_identifier)
2020-01-27 01:55:02 +00:00
2020-01-26 20:14:27 +00:00
@csrf_exempt
@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-28 02:47:54 +00:00
api.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
@csrf_exempt
@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
@csrf_exempt
@login_required
def search(request):
''' that search bar up top '''
query = request.GET.get('q')
if re.match(r'\w+@\w+.\w+', query):
results = [api.handle_account_search(query)]
else:
2020-01-28 19:45:27 +00:00
# TODO: book search
2020-01-28 03:57:17 +00:00
results = []
return TemplateResponse(request, 'results.html', {'results': results})