From 1fbd6537b7ed4ed01da0a9d3f4fd605f4eb30524 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 11 May 2021 13:28:52 -0700 Subject: [PATCH 01/11] Remove unnecessary tags from import page --- bookwyrm/templates/import_status.html | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/bookwyrm/templates/import_status.html b/bookwyrm/templates/import_status.html index e06392a8..b1145611 100644 --- a/bookwyrm/templates/import_status.html +++ b/bookwyrm/templates/import_status.html @@ -1,6 +1,5 @@ {% extends 'layout.html' %} {% load i18n %} -{% load bookwyrm_tags %} {% load humanize %} {% block title %}{% trans "Import Status" %}{% endblock %} @@ -54,8 +53,8 @@

{{ item.fail_reason }}. @@ -90,8 +89,8 @@

  • Line {{ item.index }}: - {{ item.data|dict_key:'Title' }} by - {{ item.data|dict_key:'Author' }} + {{ item.data.Title }} by + {{ item.data.Author }}

    {{ item.fail_reason }}. @@ -130,10 +129,10 @@ {% endif %} - {{ item.data|dict_key:'Title' }} + {{ item.data.Title }} - {{ item.data|dict_key:'Author' }} + {{ item.data.Author }} {% if item.book %} From 63172ecf0015ee0f8c9aef395fd3793d1af6e9ed Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 11 May 2021 13:54:38 -0700 Subject: [PATCH 02/11] Fixes ratings on shelf pages --- bookwyrm/templates/user/shelf/shelf.html | 8 +++++--- bookwyrm/views/shelf.py | 20 ++++++++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/bookwyrm/templates/user/shelf/shelf.html b/bookwyrm/templates/user/shelf/shelf.html index 639ab502..86610629 100644 --- a/bookwyrm/templates/user/shelf/shelf.html +++ b/bookwyrm/templates/user/shelf/shelf.html @@ -79,7 +79,9 @@ {% trans "Shelved" %} {% trans "Started" %} {% trans "Finished" %} - {% if ratings %}{% trans "Rating" %}{% endif %} + {% if request.user.is_authenticated %} + {% trans "Rating" %} + {% endif %} {% if shelf.user == request.user %} {% endif %} @@ -108,9 +110,9 @@ {{ read_through.finish_date | naturalday |default_if_none:""}} - {% if ratings %} + {% if request.user.is_authenticated %} - {% include 'snippets/stars.html' with rating=ratings|dict_key:book.id %} + {% include 'snippets/stars.html' with rating=book.rating %} {% endif %} {% if shelf.user == request.user %} diff --git a/bookwyrm/views/shelf.py b/bookwyrm/views/shelf.py index 5312ac21..758e290e 100644 --- a/bookwyrm/views/shelf.py +++ b/bookwyrm/views/shelf.py @@ -2,6 +2,7 @@ from collections import namedtuple from django.db import IntegrityError +from django.db.models import Count, OuterRef, Subquery, F, Q from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator from django.http import HttpResponseBadRequest, HttpResponseNotFound @@ -37,30 +38,41 @@ class Shelf(View): return HttpResponseNotFound() if not shelf.visible_to_user(request.user): return HttpResponseNotFound() + books = shelf.books # this is a constructed "all books" view, with a fake "shelf" obj else: FakeShelf = namedtuple( "Shelf", ("identifier", "name", "user", "books", "privacy") ) books = models.Edition.objects.filter( + # privacy is ensured because the shelves are already filtered above shelfbook__shelf__in=shelves.all() ).distinct() shelf = FakeShelf("all", _("All books"), user, books, "public") - is_self = request.user == user - if is_api_request(request): return ActivitypubResponse(shelf.to_activity(**request.GET)) + reviews = privacy_filter( + request.user, + models.Review.objects.filter( + user=user, + rating__isnull=False, + book__id=OuterRef("id"), + ), + ).order_by("-published_date") + + books = books.annotate(rating=Subquery(reviews.values("rating")[:1])) + paginated = Paginator( - shelf.books.order_by("-updated_date"), + books.order_by("-updated_date"), PAGE_LENGTH, ) page = paginated.get_page(request.GET.get("page")) data = { "user": user, - "is_self": is_self, + "is_self": request.user == user, "shelves": shelves.all(), "shelf": shelf, "books": page, From 04cc2fb3f31ae557328dfaf660fc41a722ad746d Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 11 May 2021 13:55:46 -0700 Subject: [PATCH 03/11] Removes unused dict_key filter --- bookwyrm/templatetags/bookwyrm_tags.py | 6 ------ bookwyrm/tests/test_templatetags.py | 7 ------- 2 files changed, 13 deletions(-) diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index eda3d102..caab30a8 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -11,12 +11,6 @@ from bookwyrm.views.status import to_markdown register = template.Library() -@register.filter(name="dict_key") -def dict_key(d, k): - """Returns the given key from a dictionary.""" - return d.get(k) or 0 - - @register.filter(name="rating") def get_rating(book, user): """get the overall rating of a book""" diff --git a/bookwyrm/tests/test_templatetags.py b/bookwyrm/tests/test_templatetags.py index a92e887a..1c05e992 100644 --- a/bookwyrm/tests/test_templatetags.py +++ b/bookwyrm/tests/test_templatetags.py @@ -2,7 +2,6 @@ import re from unittest.mock import patch -from dateutil.relativedelta import relativedelta from django.test import TestCase from django.utils import timezone @@ -33,12 +32,6 @@ class TemplateTags(TestCase): ) self.book = models.Edition.objects.create(title="Test Book") - def test_dict_key(self, _): - """just getting a value out of a dict""" - test_dict = {"a": 1, "b": 3} - self.assertEqual(bookwyrm_tags.dict_key(test_dict, "a"), 1) - self.assertEqual(bookwyrm_tags.dict_key(test_dict, "c"), 0) - def test_get_user_rating(self, _): """get a user's most recent rating of a book""" with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): From 3de8ae00aed608c656e888875a570ef0b6cea4ab Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 11 May 2021 14:00:23 -0700 Subject: [PATCH 04/11] Removes unused templatetag --- bookwyrm/templatetags/bookwyrm_tags.py | 6 ------ bookwyrm/tests/test_templatetags.py | 13 ------------- 2 files changed, 19 deletions(-) diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index caab30a8..fa2998e6 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -43,12 +43,6 @@ def get_user_identifier(user): return user.localname if user.localname else user.username -@register.filter(name="notification_count") -def get_notification_count(user): - """how many UNREAD notifications are there""" - return user.notification_set.filter(read=False).count() - - @register.filter(name="replies") def get_replies(status): """get all direct replies to a status""" diff --git a/bookwyrm/tests/test_templatetags.py b/bookwyrm/tests/test_templatetags.py index 1c05e992..df4df671 100644 --- a/bookwyrm/tests/test_templatetags.py +++ b/bookwyrm/tests/test_templatetags.py @@ -53,19 +53,6 @@ class TemplateTags(TestCase): bookwyrm_tags.get_user_identifier(self.remote_user), "rat@example.com" ) - def test_get_notification_count(self, _): - """just countin'""" - self.assertEqual(bookwyrm_tags.get_notification_count(self.user), 0) - - models.Notification.objects.create(user=self.user, notification_type="FAVORITE") - models.Notification.objects.create(user=self.user, notification_type="MENTION") - - models.Notification.objects.create( - user=self.remote_user, notification_type="FOLLOW" - ) - - self.assertEqual(bookwyrm_tags.get_notification_count(self.user), 2) - def test_get_replies(self, _): """direct replies to a status""" with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): From f5eb5f982ab1d32c5388cf5651876a36d181f8d2 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 11 May 2021 14:04:45 -0700 Subject: [PATCH 05/11] Removes filter for checking for follow requests --- .../snippets/follow_request_buttons.html | 3 +-- bookwyrm/templatetags/bookwyrm_tags.py | 13 ------------- bookwyrm/tests/test_templatetags.py | 18 ------------------ 3 files changed, 1 insertion(+), 33 deletions(-) diff --git a/bookwyrm/templates/snippets/follow_request_buttons.html b/bookwyrm/templates/snippets/follow_request_buttons.html index 42e69153..d9101802 100644 --- a/bookwyrm/templates/snippets/follow_request_buttons.html +++ b/bookwyrm/templates/snippets/follow_request_buttons.html @@ -1,6 +1,5 @@ {% load i18n %} -{% load bookwyrm_tags %} -{% if request.user|follow_request_exists:user %} +{% if request.user in user.follow_requests.all %}

    {% csrf_token %} diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index fa2998e6..81019fe5 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -79,19 +79,6 @@ def get_user_boosted(user, status): return user.id in status.boosters.all().values_list("user", flat=True) -@register.filter(name="follow_request_exists") -def follow_request_exists(user, requester): - """see if there is a pending follow request for a user""" - try: - models.UserFollowRequest.objects.filter( - user_subject=requester, - user_object=user, - ).get() - return True - except models.UserFollowRequest.DoesNotExist: - return False - - @register.filter(name="boosted_status") def get_boosted(boost): """load a boosted status. have to do this or it wont get foregin keys""" diff --git a/bookwyrm/tests/test_templatetags.py b/bookwyrm/tests/test_templatetags.py index df4df671..3e2ecf37 100644 --- a/bookwyrm/tests/test_templatetags.py +++ b/bookwyrm/tests/test_templatetags.py @@ -113,24 +113,6 @@ class TemplateTags(TestCase): models.Boost.objects.create(user=self.user, boosted_status=status) self.assertTrue(bookwyrm_tags.get_user_boosted(self.user, status)) - def test_follow_request_exists(self, _): - """does a user want to follow""" - self.assertFalse( - bookwyrm_tags.follow_request_exists(self.user, self.remote_user) - ) - - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): - models.UserFollowRequest.objects.create( - user_subject=self.user, user_object=self.remote_user - ) - - self.assertFalse( - bookwyrm_tags.follow_request_exists(self.user, self.remote_user) - ) - self.assertTrue( - bookwyrm_tags.follow_request_exists(self.remote_user, self.user) - ) - def test_get_boosted(self, _): """load a boosted status""" with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): From acd26012bea564850bda7cf9c1b1a67b7be592f1 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 11 May 2021 14:12:28 -0700 Subject: [PATCH 06/11] Removes more unused filters --- bookwyrm/templatetags/bookwyrm_tags.py | 31 ++------------------------ bookwyrm/tests/test_templatetags.py | 25 --------------------- 2 files changed, 2 insertions(+), 54 deletions(-) diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index 81019fe5..c992b9e5 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -81,11 +81,9 @@ def get_user_boosted(user, status): @register.filter(name="boosted_status") def get_boosted(boost): - """load a boosted status. have to do this or it wont get foregin keys""" + """load a boosted status. have to do this or it won't get foreign keys""" return ( - models.Status.objects.select_subclasses() - .filter(id=boost.boosted_status.id) - .get() + models.Status.objects.select_subclasses().get(id=boost.boosted_status.id) ) @@ -118,19 +116,6 @@ def get_mentions(status, user): ) -@register.filter(name="status_preview_name") -def get_status_preview_name(obj): - """text snippet with book context for a status""" - name = obj.__class__.__name__.lower() - if name == "review": - return "%s of %s" % (name, obj.book.title) - if name == "comment": - return "%s on %s" % (name, obj.book.title) - if name == "quotation": - return "%s from %s" % (name, obj.book.title) - return name - - @register.filter(name="next_shelf") def get_next_shelf(current_shelf): """shelf you'd use to update reading progress""" @@ -187,18 +172,6 @@ def latest_read_through(book, user): ) -@register.simple_tag(takes_context=False) -def active_read_through(book, user): - """the most recent read activity""" - return ( - models.ReadThrough.objects.filter( - user=user, book=book, finish_date__isnull=True - ) - .order_by("-start_date") - .first() - ) - - @register.simple_tag(takes_context=False) def comparison_bool(str1, str2): """idk why I need to write a tag for this, it reutrns a bool""" diff --git a/bookwyrm/tests/test_templatetags.py b/bookwyrm/tests/test_templatetags.py index 3e2ecf37..3b782fd5 100644 --- a/bookwyrm/tests/test_templatetags.py +++ b/bookwyrm/tests/test_templatetags.py @@ -157,31 +157,6 @@ class TemplateTags(TestCase): result = bookwyrm_tags.get_mentions(status, self.user) self.assertEqual(result, "@rat@example.com ") - def test_get_status_preview_name(self, _): - """status context string""" - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): - status = models.Status.objects.create(content="hi", user=self.user) - result = bookwyrm_tags.get_status_preview_name(status) - self.assertEqual(result, "status") - - status = models.Review.objects.create( - content="hi", user=self.user, book=self.book - ) - result = bookwyrm_tags.get_status_preview_name(status) - self.assertEqual(result, "review of Test Book") - - status = models.Comment.objects.create( - content="hi", user=self.user, book=self.book - ) - result = bookwyrm_tags.get_status_preview_name(status) - self.assertEqual(result, "comment on Test Book") - - status = models.Quotation.objects.create( - content="hi", user=self.user, book=self.book - ) - result = bookwyrm_tags.get_status_preview_name(status) - self.assertEqual(result, "quotation from Test Book") - def test_related_status(self, _): """gets the subclass model for a notification status""" with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): From eba6159f6868b5a1e19e6eb72758d9d54b9ce420 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 11 May 2021 14:14:22 -0700 Subject: [PATCH 07/11] Moves status interaction filters into their own module --- bookwyrm/templatetags/bookwyrm_tags.py | 16 ---------------- bookwyrm/templatetags/interaction.py | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 16 deletions(-) create mode 100644 bookwyrm/templatetags/interaction.py diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index c992b9e5..719dc9ad 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -63,22 +63,6 @@ def get_parent(status): ) -@register.filter(name="liked") -def get_user_liked(user, status): - """did the given user fav a status?""" - try: - models.Favorite.objects.get(user=user, status=status) - return True - except models.Favorite.DoesNotExist: - return False - - -@register.filter(name="boosted") -def get_user_boosted(user, status): - """did the given user fav a status?""" - return user.id in status.boosters.all().values_list("user", flat=True) - - @register.filter(name="boosted_status") def get_boosted(boost): """load a boosted status. have to do this or it won't get foreign keys""" diff --git a/bookwyrm/templatetags/interaction.py b/bookwyrm/templatetags/interaction.py new file mode 100644 index 00000000..6e8d0549 --- /dev/null +++ b/bookwyrm/templatetags/interaction.py @@ -0,0 +1,22 @@ +""" template filters for status interaction buttons """ +from django import template +from bookwyrm import models + + +register = template.Library() + + +@register.filter(name="liked") +def get_user_liked(user, status): + """did the given user fav a status?""" + try: + models.Favorite.objects.get(user=user, status=status) + return True + except models.Favorite.DoesNotExist: + return False + + +@register.filter(name="boosted") +def get_user_boosted(user, status): + """did the given user fav a status?""" + return user.id in status.boosters.all().values_list("user", flat=True) From 58da17d69411806249446bd4aff734920fb746ac Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 11 May 2021 14:41:28 -0700 Subject: [PATCH 08/11] Use smaller templatetag modules --- bookwyrm/templates/components/dropdown.html | 2 +- bookwyrm/templates/feed/suggested_users.html | 2 +- bookwyrm/templates/layout.html | 2 +- bookwyrm/templates/snippets/boost_button.html | 3 +- .../templates/snippets/create_status.html | 2 +- .../snippets/create_status_form.html | 2 + bookwyrm/templates/snippets/fav_button.html | 4 +- .../templates/snippets/privacy_select.html | 2 +- .../templates/snippets/report_button.html | 3 +- .../snippets/shelve_button/shelve_button.html | 1 + .../shelve_button/shelve_button_options.html | 2 + .../templates/snippets/status/layout.html | 3 +- .../snippets/status/status_options.html | 2 +- bookwyrm/templates/snippets/trimmed_text.html | 1 + bookwyrm/templatetags/bookwyrm_tags.py | 41 +------------------ bookwyrm/templatetags/layout.py | 12 ++++++ bookwyrm/templatetags/utilities.py | 35 ++++++++++++++++ 17 files changed, 69 insertions(+), 50 deletions(-) create mode 100644 bookwyrm/templatetags/layout.py create mode 100644 bookwyrm/templatetags/utilities.py diff --git a/bookwyrm/templates/components/dropdown.html b/bookwyrm/templates/components/dropdown.html index 96dce823..35caa55b 100644 --- a/bookwyrm/templates/components/dropdown.html +++ b/bookwyrm/templates/components/dropdown.html @@ -1,5 +1,5 @@ {% spaceless %} -{% load bookwyrm_tags %} +{% load utilities %} {% with 0|uuid as uuid %}
    {% for user in suggested_users %} diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html index bc654401..becf7781 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -1,4 +1,4 @@ -{% load bookwyrm_tags %} +{% load layout %} {% load i18n %} diff --git a/bookwyrm/templates/snippets/boost_button.html b/bookwyrm/templates/snippets/boost_button.html index 3a01fc82..5b84ea44 100644 --- a/bookwyrm/templates/snippets/boost_button.html +++ b/bookwyrm/templates/snippets/boost_button.html @@ -1,4 +1,5 @@ -{% load bookwyrm_tags %} +{% load interaction %} +{% load utilities %} {% load i18n %} {% with status.id|uuid as uuid %} diff --git a/bookwyrm/templates/snippets/create_status.html b/bookwyrm/templates/snippets/create_status.html index 7dee4b70..b1a8e846 100644 --- a/bookwyrm/templates/snippets/create_status.html +++ b/bookwyrm/templates/snippets/create_status.html @@ -1,6 +1,6 @@ {% load humanize %} {% load i18n %} -{% load bookwyrm_tags %} +{% load utilities %} {% with status_type=request.GET.status_type %}
    diff --git a/bookwyrm/templates/snippets/create_status_form.html b/bookwyrm/templates/snippets/create_status_form.html index f6021622..ca9cb5b4 100644 --- a/bookwyrm/templates/snippets/create_status_form.html +++ b/bookwyrm/templates/snippets/create_status_form.html @@ -1,4 +1,6 @@ {% load bookwyrm_tags %} +{% load utilities %} + {% load i18n %} {% csrf_token %} diff --git a/bookwyrm/templates/snippets/fav_button.html b/bookwyrm/templates/snippets/fav_button.html index cd22822a..40fa2d25 100644 --- a/bookwyrm/templates/snippets/fav_button.html +++ b/bookwyrm/templates/snippets/fav_button.html @@ -1,5 +1,7 @@ -{% load bookwyrm_tags %} +{% load interaction %} +{% load utilities %} {% load i18n %} + {% with status.id|uuid as uuid %} {% csrf_token %} diff --git a/bookwyrm/templates/snippets/privacy_select.html b/bookwyrm/templates/snippets/privacy_select.html index e8008509..f6959de5 100644 --- a/bookwyrm/templates/snippets/privacy_select.html +++ b/bookwyrm/templates/snippets/privacy_select.html @@ -1,5 +1,5 @@ {% load i18n %} -{% load bookwyrm_tags %} +{% load utilities %}
    {% with 0|uuid as uuid %} {% if not no_label %} diff --git a/bookwyrm/templates/snippets/report_button.html b/bookwyrm/templates/snippets/report_button.html index 2fa0a3f3..19414c16 100644 --- a/bookwyrm/templates/snippets/report_button.html +++ b/bookwyrm/templates/snippets/report_button.html @@ -1,5 +1,6 @@ {% load i18n %} -{% load bookwyrm_tags %} +{% load utilities %} + {% with 0|uuid as report_uuid %} {% trans "Report" as button_text %} diff --git a/bookwyrm/templates/snippets/shelve_button/shelve_button.html b/bookwyrm/templates/snippets/shelve_button/shelve_button.html index dc07fb64..754907c2 100644 --- a/bookwyrm/templates/snippets/shelve_button/shelve_button.html +++ b/bookwyrm/templates/snippets/shelve_button/shelve_button.html @@ -1,4 +1,5 @@ {% load bookwyrm_tags %} +{% load utilities %} {% if request.user.is_authenticated %} {% with book.id|uuid as uuid %} diff --git a/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html b/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html index 1eaa2463..1ce10e7c 100644 --- a/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html +++ b/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html @@ -1,5 +1,7 @@ {% load bookwyrm_tags %} +{% load utilities %} {% load i18n %} + {% for shelf in shelves %} {% comparison_bool shelf.identifier active_shelf.shelf.identifier as is_current %} {% if dropdown %}
    diff --git a/bookwyrm/templates/book/editions.html b/bookwyrm/templates/book/editions.html index 775b05c8..17d5474c 100644 --- a/bookwyrm/templates/book/editions.html +++ b/bookwyrm/templates/book/editions.html @@ -1,6 +1,5 @@ {% extends 'layout.html' %} {% load i18n %} -{% load bookwyrm_tags %} {% block title %}{% blocktrans with book_title=work.title %}Editions of {{ book_title }}{% endblocktrans %}{% endblock %} diff --git a/bookwyrm/templates/compose.html b/bookwyrm/templates/compose.html index 0f609cf7..1b6aa735 100644 --- a/bookwyrm/templates/compose.html +++ b/bookwyrm/templates/compose.html @@ -1,6 +1,6 @@ {% extends 'layout.html' %} {% load i18n %} -{% load bookwyrm_tags %} +{% load utilities %} {% block title %}{% trans "Compose status" %}{% endblock %} {% block content %} diff --git a/bookwyrm/templates/directory/user_card.html b/bookwyrm/templates/directory/user_card.html index 8e7538c8..c52c1f7a 100644 --- a/bookwyrm/templates/directory/user_card.html +++ b/bookwyrm/templates/directory/user_card.html @@ -1,5 +1,6 @@ {% load i18n %} -{% load bookwyrm_tags %} +{% load utilities %} +{% load markdown %} {% load humanize %}
    @@ -19,7 +20,7 @@
    {% if user.summary %} - {{ user.summary | to_markdown | safe | truncatechars_html:40 }} + {{ user.summary|to_markdown|safe|truncatechars_html:40 }} {% else %} {% endif %}
    diff --git a/bookwyrm/templates/discover/landing_layout.html b/bookwyrm/templates/discover/landing_layout.html index 8e507531..946482cb 100644 --- a/bookwyrm/templates/discover/landing_layout.html +++ b/bookwyrm/templates/discover/landing_layout.html @@ -1,6 +1,6 @@ {% extends 'layout.html' %} {% load i18n %} -{% load bookwyrm_tags %} +{% load markdown %} {% block title %}{% trans "Welcome" %}{% endblock %} @@ -49,7 +49,7 @@ {% else %}

    {% trans "This instance is closed" %}

    -

    {{ site.registration_closed_text | safe}}

    +

    {{ site.registration_closed_text|safe}}

    {% if site.allow_invite_requests %} {% if request_received %} @@ -64,7 +64,7 @@ {% for error in request_form.email.errors %} -

    {{ error | escape }}

    +

    {{ error|escape }}

    {% endfor %}
    @@ -80,7 +80,7 @@ {% include 'user/user_preview.html' with user=request.user %} {% if request.user.summary %}
    - {{ request.user.summary | to_markdown | safe }} + {{ request.user.summary|to_markdown|safe }}
    {% endif %} diff --git a/bookwyrm/templates/discover/large-book.html b/bookwyrm/templates/discover/large-book.html index 7def6e13..93026991 100644 --- a/bookwyrm/templates/discover/large-book.html +++ b/bookwyrm/templates/discover/large-book.html @@ -1,5 +1,5 @@ - {% load bookwyrm_tags %} +{% load markdown %} {% load i18n %} {% if book %} diff --git a/bookwyrm/templates/feed/feed.html b/bookwyrm/templates/feed/feed.html index f106b4ce..21e71ae1 100644 --- a/bookwyrm/templates/feed/feed.html +++ b/bookwyrm/templates/feed/feed.html @@ -1,7 +1,6 @@ {% extends 'feed/feed_layout.html' %} {% load i18n %} -{% load bookwyrm_tags %} -{% load humanize %} + {% block panel %}

    diff --git a/bookwyrm/templates/feed/feed_layout.html b/bookwyrm/templates/feed/feed_layout.html index 75fc1951..3c6b65a1 100644 --- a/bookwyrm/templates/feed/feed_layout.html +++ b/bookwyrm/templates/feed/feed_layout.html @@ -1,6 +1,5 @@ {% extends 'layout.html' %} {% load i18n %} -{% load bookwyrm_tags %} {% block title %}{% trans "Updates" %}{% endblock %} diff --git a/bookwyrm/templates/feed/thread.html b/bookwyrm/templates/feed/thread.html index 18ab6ea3..793cddfc 100644 --- a/bookwyrm/templates/feed/thread.html +++ b/bookwyrm/templates/feed/thread.html @@ -1,4 +1,4 @@ -{% load bookwyrm_tags %} +{% load status_display %}
    {% with depth=depth|add:1 %} diff --git a/bookwyrm/templates/lists/list.html b/bookwyrm/templates/lists/list.html index 2902f793..55dc68aa 100644 --- a/bookwyrm/templates/lists/list.html +++ b/bookwyrm/templates/lists/list.html @@ -1,12 +1,13 @@ {% extends 'lists/list_layout.html' %} {% load i18n %} {% load bookwyrm_tags %} +{% load markdown %} {% block panel %} {% if request.user == list.user and pending_count %} {% endif %} diff --git a/bookwyrm/templates/lists/list_items.html b/bookwyrm/templates/lists/list_items.html index b59e2a96..1fdaaca9 100644 --- a/bookwyrm/templates/lists/list_items.html +++ b/bookwyrm/templates/lists/list_items.html @@ -1,4 +1,4 @@ -{% load bookwyrm_tags %} +{% load markdown %}
    {% for list in lists %}
    diff --git a/bookwyrm/templates/lists/list_layout.html b/bookwyrm/templates/lists/list_layout.html index c0899c84..de7665e8 100644 --- a/bookwyrm/templates/lists/list_layout.html +++ b/bookwyrm/templates/lists/list_layout.html @@ -1,6 +1,5 @@ {% extends 'layout.html' %} {% load i18n %} -{% load bookwyrm_tags %} {% block title %}{{ list.name }}{% endblock %} diff --git a/bookwyrm/templates/lists/lists.html b/bookwyrm/templates/lists/lists.html index a7a54811..d4b0a7c2 100644 --- a/bookwyrm/templates/lists/lists.html +++ b/bookwyrm/templates/lists/lists.html @@ -1,5 +1,5 @@ {% extends 'layout.html' %} -{% load bookwyrm_tags %} +{% load utilities %} {% load i18n %} {% block title %}{% trans "Lists" %}{% endblock %} diff --git a/bookwyrm/templates/moderation/report.html b/bookwyrm/templates/moderation/report.html index 934799e3..db03850f 100644 --- a/bookwyrm/templates/moderation/report.html +++ b/bookwyrm/templates/moderation/report.html @@ -1,6 +1,5 @@ {% extends 'settings/admin_layout.html' %} {% load i18n %} -{% load bookwyrm_tags %} {% load humanize %} {% block title %}{% blocktrans with report_id=report.id username=report.user.username %}Report #{{ report_id }}: {{ username }}{% endblocktrans %}{% endblock %} @@ -29,7 +28,7 @@ {{ comment.user.display_name }}
    diff --git a/bookwyrm/templates/notifications.html b/bookwyrm/templates/notifications.html index ed623b12..a51b2fdb 100644 --- a/bookwyrm/templates/notifications.html +++ b/bookwyrm/templates/notifications.html @@ -1,7 +1,7 @@ {% extends 'layout.html' %} {% load i18n %} -{% load humanize %} {% load bookwyrm_tags %} +{% load humanize %} {% block title %}{% trans "Notifications" %}{% endblock %} diff --git a/bookwyrm/templates/search/user.html b/bookwyrm/templates/search/user.html index c6adc613..56bb1bfa 100644 --- a/bookwyrm/templates/search/user.html +++ b/bookwyrm/templates/search/user.html @@ -1,5 +1,4 @@ {% extends 'search/layout.html' %} -{% load bookwyrm_tags %} {% block panel %} diff --git a/bookwyrm/templates/settings/federated_server.html b/bookwyrm/templates/settings/federated_server.html index 386433f3..b300a92d 100644 --- a/bookwyrm/templates/settings/federated_server.html +++ b/bookwyrm/templates/settings/federated_server.html @@ -1,7 +1,8 @@ {% extends 'settings/admin_layout.html' %} -{% block title %}{{ server.server_name }}{% endblock %} {% load i18n %} -{% load bookwyrm_tags %} +{% load markdown %} + +{% block title %}{{ server.server_name }}{% endblock %} {% block header %} {{ server.server_name }} diff --git a/bookwyrm/templates/snippets/avatar.html b/bookwyrm/templates/snippets/avatar.html index d53acf2b..bb37c46d 100644 --- a/bookwyrm/templates/snippets/avatar.html +++ b/bookwyrm/templates/snippets/avatar.html @@ -1,3 +1,2 @@ -{% load bookwyrm_tags %} {{ user.alt_text }} diff --git a/bookwyrm/templates/snippets/book_cover.html b/bookwyrm/templates/snippets/book_cover.html index 3c6f212d..f64ec3e4 100644 --- a/bookwyrm/templates/snippets/book_cover.html +++ b/bookwyrm/templates/snippets/book_cover.html @@ -1,6 +1,5 @@ {% spaceless %} -{% load bookwyrm_tags %} {% load i18n %}
    {{ title }} by {% endblocktrans %}{% include 'snippets/authors.html' with book=book %} {% else %} diff --git a/bookwyrm/templates/snippets/create_status_form.html b/bookwyrm/templates/snippets/create_status_form.html index ca9cb5b4..5fd91898 100644 --- a/bookwyrm/templates/snippets/create_status_form.html +++ b/bookwyrm/templates/snippets/create_status_form.html @@ -1,5 +1,6 @@ {% load bookwyrm_tags %} {% load utilities %} +{% load status_display %} {% load i18n %} diff --git a/bookwyrm/templates/snippets/form_rate_stars.html b/bookwyrm/templates/snippets/form_rate_stars.html index 3abebac0..a53733df 100644 --- a/bookwyrm/templates/snippets/form_rate_stars.html +++ b/bookwyrm/templates/snippets/form_rate_stars.html @@ -1,6 +1,5 @@ {% spaceless %} {% load i18n %} -{% load bookwyrm_tags %}
    diff --git a/bookwyrm/templates/snippets/status/status_header.html b/bookwyrm/templates/snippets/status/status_header.html index 605b46cc..b06c6bfc 100644 --- a/bookwyrm/templates/snippets/status/status_header.html +++ b/bookwyrm/templates/snippets/status/status_header.html @@ -1,4 +1,5 @@ -{% load bookwyrm_tags %} +{% load utilities %} +{% load status_display %} {% load i18n %} {% load humanize %} @@ -29,7 +30,7 @@ {% if status.status_type == 'GeneratedNote' %} - {{ status.content | safe }} + {{ status.content|safe }} {% elif status.status_type == 'Rating' %} {% trans "rated" %} {% elif status.status_type == 'Review' %} diff --git a/bookwyrm/templates/snippets/trimmed_text.html b/bookwyrm/templates/snippets/trimmed_text.html index 6e222e97..cfffe055 100644 --- a/bookwyrm/templates/snippets/trimmed_text.html +++ b/bookwyrm/templates/snippets/trimmed_text.html @@ -1,4 +1,4 @@ -{% load bookwyrm_tags %} +{% load markdown %} {% load utilities %} {% load i18n %} diff --git a/bookwyrm/templates/snippets/user_options.html b/bookwyrm/templates/snippets/user_options.html index 32e83ed2..01cc09bf 100644 --- a/bookwyrm/templates/snippets/user_options.html +++ b/bookwyrm/templates/snippets/user_options.html @@ -1,6 +1,6 @@ {% extends 'components/dropdown.html' %} {% load i18n %} -{% load bookwyrm_tags %} +{% load utilities %} {% block dropdown-trigger %} diff --git a/bookwyrm/templates/user/layout.html b/bookwyrm/templates/user/layout.html index 661d8078..0830a406 100644 --- a/bookwyrm/templates/user/layout.html +++ b/bookwyrm/templates/user/layout.html @@ -1,7 +1,8 @@ {% extends 'layout.html' %} {% load i18n %} {% load humanize %} -{% load bookwyrm_tags %} +{% load utilities %} +{% load markdown %} {% block title %}{{ user.display_name }}{% endblock %} @@ -23,7 +24,7 @@ {% if user.summary %}
    - {{ user.summary | to_markdown | safe }} + {{ user.summary|to_markdown|safe }}
    {% endif %}
    diff --git a/bookwyrm/templates/user/relationships/layout.html b/bookwyrm/templates/user/relationships/layout.html index f36b304c..0348d82f 100644 --- a/bookwyrm/templates/user/relationships/layout.html +++ b/bookwyrm/templates/user/relationships/layout.html @@ -1,6 +1,6 @@ {% extends 'user/layout.html' %} {% load i18n %} -{% load bookwyrm_tags %} +{% load utilities %} {% block tabs %} {% with user|username as username %} diff --git a/bookwyrm/templates/user/shelf/shelf.html b/bookwyrm/templates/user/shelf/shelf.html index 86610629..0f386385 100644 --- a/bookwyrm/templates/user/shelf/shelf.html +++ b/bookwyrm/templates/user/shelf/shelf.html @@ -1,5 +1,5 @@ {% extends 'user/layout.html' %} -{% load bookwyrm_tags %} +{% load utilities %} {% load humanize %} {% load i18n %} @@ -101,14 +101,14 @@ {% include 'snippets/authors.html' %} - {{ book.created_date | naturalday }} + {{ book.created_date|naturalday }} {% latest_read_through book user as read_through %} - {{ read_through.start_date | naturalday |default_if_none:""}} + {{ read_through.start_date|naturalday|default_if_none:""}} - {{ read_through.finish_date | naturalday |default_if_none:""}} + {{ read_through.finish_date|naturalday|default_if_none:""}} {% if request.user.is_authenticated %} diff --git a/bookwyrm/templates/user/user.html b/bookwyrm/templates/user/user.html index 99698575..5afe168a 100644 --- a/bookwyrm/templates/user/user.html +++ b/bookwyrm/templates/user/user.html @@ -1,6 +1,6 @@ {% extends 'user/layout.html' %} {% load i18n %} -{% load bookwyrm_tags %} +{% load utilities %} {% block title %}{{ user.display_name }}{% endblock %} diff --git a/bookwyrm/templates/user/user_preview.html b/bookwyrm/templates/user/user_preview.html index c450365b..22d44c87 100644 --- a/bookwyrm/templates/user/user_preview.html +++ b/bookwyrm/templates/user/user_preview.html @@ -1,5 +1,6 @@ {% load i18n %} {% load humanize %} +{% load utilities %} {% load bookwyrm_tags %}
    diff --git a/bookwyrm/templates/user_admin/user.html b/bookwyrm/templates/user_admin/user.html index 46390650..c79c6971 100644 --- a/bookwyrm/templates/user_admin/user.html +++ b/bookwyrm/templates/user_admin/user.html @@ -1,7 +1,5 @@ {% extends 'settings/admin_layout.html' %} {% load i18n %} -{% load bookwyrm_tags %} -{% load humanize %} {% block title %}{{ user.username }}{% endblock %} {% block header %}{{ user.username }}{% endblock %} diff --git a/bookwyrm/templates/user_admin/user_info.html b/bookwyrm/templates/user_admin/user_info.html index e5f5d580..579b3af7 100644 --- a/bookwyrm/templates/user_admin/user_info.html +++ b/bookwyrm/templates/user_admin/user_info.html @@ -1,5 +1,5 @@ {% load i18n %} -{% load bookwyrm_tags %} +{% load markdown %}

    {% trans "User details" %}

    @@ -7,7 +7,7 @@ {% include 'user/user_preview.html' with user=user %} {% if user.summary %}
    - {{ user.summary | to_markdown | safe }} + {{ user.summary|to_markdown|safe }}
    {% endif %} diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index db9335e8..70228799 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -36,57 +36,12 @@ def get_user_rating(book, user): return 0 -@register.filter(name="replies") -def get_replies(status): - """get all direct replies to a status""" - # TODO: this limit could cause problems - return models.Status.objects.filter( - reply_parent=status, - deleted=False, - ).select_subclasses()[:10] - - -@register.filter(name="parent") -def get_parent(status): - """get the reply parent for a status""" - return ( - models.Status.objects.filter(id=status.reply_parent_id) - .select_subclasses() - .get() - ) - - -@register.filter(name="boosted_status") -def get_boosted(boost): - """load a boosted status. have to do this or it won't get foreign keys""" - return ( - models.Status.objects.select_subclasses().get(id=boost.boosted_status.id) - ) - - @register.filter(name="book_description") def get_book_description(book): """use the work's text if the book doesn't have it""" return book.description or book.parent_work.description -@register.filter(name="to_markdown") -def get_markdown(content): - """convert markdown to html""" - if content: - return to_markdown(content) - return None - - -@register.filter(name="mentions") -def get_mentions(status, user): - """people to @ in a reply: the parent and all mentions""" - mentions = set([status.user] + list(status.mention_users.all())) - return ( - " ".join("@" + get_user_identifier(m) for m in mentions if not m == user) + " " - ) - - @register.filter(name="next_shelf") def get_next_shelf(current_shelf): """shelf you'd use to update reading progress""" diff --git a/bookwyrm/templatetags/markdown.py b/bookwyrm/templatetags/markdown.py new file mode 100644 index 00000000..370d60a1 --- /dev/null +++ b/bookwyrm/templatetags/markdown.py @@ -0,0 +1,14 @@ +""" template filters """ +from django import template +from bookwyrm.views.status import to_markdown + + +register = template.Library() + + +@register.filter(name="to_markdown") +def get_markdown(content): + """convert markdown to html""" + if content: + return to_markdown(content) + return None diff --git a/bookwyrm/templatetags/status_display.py b/bookwyrm/templatetags/status_display.py new file mode 100644 index 00000000..589881bf --- /dev/null +++ b/bookwyrm/templatetags/status_display.py @@ -0,0 +1,46 @@ +""" template filters """ +from django import template + +from bookwyrm import models +from bookwyrm.templatetags.utilities import get_user_identifier + + +register = template.Library() + + +@register.filter(name="mentions") +def get_mentions(status, user): + """people to @ in a reply: the parent and all mentions""" + mentions = set([status.user] + list(status.mention_users.all())) + return ( + " ".join("@" + get_user_identifier(m) for m in mentions if not m == user) + " " + ) + + + +@register.filter(name="replies") +def get_replies(status): + """get all direct replies to a status""" + # TODO: this limit could cause problems + return models.Status.objects.filter( + reply_parent=status, + deleted=False, + ).select_subclasses()[:10] + + +@register.filter(name="parent") +def get_parent(status): + """get the reply parent for a status""" + return ( + models.Status.objects.filter(id=status.reply_parent_id) + .select_subclasses() + .get() + ) + + +@register.filter(name="boosted_status") +def get_boosted(boost): + """load a boosted status. have to do this or it won't get foreign keys""" + return ( + models.Status.objects.select_subclasses().get(id=boost.boosted_status.id) + ) From dbf795be5d785f1464b902850ab9bd2d0c6b4f0b Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 11 May 2021 15:16:27 -0700 Subject: [PATCH 10/11] Python formatting --- bookwyrm/templatetags/status_display.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bookwyrm/templatetags/status_display.py b/bookwyrm/templatetags/status_display.py index 589881bf..f39de9e3 100644 --- a/bookwyrm/templatetags/status_display.py +++ b/bookwyrm/templatetags/status_display.py @@ -17,7 +17,6 @@ def get_mentions(status, user): ) - @register.filter(name="replies") def get_replies(status): """get all direct replies to a status""" @@ -41,6 +40,4 @@ def get_parent(status): @register.filter(name="boosted_status") def get_boosted(boost): """load a boosted status. have to do this or it won't get foreign keys""" - return ( - models.Status.objects.select_subclasses().get(id=boost.boosted_status.id) - ) + return models.Status.objects.select_subclasses().get(id=boost.boosted_status.id) From 865faf671497c947e3ba6e7dca2d416e5aee3f61 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 11 May 2021 15:52:41 -0700 Subject: [PATCH 11/11] Fixes tests --- bookwyrm/templates/user/shelf/shelf.html | 1 + bookwyrm/tests/test_templatetags.py | 34 ++++++++++++++---------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/bookwyrm/templates/user/shelf/shelf.html b/bookwyrm/templates/user/shelf/shelf.html index 0f386385..9e7ae4f7 100644 --- a/bookwyrm/templates/user/shelf/shelf.html +++ b/bookwyrm/templates/user/shelf/shelf.html @@ -1,4 +1,5 @@ {% extends 'user/layout.html' %} +{% load bookwyrm_tags %} {% load utilities %} {% load humanize %} {% load i18n %} diff --git a/bookwyrm/tests/test_templatetags.py b/bookwyrm/tests/test_templatetags.py index 3b782fd5..4b1aa594 100644 --- a/bookwyrm/tests/test_templatetags.py +++ b/bookwyrm/tests/test_templatetags.py @@ -6,7 +6,13 @@ from django.test import TestCase from django.utils import timezone from bookwyrm import models -from bookwyrm.templatetags import bookwyrm_tags +from bookwyrm.templatetags import ( + bookwyrm_tags, + interaction, + markdown, + status_display, + utilities, +) @patch("bookwyrm.activitystreams.ActivityStream.add_status") @@ -45,12 +51,12 @@ class TemplateTags(TestCase): def test_get_user_identifer_local(self, _): """fall back to the simplest uid available""" self.assertNotEqual(self.user.username, self.user.localname) - self.assertEqual(bookwyrm_tags.get_user_identifier(self.user), "mouse") + self.assertEqual(utilities.get_user_identifier(self.user), "mouse") def test_get_user_identifer_remote(self, _): """for a remote user, should be their full username""" self.assertEqual( - bookwyrm_tags.get_user_identifier(self.remote_user), "rat@example.com" + utilities.get_user_identifier(self.remote_user), "rat@example.com" ) def test_get_replies(self, _): @@ -75,7 +81,7 @@ class TemplateTags(TestCase): deleted_date=timezone.now(), ) - replies = bookwyrm_tags.get_replies(parent) + replies = status_display.get_replies(parent) self.assertEqual(len(replies), 2) self.assertTrue(first_child in replies) self.assertTrue(second_child in replies) @@ -91,7 +97,7 @@ class TemplateTags(TestCase): reply_parent=parent, user=self.user, content="hi" ) - result = bookwyrm_tags.get_parent(child) + result = status_display.get_parent(child) self.assertEqual(result, parent) self.assertIsInstance(result, models.Review) @@ -99,26 +105,26 @@ class TemplateTags(TestCase): """did a user like a status""" status = models.Review.objects.create(user=self.remote_user, book=self.book) - self.assertFalse(bookwyrm_tags.get_user_liked(self.user, status)) + self.assertFalse(interaction.get_user_liked(self.user, status)) with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): models.Favorite.objects.create(user=self.user, status=status) - self.assertTrue(bookwyrm_tags.get_user_liked(self.user, status)) + self.assertTrue(interaction.get_user_liked(self.user, status)) def test_get_user_boosted(self, _): """did a user boost a status""" status = models.Review.objects.create(user=self.remote_user, book=self.book) - self.assertFalse(bookwyrm_tags.get_user_boosted(self.user, status)) + self.assertFalse(interaction.get_user_boosted(self.user, status)) with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): models.Boost.objects.create(user=self.user, boosted_status=status) - self.assertTrue(bookwyrm_tags.get_user_boosted(self.user, status)) + self.assertTrue(interaction.get_user_boosted(self.user, status)) def test_get_boosted(self, _): """load a boosted status""" with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): status = models.Review.objects.create(user=self.remote_user, book=self.book) boost = models.Boost.objects.create(user=self.user, boosted_status=status) - boosted = bookwyrm_tags.get_boosted(boost) + boosted = status_display.get_boosted(boost) self.assertIsInstance(boosted, models.Review) self.assertEqual(boosted, status) @@ -140,21 +146,21 @@ class TemplateTags(TestCase): def test_get_uuid(self, _): """uuid functionality""" - uuid = bookwyrm_tags.get_uuid("hi") + uuid = utilities.get_uuid("hi") self.assertTrue(re.match(r"hi[A-Za-z0-9\-]", uuid)) def test_get_markdown(self, _): """mardown format data""" - result = bookwyrm_tags.get_markdown("_hi_") + result = markdown.get_markdown("_hi_") self.assertEqual(result, "

    hi

    ") - result = bookwyrm_tags.get_markdown("_hi_") + result = markdown.get_markdown("_hi_") self.assertEqual(result, "

    hi

    ") def test_get_mentions(self, _): """list of people mentioned""" status = models.Status.objects.create(content="hi", user=self.remote_user) - result = bookwyrm_tags.get_mentions(status, self.user) + result = status_display.get_mentions(status, self.user) self.assertEqual(result, "@rat@example.com ") def test_related_status(self, _):