2021-03-08 16:49:10 +00:00
|
|
|
""" non-interactive pages """
|
2021-01-12 20:05:30 +00:00
|
|
|
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
|
2021-01-16 20:39:51 +00:00
|
|
|
from django.utils import timezone
|
2021-01-12 20:05:30 +00:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views import View
|
|
|
|
|
|
|
|
from bookwyrm import forms, models
|
|
|
|
from bookwyrm.activitypub import ActivitypubResponse
|
|
|
|
from bookwyrm.settings import PAGE_LENGTH
|
|
|
|
from .helpers import get_activity_feed, get_user_from_username, is_api_request
|
2021-01-26 16:31:55 +00:00
|
|
|
from .helpers import is_blocked, object_visible_to_user
|
2021-01-12 20:05:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable= no-self-use
|
|
|
|
class User(View):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" user profile page """
|
|
|
|
|
2021-01-12 20:05:30 +00:00
|
|
|
def get(self, request, username):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" profile page for a user """
|
2021-01-12 20:05:30 +00:00
|
|
|
try:
|
2021-02-23 20:41:37 +00:00
|
|
|
user = get_user_from_username(request.user, username)
|
2021-01-12 20:05:30 +00:00
|
|
|
except models.User.DoesNotExist:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
2021-01-26 16:07:38 +00:00
|
|
|
# make sure we're not blocked
|
2021-01-26 16:31:55 +00:00
|
|
|
if is_blocked(request.user, user):
|
|
|
|
return HttpResponseNotFound()
|
2021-01-26 16:07:38 +00:00
|
|
|
|
2021-01-12 20:05:30 +00:00
|
|
|
if is_api_request(request):
|
|
|
|
# we have a json request
|
|
|
|
return ActivitypubResponse(user.to_activity())
|
|
|
|
# otherwise we're at a UI view
|
|
|
|
|
|
|
|
try:
|
2021-03-08 16:49:10 +00:00
|
|
|
page = int(request.GET.get("page", 1))
|
2021-01-12 20:05:30 +00:00
|
|
|
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:
|
2021-03-08 16:49:10 +00:00
|
|
|
shelves = shelves.filter(privacy__in=["public", "followers"])
|
2021-01-12 20:05:30 +00:00
|
|
|
else:
|
2021-03-08 16:49:10 +00:00
|
|
|
shelves = shelves.filter(privacy="public")
|
2021-01-12 20:05:30 +00:00
|
|
|
|
|
|
|
for user_shelf in shelves.all():
|
|
|
|
if not user_shelf.books.count():
|
|
|
|
continue
|
2021-03-08 16:49:10 +00:00
|
|
|
shelf_preview.append(
|
|
|
|
{
|
|
|
|
"name": user_shelf.name,
|
|
|
|
"local_path": user_shelf.local_path,
|
|
|
|
"books": user_shelf.books.all()[:3],
|
|
|
|
"size": user_shelf.books.count(),
|
|
|
|
}
|
|
|
|
)
|
2021-01-12 20:05:30 +00:00
|
|
|
if len(shelf_preview) > 2:
|
|
|
|
break
|
|
|
|
|
|
|
|
# user's posts
|
|
|
|
activities = get_activity_feed(
|
|
|
|
request.user,
|
2021-02-24 19:59:21 +00:00
|
|
|
queryset=user.status_set.select_subclasses(),
|
2021-01-12 20:05:30 +00:00
|
|
|
)
|
|
|
|
paginated = Paginator(activities, PAGE_LENGTH)
|
2021-01-16 20:39:51 +00:00
|
|
|
goal = models.AnnualGoal.objects.filter(
|
2021-03-08 16:49:10 +00:00
|
|
|
user=user, year=timezone.now().year
|
|
|
|
).first()
|
2021-01-16 20:39:51 +00:00
|
|
|
if not object_visible_to_user(request.user, goal):
|
|
|
|
goal = None
|
2021-01-12 20:05:30 +00:00
|
|
|
data = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"user": user,
|
|
|
|
"is_self": is_self,
|
|
|
|
"shelves": shelf_preview,
|
|
|
|
"shelf_count": shelves.count(),
|
|
|
|
"activities": paginated.page(page),
|
|
|
|
"goal": goal,
|
2021-01-12 20:05:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
return TemplateResponse(request, "user/user.html", data)
|
|
|
|
|
2021-01-12 20:05:30 +00:00
|
|
|
|
|
|
|
class Followers(View):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" list of followers view """
|
|
|
|
|
2021-01-12 20:05:30 +00:00
|
|
|
def get(self, request, username):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" list of followers """
|
2021-01-12 20:05:30 +00:00
|
|
|
try:
|
2021-02-23 20:41:37 +00:00
|
|
|
user = get_user_from_username(request.user, username)
|
2021-01-12 20:05:30 +00:00
|
|
|
except models.User.DoesNotExist:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
2021-01-26 16:31:55 +00:00
|
|
|
# make sure we're not blocked
|
|
|
|
if is_blocked(request.user, user):
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
2021-01-12 20:05:30 +00:00
|
|
|
if is_api_request(request):
|
2021-03-08 16:49:10 +00:00
|
|
|
return ActivitypubResponse(user.to_followers_activity(**request.GET))
|
2021-01-12 20:05:30 +00:00
|
|
|
|
|
|
|
data = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"user": user,
|
|
|
|
"is_self": request.user.id == user.id,
|
|
|
|
"followers": user.followers.all(),
|
2021-01-12 20:05:30 +00:00
|
|
|
}
|
2021-03-08 16:49:10 +00:00
|
|
|
return TemplateResponse(request, "user/followers.html", data)
|
|
|
|
|
2021-01-12 20:05:30 +00:00
|
|
|
|
|
|
|
class Following(View):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" list of following view """
|
|
|
|
|
2021-01-12 20:05:30 +00:00
|
|
|
def get(self, request, username):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" list of followers """
|
2021-01-12 20:05:30 +00:00
|
|
|
try:
|
2021-02-23 20:41:37 +00:00
|
|
|
user = get_user_from_username(request.user, username)
|
2021-01-12 20:05:30 +00:00
|
|
|
except models.User.DoesNotExist:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
2021-01-26 16:31:55 +00:00
|
|
|
# make sure we're not blocked
|
|
|
|
if is_blocked(request.user, user):
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
2021-01-12 20:05:30 +00:00
|
|
|
if is_api_request(request):
|
2021-03-08 16:49:10 +00:00
|
|
|
return ActivitypubResponse(user.to_following_activity(**request.GET))
|
2021-01-12 20:05:30 +00:00
|
|
|
|
|
|
|
data = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"user": user,
|
|
|
|
"is_self": request.user.id == user.id,
|
|
|
|
"following": user.following.all(),
|
2021-01-12 20:05:30 +00:00
|
|
|
}
|
2021-03-08 16:49:10 +00:00
|
|
|
return TemplateResponse(request, "user/following.html", data)
|
2021-01-12 20:05:30 +00:00
|
|
|
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
2021-01-12 20:05:30 +00:00
|
|
|
class EditUser(View):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" edit user view """
|
|
|
|
|
2021-01-12 20:05:30 +00:00
|
|
|
def get(self, request):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" edit profile page for a user """
|
2021-01-12 20:05:30 +00:00
|
|
|
data = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"form": forms.EditUserForm(instance=request.user),
|
|
|
|
"user": request.user,
|
2021-01-12 20:05:30 +00:00
|
|
|
}
|
2021-03-08 16:49:10 +00:00
|
|
|
return TemplateResponse(request, "preferences/edit_user.html", data)
|
2021-01-12 20:05:30 +00:00
|
|
|
|
|
|
|
def post(self, request):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" les get fancy with images """
|
|
|
|
form = forms.EditUserForm(request.POST, request.FILES, instance=request.user)
|
2021-01-12 20:05:30 +00:00
|
|
|
if not form.is_valid():
|
2021-03-08 16:49:10 +00:00
|
|
|
data = {"form": form, "user": request.user}
|
|
|
|
return TemplateResponse(request, "preferences/edit_user.html", data)
|
2021-01-12 20:05:30 +00:00
|
|
|
|
|
|
|
user = form.save(commit=False)
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
if "avatar" in form.files:
|
2021-01-12 20:05:30 +00:00
|
|
|
# crop and resize avatar upload
|
2021-03-08 16:49:10 +00:00
|
|
|
image = Image.open(form.files["avatar"])
|
2021-01-26 16:03:16 +00:00
|
|
|
image = crop_avatar(image)
|
2021-01-12 20:05:30 +00:00
|
|
|
|
|
|
|
# set the name to a hash
|
2021-03-08 16:49:10 +00:00
|
|
|
extension = form.files["avatar"].name.split(".")[-1]
|
|
|
|
filename = "%s.%s" % (uuid4(), extension)
|
2021-01-26 16:03:16 +00:00
|
|
|
user.avatar.save(filename, image)
|
2021-01-12 20:05:30 +00:00
|
|
|
user.save()
|
|
|
|
|
2021-01-18 19:19:30 +00:00
|
|
|
return redirect(user.local_path)
|
2021-01-26 16:03:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def crop_avatar(image):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" reduce the size and make an avatar square """
|
2021-01-26 16:03:16 +00:00
|
|
|
target_size = 120
|
|
|
|
width, height = image.size
|
2021-03-08 16:49:10 +00:00
|
|
|
thumbnail_scale = (
|
|
|
|
height / (width / target_size)
|
|
|
|
if height > width
|
2021-01-26 16:03:16 +00:00
|
|
|
else width / (height / target_size)
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2021-01-26 16:03:16 +00:00
|
|
|
image.thumbnail([thumbnail_scale, thumbnail_scale])
|
|
|
|
width, height = image.size
|
|
|
|
|
|
|
|
width_diff = width - target_size
|
|
|
|
height_diff = height - target_size
|
2021-03-08 16:49:10 +00:00
|
|
|
cropped = image.crop(
|
|
|
|
(
|
|
|
|
int(width_diff / 2),
|
|
|
|
int(height_diff / 2),
|
|
|
|
int(width - (width_diff / 2)),
|
|
|
|
int(height - (height_diff / 2)),
|
|
|
|
)
|
|
|
|
)
|
2021-01-26 16:03:16 +00:00
|
|
|
output = BytesIO()
|
|
|
|
cropped.save(output, format=image.format)
|
|
|
|
return ContentFile(output.getvalue())
|