moviewyrm/bookwyrm/views/user.py

121 lines
3.9 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" non-interactive pages """
2021-01-12 20:05:30 +00:00
from django.core.paginator import Paginator
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.views import View
2021-06-14 16:57:51 +00:00
from bookwyrm import models
2021-01-12 20:05:30 +00:00
from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.settings import PAGE_LENGTH
2021-03-23 02:17:46 +00:00
from .helpers import get_user_from_username, is_api_request
2021-04-30 16:26:02 +00:00
from .helpers import privacy_filter
2021-01-12 20:05:30 +00:00
2021-06-14 16:57:51 +00:00
# pylint: disable=no-self-use
2021-01-12 20:05:30 +00:00
class User(View):
2021-04-26 16:15:42 +00:00
"""user profile page"""
2021-03-08 16:49:10 +00:00
2021-01-12 20:05:30 +00:00
def get(self, request, username):
2021-04-26 16:15:42 +00:00
"""profile page for a user"""
user = get_user_from_username(request.user, username)
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
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,
2021-04-20 19:31:45 +00:00
"books": user_shelf.books.all()[:3],
2021-03-08 16:49:10 +00:00
"size": user_shelf.books.count(),
}
)
2021-01-12 20:05:30 +00:00
if len(shelf_preview) > 2:
break
# user's posts
activities = (
privacy_filter(
request.user,
user.status_set.select_subclasses(),
)
.select_related("reply_parent")
.prefetch_related("mention_books", "mention_users")
2021-01-12 20:05:30 +00:00
)
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()
if goal and not goal.visible_to_user(request.user):
2021-01-16 20:39:51 +00:00
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(),
2021-04-19 22:01:20 +00:00
"activities": paginated.get_page(request.GET.get("page", 1)),
2021-03-08 16:49:10 +00:00
"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-04-26 16:15:42 +00:00
"""list of followers view"""
2021-03-08 16:49:10 +00:00
2021-01-12 20:05:30 +00:00
def get(self, request, username):
2021-04-26 16:15:42 +00:00
"""list of followers"""
user = get_user_from_username(request.user, username)
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
2021-08-03 00:48:29 +00:00
paginated = Paginator(
user.followers.order_by("-created_date").all(), PAGE_LENGTH
)
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,
"follow_list": paginated.get_page(request.GET.get("page")),
2021-01-12 20:05:30 +00:00
}
return TemplateResponse(request, "user/relationships/followers.html", data)
2021-03-08 16:49:10 +00:00
2021-01-12 20:05:30 +00:00
class Following(View):
2021-04-26 16:15:42 +00:00
"""list of following view"""
2021-03-08 16:49:10 +00:00
2021-01-12 20:05:30 +00:00
def get(self, request, username):
2021-04-26 16:15:42 +00:00
"""list of followers"""
user = get_user_from_username(request.user, username)
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
2021-08-03 00:48:29 +00:00
paginated = Paginator(
user.following.order_by("-created_date").all(), PAGE_LENGTH
)
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,
"follow_list": paginated.get_page(request.GET.get("page")),
2021-01-12 20:05:30 +00:00
}
return TemplateResponse(request, "user/relationships/following.html", data)