From fbd8e22b9797cb1ea2880e76b9d3c479d5678666 Mon Sep 17 00:00:00 2001 From: Christof Dorner Date: Sat, 10 Dec 2022 16:55:45 +0100 Subject: [PATCH] Add new user tab listing only reviews and comments --- bookwyrm/templates/user/layout.html | 4 ++ bookwyrm/templates/user/reviews_comments.html | 30 +++++++++++++ bookwyrm/tests/views/test_user.py | 16 +++++++ bookwyrm/urls.py | 5 +++ bookwyrm/views/__init__.py | 8 +++- bookwyrm/views/user.py | 44 +++++++++++++++++++ 6 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 bookwyrm/templates/user/reviews_comments.html diff --git a/bookwyrm/templates/user/layout.html b/bookwyrm/templates/user/layout.html index f0254e187..ecf36845c 100755 --- a/bookwyrm/templates/user/layout.html +++ b/bookwyrm/templates/user/layout.html @@ -66,6 +66,10 @@ {% trans "Activity" %} + {% url 'user-reviews-comments' user|username as url %} + + {% trans "Reviews and Comments" %} + {% if is_self or user.goal.exists %} {% now 'Y' as year %} {% url 'user-goal' user|username year as url %} diff --git a/bookwyrm/templates/user/reviews_comments.html b/bookwyrm/templates/user/reviews_comments.html new file mode 100644 index 000000000..f5c5c9265 --- /dev/null +++ b/bookwyrm/templates/user/reviews_comments.html @@ -0,0 +1,30 @@ +{% extends 'user/layout.html' %} +{% load i18n %} +{% load utilities %} + +{% block title %}{{ user.display_name }}{% endblock %} + +{% block header %} +
+
+

{% trans "Reviews and Comments" %}

+
+
+{% endblock %} + +{% block panel %} +
+ {% for activity in activities %} +
+ {% include 'snippets/status/status.html' with status=activity %} +
+ {% endfor %} + {% if not activities %} +
+

{% trans "No reviews or comments yet!" %}

+
+ {% endif %} + + {% include 'snippets/pagination.html' with page=activities path=path %} +
+{% endblock %} diff --git a/bookwyrm/tests/views/test_user.py b/bookwyrm/tests/views/test_user.py index d338d97be..903f2321b 100644 --- a/bookwyrm/tests/views/test_user.py +++ b/bookwyrm/tests/views/test_user.py @@ -233,3 +233,19 @@ class UserViews(TestCase): result = views.user_redirect(request, "mouse") self.assertEqual(result.status_code, 302) + + def test_reviews_comments_page(self): + """there are so many views, this just makes sure it LOADS""" + view = views.UserReviewsComments.as_view() + request = self.factory.get("") + request.user = self.local_user + result = view(request, "mouse") + self.assertIsInstance(result, TemplateResponse) + validate_html(result.render()) + self.assertEqual(result.status_code, 200) + + request.user = self.anonymous_user + result = view(request, "mouse") + self.assertIsInstance(result, TemplateResponse) + validate_html(result.render()) + self.assertEqual(result.status_code, 200) diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index 90d0d0edc..e2bedd8f2 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -410,6 +410,11 @@ urlpatterns = [ name="user-relationships", ), re_path(r"^hide-suggestions/?$", views.hide_suggestions, name="hide-suggestions"), + re_path( + rf"{USER_PATH}/reviews-comments", + views.UserReviewsComments.as_view(), + name="user-reviews-comments", + ), # groups re_path(rf"{USER_PATH}/groups/?$", views.UserGroups.as_view(), name="user-groups"), re_path( diff --git a/bookwyrm/views/__init__.py b/bookwyrm/views/__init__.py index bc70490c5..ca09d2a02 100644 --- a/bookwyrm/views/__init__.py +++ b/bookwyrm/views/__init__.py @@ -137,7 +137,13 @@ from .setup import InstanceConfig, CreateAdmin from .status import CreateStatus, EditStatus, DeleteStatus, update_progress from .status import edit_readthrough from .updates import get_notification_count, get_unread_status_string -from .user import User, hide_suggestions, user_redirect, toggle_guided_tour +from .user import ( + User, + UserReviewsComments, + hide_suggestions, + user_redirect, + toggle_guided_tour, +) from .relationships import Relationships from .wellknown import * from .annual_summary import ( diff --git a/bookwyrm/views/user.py b/bookwyrm/views/user.py index a4ce9d259..b027ffa56 100644 --- a/bookwyrm/views/user.py +++ b/bookwyrm/views/user.py @@ -1,6 +1,7 @@ """ The user profile """ from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator +from django.db.models import Q from django.http import Http404 from django.shortcuts import redirect from django.template.response import TemplateResponse @@ -100,6 +101,49 @@ class User(View): return TemplateResponse(request, "user/user.html", data) +class UserReviewsComments(View): + """user's activity filtered by reviews and comments""" + + def get(self, request, username): + """user's activity filtered by reviews and comments""" + user = get_user_from_username(request.user, username) + is_self = request.user.id == user.id + + activities = ( + models.Status.privacy_filter( + request.user, + ) + .filter( + Q(review__isnull=False) | Q(comment__isnull=False), + user=user, + ) + .exclude( + privacy="direct", + ) + .select_related( + "user", + "reply_parent", + "review__book", + "comment__book", + "quotation__book", + ) + .prefetch_related( + "mention_books", + "mention_users", + "attachments", + ) + ) + + paginated = Paginator(activities, PAGE_LENGTH) + + data = { + "user": user, + "is_self": is_self, + "activities": paginated.get_page(request.GET.get("page", 1)), + } + return TemplateResponse(request, "user/reviews_comments.html", data) + + @require_POST @login_required def hide_suggestions(request):