From 8746409e6debf3fab1f1cd3f0891622f84c2145f Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 18 Jan 2022 11:20:27 -0800 Subject: [PATCH 01/13] Adds shelf tags file --- bookwyrm/templates/shelf/shelf.html | 2 +- .../snippets/create_status/comment.html | 2 +- .../templates/snippets/shelf_selector.html | 4 +- .../snippets/shelve_button/shelve_button.html | 2 +- .../shelve_button_dropdown_options.html | 2 +- .../shelve_button/shelve_button_options.html | 2 +- bookwyrm/templatetags/bookwyrm_tags.py | 63 ---------------- bookwyrm/templatetags/shelf_tags.py | 71 +++++++++++++++++++ 8 files changed, 78 insertions(+), 70 deletions(-) create mode 100644 bookwyrm/templatetags/shelf_tags.py diff --git a/bookwyrm/templates/shelf/shelf.html b/bookwyrm/templates/shelf/shelf.html index 0e295a87..a630918a 100644 --- a/bookwyrm/templates/shelf/shelf.html +++ b/bookwyrm/templates/shelf/shelf.html @@ -1,5 +1,5 @@ {% extends 'layout.html' %} -{% load bookwyrm_tags %} +{% load shelf_tags %} {% load utilities %} {% load humanize %} {% load i18n %} diff --git a/bookwyrm/templates/snippets/create_status/comment.html b/bookwyrm/templates/snippets/create_status/comment.html index 10cdf639..65b32269 100644 --- a/bookwyrm/templates/snippets/create_status/comment.html +++ b/bookwyrm/templates/snippets/create_status/comment.html @@ -1,5 +1,5 @@ {% extends "snippets/create_status/layout.html" %} -{% load bookwyrm_tags %} +{% load shelf_tags %} {% load i18n %} {% load utilities %} {% load status_display %} diff --git a/bookwyrm/templates/snippets/shelf_selector.html b/bookwyrm/templates/snippets/shelf_selector.html index 323e04a2..ea609666 100644 --- a/bookwyrm/templates/snippets/shelf_selector.html +++ b/bookwyrm/templates/snippets/shelf_selector.html @@ -1,7 +1,7 @@ {% extends 'components/dropdown.html' %} -{% load i18n %} -{% load bookwyrm_tags %} +{% load shelf_tags %} {% load utilities %} +{% load i18n %} {% block dropdown-trigger %} {% trans "Move book" %} diff --git a/bookwyrm/templates/snippets/shelve_button/shelve_button.html b/bookwyrm/templates/snippets/shelve_button/shelve_button.html index 37c57fc2..04dc4e4b 100644 --- a/bookwyrm/templates/snippets/shelve_button/shelve_button.html +++ b/bookwyrm/templates/snippets/shelve_button/shelve_button.html @@ -1,5 +1,5 @@ -{% load bookwyrm_tags %} {% load utilities %} +{% load shelf_tags %} {% if request.user.is_authenticated %} diff --git a/bookwyrm/templates/snippets/shelve_button/shelve_button_dropdown_options.html b/bookwyrm/templates/snippets/shelve_button/shelve_button_dropdown_options.html index 15e2bb51..5a29f9ad 100644 --- a/bookwyrm/templates/snippets/shelve_button/shelve_button_dropdown_options.html +++ b/bookwyrm/templates/snippets/shelve_button/shelve_button_dropdown_options.html @@ -1,5 +1,5 @@ -{% load bookwyrm_tags %} {% load utilities %} +{% load shelf_tags %} {% load i18n %} {% with next_shelf_identifier=active_shelf.shelf.identifier|next_shelf %} diff --git a/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html b/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html index d6ca9933..04f4bdc2 100644 --- a/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html +++ b/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html @@ -1,5 +1,5 @@ -{% load bookwyrm_tags %} {% load utilities %} +{% load shelf_tags %} {% load i18n %} {% with next_shelf_identifier=active_shelf.shelf.identifier|next_shelf %} diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index 4eff0d82..08d147aa 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -43,36 +43,12 @@ def get_user_rating(book, user): return 0 -@register.filter(name="is_book_on_shelf") -def get_is_book_on_shelf(book, shelf): - """is a book on a shelf""" - return cache.get_or_set( - f"book-on-shelf-{book.id}-{shelf.id}", - lambda b, s: s.books.filter(id=b.id).exists(), - book, - shelf, - timeout=15552000, - ) - - @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="next_shelf") -def get_next_shelf(current_shelf): - """shelf you'd use to update reading progress""" - if current_shelf == "to-read": - return "reading" - if current_shelf == "reading": - return "read" - if current_shelf == "read": - return "complete" - return "to-read" - - @register.filter(name="load_subclass") def load_subclass(status): """sometimes you didn't select_subclass""" @@ -146,45 +122,6 @@ def related_status(notification): return load_subclass(notification.related_status) -@register.simple_tag(takes_context=True) -def active_shelf(context, book): - """check what shelf a user has a book on, if any""" - user = context["request"].user - return ( - cache.get_or_set( - f"active_shelf-{user.id}-{book.id}", - lambda u, b: ( - models.ShelfBook.objects.filter( - shelf__user=u, - book__parent_work__editions=b, - ).first() - or False - ), - user, - book, - timeout=15552000, - ) - or {"book": book} - ) - - -@register.simple_tag(takes_context=False) -def latest_read_through(book, user): - """the most recent read activity""" - return cache.get_or_set( - f"latest_read_through-{user.id}-{book.id}", - lambda u, b: ( - models.ReadThrough.objects.filter(user=u, book=b, is_active=True) - .order_by("-start_date") - .first() - or False - ), - user, - book, - timeout=15552000, - ) - - @register.simple_tag(takes_context=False) def get_landing_books(): """list of books for the landing page""" diff --git a/bookwyrm/templatetags/shelf_tags.py b/bookwyrm/templatetags/shelf_tags.py new file mode 100644 index 00000000..7aef638f --- /dev/null +++ b/bookwyrm/templatetags/shelf_tags.py @@ -0,0 +1,71 @@ +""" Filters and tags related to shelving books """ +from django import template + +from bookwyrm import models +from bookwyrm.utils import cache + + +register = template.Library() + + +@register.filter(name="is_book_on_shelf") +def get_is_book_on_shelf(book, shelf): + """is a book on a shelf""" + return cache.get_or_set( + f"book-on-shelf-{book.id}-{shelf.id}", + lambda b, s: s.books.filter(id=b.id).exists(), + book, + shelf, + timeout=15552000, + ) + + +@register.filter(name="next_shelf") +def get_next_shelf(current_shelf): + """shelf you'd use to update reading progress""" + if current_shelf == "to-read": + return "reading" + if current_shelf == "reading": + return "read" + if current_shelf == "read": + return "complete" + return "to-read" + + +@register.simple_tag(takes_context=True) +def active_shelf(context, book): + """check what shelf a user has a book on, if any""" + user = context["request"].user + return ( + cache.get_or_set( + f"active_shelf-{user.id}-{book.id}", + lambda u, b: ( + models.ShelfBook.objects.filter( + shelf__user=u, + book__parent_work__editions=b, + ).first() + or False + ), + user, + book, + timeout=15552000, + ) + or {"book": book} + ) + + +@register.simple_tag(takes_context=False) +def latest_read_through(book, user): + """the most recent read activity""" + return cache.get_or_set( + f"latest_read_through-{user.id}-{book.id}", + lambda u, b: ( + models.ReadThrough.objects.filter(user=u, book=b, is_active=True) + .order_by("-start_date") + .first() + or False + ), + user, + book, + timeout=15552000, + ) From 4f4d2dc53ea0385b598d78aa90f479dfb4168cd8 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 18 Jan 2022 11:37:38 -0800 Subject: [PATCH 02/13] Adds tests --- .../tests/templatetags/test_bookwyrm_tags.py | 7 -- .../tests/templatetags/test_shelf_tags.py | 70 +++++++++++++++++++ 2 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 bookwyrm/tests/templatetags/test_shelf_tags.py diff --git a/bookwyrm/tests/templatetags/test_bookwyrm_tags.py b/bookwyrm/tests/templatetags/test_bookwyrm_tags.py index 7b8d199d..d14380d1 100644 --- a/bookwyrm/tests/templatetags/test_bookwyrm_tags.py +++ b/bookwyrm/tests/templatetags/test_bookwyrm_tags.py @@ -60,13 +60,6 @@ class BookWyrmTags(TestCase): self.book.save() self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hello") - def test_get_next_shelf(self, *_): - """self progress helper""" - self.assertEqual(bookwyrm_tags.get_next_shelf("to-read"), "reading") - self.assertEqual(bookwyrm_tags.get_next_shelf("reading"), "read") - self.assertEqual(bookwyrm_tags.get_next_shelf("read"), "complete") - self.assertEqual(bookwyrm_tags.get_next_shelf("blooooga"), "to-read") - @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") def test_load_subclass(self, *_): """get a status' real type""" diff --git a/bookwyrm/tests/templatetags/test_shelf_tags.py b/bookwyrm/tests/templatetags/test_shelf_tags.py new file mode 100644 index 00000000..195a5aff --- /dev/null +++ b/bookwyrm/tests/templatetags/test_shelf_tags.py @@ -0,0 +1,70 @@ +""" style fixes and lookups for templates """ +from unittest.mock import patch + +from django.test import TestCase +from django.test.client import RequestFactory + +from bookwyrm import models +from bookwyrm.templatetags import shelf_tags + + +@patch("bookwyrm.activitystreams.add_status_task.delay") +@patch("bookwyrm.activitystreams.remove_status_task.delay") +@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") +@patch("bookwyrm.activitystreams.add_book_statuses_task.delay") +class BookWyrmTags(TestCase): + """lotta different things here""" + + def setUp(self): + """create some filler objects""" + self.factory = RequestFactory() + with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( + "bookwyrm.activitystreams.populate_stream_task.delay" + ), patch("bookwyrm.lists_stream.populate_lists_task.delay"): + self.local_user = models.User.objects.create_user( + "mouse@example.com", + "mouse@mouse.mouse", + "mouseword", + local=True, + localname="mouse", + ) + with patch("bookwyrm.models.user.set_remote_server.delay"): + self.remote_user = models.User.objects.create_user( + "rat", + "rat@rat.rat", + "ratword", + remote_id="http://example.com/rat", + local=False, + ) + self.book = models.Edition.objects.create( + title="Test Book", + parent_work=models.Work.objects.create(title="Test work"), + ) + + def test_get_is_book_on_shelf(self, *_): + """check if a book is on a shelf""" + shelf = self.local_user.shelf_set.first() + self.assertFalse(shelf_tags.get_is_book_on_shelf(self.book, shelf)) + models.ShelfBook.objects.create( + shelf=shelf, book=self.book, user=self.local_user + ) + self.assertTrue(shelf_tags.get_is_book_on_shelf(self.book, shelf)) + + def test_get_next_shelf(self, *_): + """self progress helper""" + self.assertEqual(shelf_tags.get_next_shelf("to-read"), "reading") + self.assertEqual(shelf_tags.get_next_shelf("reading"), "read") + self.assertEqual(shelf_tags.get_next_shelf("read"), "complete") + self.assertEqual(shelf_tags.get_next_shelf("blooooga"), "to-read") + + def test_active_shelf(self, *_): + """get the shelf a book is on""" + shelf = self.local_user.shelf_set.first() + request = self.factory.get("") + request.user = self.local_user + context = {"request": request} + self.assertIsInstance(shelf_tags.active_shelf(context, self.book), dict) + models.ShelfBook.objects.create( + shelf=shelf, book=self.book, user=self.local_user + ) + self.assertEqual(shelf_tags.active_shelf(context, self.book).shelf, shelf) From 6e6e2ab777684f09c0008f32afe552c22befdc37 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 18 Jan 2022 12:02:38 -0800 Subject: [PATCH 03/13] Moves rating tag to file --- bookwyrm/templatetags/bookwyrm_tags.py | 34 --------------------- bookwyrm/templatetags/rating_tags.py | 42 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 34 deletions(-) create mode 100644 bookwyrm/templatetags/rating_tags.py diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index 08d147aa..ba6f7c2f 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -3,46 +3,12 @@ from django import template from django.db.models import Avg, StdDev, Count, F, Q from bookwyrm import models -from bookwyrm.utils import cache from bookwyrm.views.feed import get_suggested_books register = template.Library() -@register.filter(name="rating") -def get_rating(book, user): - """get the overall rating of a book""" - return cache.get_or_set( - f"book-rating-{book.parent_work.id}-{user.id}", - lambda u, b: models.Review.privacy_filter(u) - .filter(book__parent_work__editions=b) - .aggregate(Avg("rating"))["rating__avg"] - or 0, - user, - book, - timeout=15552000, - ) - - -@register.filter(name="user_rating") -def get_user_rating(book, user): - """get a user's rating of a book""" - rating = ( - models.Review.objects.filter( - user=user, - book=book, - rating__isnull=False, - deleted=False, - ) - .order_by("-published_date") - .first() - ) - if rating: - return rating.rating - return 0 - - @register.filter(name="book_description") def get_book_description(book): """use the work's text if the book doesn't have it""" diff --git a/bookwyrm/templatetags/rating_tags.py b/bookwyrm/templatetags/rating_tags.py new file mode 100644 index 00000000..b986f6b8 --- /dev/null +++ b/bookwyrm/templatetags/rating_tags.py @@ -0,0 +1,42 @@ +""" template filters """ +from django import template +from django.db.models import Avg + +from bookwyrm import models +from bookwyrm.utils import cache + + +register = template.Library() + + +@register.filter(name="rating") +def get_rating(book, user): + """get the overall rating of a book""" + return cache.get_or_set( + f"book-rating-{book.parent_work.id}-{user.id}", + lambda u, b: models.Review.privacy_filter(u) + .filter(book__parent_work__editions=b) + .aggregate(Avg("rating"))["rating__avg"] + or 0, + user, + book, + timeout=15552000, + ) + + +@register.filter(name="user_rating") +def get_user_rating(book, user): + """get a user's rating of a book""" + rating = ( + models.Review.objects.filter( + user=user, + book=book, + rating__isnull=False, + deleted=False, + ) + .order_by("-published_date") + .first() + ) + if rating: + return rating.rating + return 0 From b25fa5a1933dc5aa113ca4ae75d2ccef97a02bd5 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 18 Jan 2022 12:07:42 -0800 Subject: [PATCH 04/13] Adds tests --- .../tests/templatetags/test_bookwyrm_tags.py | 10 ----- .../tests/templatetags/test_rating_tags.py | 45 +++++++++++++++++++ .../tests/templatetags/test_shelf_tags.py | 2 +- 3 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 bookwyrm/tests/templatetags/test_rating_tags.py diff --git a/bookwyrm/tests/templatetags/test_bookwyrm_tags.py b/bookwyrm/tests/templatetags/test_bookwyrm_tags.py index d14380d1..d44d9784 100644 --- a/bookwyrm/tests/templatetags/test_bookwyrm_tags.py +++ b/bookwyrm/tests/templatetags/test_bookwyrm_tags.py @@ -34,16 +34,6 @@ class BookWyrmTags(TestCase): ) self.book = models.Edition.objects.create(title="Test Book") - def test_get_user_rating(self, *_): - """get a user's most recent rating of a book""" - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - models.Review.objects.create(user=self.user, book=self.book, rating=3) - self.assertEqual(bookwyrm_tags.get_user_rating(self.book, self.user), 3) - - def test_get_user_rating_doesnt_exist(self, *_): - """there is no rating available""" - self.assertEqual(bookwyrm_tags.get_user_rating(self.book, self.user), 0) - def test_get_book_description(self, *_): """grab it from the edition or the parent""" work = models.Work.objects.create(title="Test Work") diff --git a/bookwyrm/tests/templatetags/test_rating_tags.py b/bookwyrm/tests/templatetags/test_rating_tags.py new file mode 100644 index 00000000..fad864ee --- /dev/null +++ b/bookwyrm/tests/templatetags/test_rating_tags.py @@ -0,0 +1,45 @@ +""" Gettings book ratings """ +from unittest.mock import patch + +from django.test import TestCase + +from bookwyrm import models +from bookwyrm.templatetags import bookwyrm_tags + + +@patch("bookwyrm.activitystreams.add_status_task.delay") +@patch("bookwyrm.activitystreams.remove_status_task.delay") +class RatingTags(TestCase): + """lotta different things here""" + + def setUp(self): + """create some filler objects""" + with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( + "bookwyrm.activitystreams.populate_stream_task.delay" + ), patch("bookwyrm.lists_stream.populate_lists_task.delay"): + self.user = models.User.objects.create_user( + "mouse@example.com", + "mouse@mouse.mouse", + "mouseword", + local=True, + localname="mouse", + ) + with patch("bookwyrm.models.user.set_remote_server.delay"): + self.remote_user = models.User.objects.create_user( + "rat", + "rat@rat.rat", + "ratword", + remote_id="http://example.com/rat", + local=False, + ) + self.book = models.Edition.objects.create(title="Test Book") + + def test_get_user_rating(self, *_): + """get a user's most recent rating of a book""" + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): + models.Review.objects.create(user=self.user, book=self.book, rating=3) + self.assertEqual(bookwyrm_tags.get_user_rating(self.book, self.user), 3) + + def test_get_user_rating_doesnt_exist(self, *_): + """there is no rating available""" + self.assertEqual(bookwyrm_tags.get_user_rating(self.book, self.user), 0) diff --git a/bookwyrm/tests/templatetags/test_shelf_tags.py b/bookwyrm/tests/templatetags/test_shelf_tags.py index 195a5aff..5a88604d 100644 --- a/bookwyrm/tests/templatetags/test_shelf_tags.py +++ b/bookwyrm/tests/templatetags/test_shelf_tags.py @@ -12,7 +12,7 @@ from bookwyrm.templatetags import shelf_tags @patch("bookwyrm.activitystreams.remove_status_task.delay") @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") @patch("bookwyrm.activitystreams.add_book_statuses_task.delay") -class BookWyrmTags(TestCase): +class ShelfTags(TestCase): """lotta different things here""" def setUp(self): From 3d63509e20c89ac18cd603b54077708b2a8bb8d8 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 18 Jan 2022 12:07:46 -0800 Subject: [PATCH 05/13] Updates snippets that use rating tags --- bookwyrm/templates/discover/large-book.html | 2 +- bookwyrm/templates/landing/large-book.html | 2 +- bookwyrm/templates/landing/small-book.html | 2 +- bookwyrm/templates/lists/embed-list.html | 2 +- bookwyrm/templates/lists/list.html | 2 +- bookwyrm/templates/snippets/rate_action.html | 3 ++- bookwyrm/templates/snippets/status/content_status.html | 1 + 7 files changed, 8 insertions(+), 6 deletions(-) diff --git a/bookwyrm/templates/discover/large-book.html b/bookwyrm/templates/discover/large-book.html index 1fa0afb9..a6ff0aca 100644 --- a/bookwyrm/templates/discover/large-book.html +++ b/bookwyrm/templates/discover/large-book.html @@ -1,4 +1,4 @@ -{% load bookwyrm_tags %} +{% load rating_tags %} {% load i18n %} {% load utilities %} {% load status_display %} diff --git a/bookwyrm/templates/landing/large-book.html b/bookwyrm/templates/landing/large-book.html index 03ec718b..6a27a4a3 100644 --- a/bookwyrm/templates/landing/large-book.html +++ b/bookwyrm/templates/landing/large-book.html @@ -1,4 +1,4 @@ -{% load bookwyrm_tags %} +{% load rating_tags %} {% load markdown %} {% load i18n %} diff --git a/bookwyrm/templates/landing/small-book.html b/bookwyrm/templates/landing/small-book.html index 813fb797..31b09580 100644 --- a/bookwyrm/templates/landing/small-book.html +++ b/bookwyrm/templates/landing/small-book.html @@ -1,4 +1,4 @@ -{% load bookwyrm_tags %} +{% load rating_tags %} {% load i18n %} {% if book %} diff --git a/bookwyrm/templates/lists/embed-list.html b/bookwyrm/templates/lists/embed-list.html index 54dc80ab..9c07291c 100644 --- a/bookwyrm/templates/lists/embed-list.html +++ b/bookwyrm/templates/lists/embed-list.html @@ -1,6 +1,6 @@ {% extends 'embed-layout.html' %} {% load i18n %} -{% load bookwyrm_tags %} +{% load rating_tags %} {% load bookwyrm_group_tags %} {% load markdown %} diff --git a/bookwyrm/templates/lists/list.html b/bookwyrm/templates/lists/list.html index 880413cd..eb351df3 100644 --- a/bookwyrm/templates/lists/list.html +++ b/bookwyrm/templates/lists/list.html @@ -1,6 +1,6 @@ {% extends 'lists/layout.html' %} {% load i18n %} -{% load bookwyrm_tags %} +{% load rating_tags %} {% load bookwyrm_group_tags %} {% load markdown %} diff --git a/bookwyrm/templates/snippets/rate_action.html b/bookwyrm/templates/snippets/rate_action.html index 767039a3..6ecbceff 100644 --- a/bookwyrm/templates/snippets/rate_action.html +++ b/bookwyrm/templates/snippets/rate_action.html @@ -1,5 +1,6 @@ {% load i18n %} -{% load bookwyrm_tags %} +{% load rating_tags %} + {% if request.user.is_authenticated %} {% trans "Leave a rating" %}
diff --git a/bookwyrm/templates/snippets/status/content_status.html b/bookwyrm/templates/snippets/status/content_status.html index 01734cc7..6d9b9263 100644 --- a/bookwyrm/templates/snippets/status/content_status.html +++ b/bookwyrm/templates/snippets/status/content_status.html @@ -1,4 +1,5 @@ {% load bookwyrm_tags %} +{% load rating_tags %} {% load markdown %} {% load i18n %} {% load static %} From 33c6e5aabe936311434f7b18c26fb1e871436a51 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 18 Jan 2022 12:28:59 -0800 Subject: [PATCH 06/13] Moves remaining tags into new files --- bookwyrm/templates/about/about.html | 2 +- bookwyrm/templates/book/book.html | 2 +- bookwyrm/templates/book/file_links/links.html | 2 +- bookwyrm/templates/discover/small-book.html | 2 +- bookwyrm/templates/feed/status.html | 2 +- bookwyrm/templates/feed/suggested_books.html | 2 +- bookwyrm/templates/groups/group.html | 1 - bookwyrm/templates/groups/members.html | 1 - bookwyrm/templates/landing/landing.html | 2 +- .../templates/notifications/items/layout.html | 2 +- .../snippets/create_status/layout.html | 1 - .../snippets/create_status/quotation.html | 1 - .../snippets/create_status/review.html | 1 - .../snippets/status/content_status.html | 2 +- .../snippets/status/generated_status.html | 2 +- bookwyrm/templates/user/user_preview.html | 2 +- bookwyrm/templatetags/book_display_tags.py | 17 ++++++ bookwyrm/templatetags/feed_page_tags.py | 28 ++++++++++ ...{bookwyrm_tags.py => landing_page_tags.py} | 53 ------------------- .../templatetags/notification_page_tags.py | 14 +++++ bookwyrm/templatetags/user_page_tags.py | 14 +++++ .../templatetags/test_book_display_tags.py | 51 ++++++++++++++++++ ...ookwyrm_tags.py => test_feed_page_tags.py} | 33 ------------ .../test_notification_page_tags.py | 38 +++++++++++++ 24 files changed, 173 insertions(+), 102 deletions(-) create mode 100644 bookwyrm/templatetags/book_display_tags.py create mode 100644 bookwyrm/templatetags/feed_page_tags.py rename bookwyrm/templatetags/{bookwyrm_tags.py => landing_page_tags.py} (59%) create mode 100644 bookwyrm/templatetags/notification_page_tags.py create mode 100644 bookwyrm/templatetags/user_page_tags.py create mode 100644 bookwyrm/tests/templatetags/test_book_display_tags.py rename bookwyrm/tests/templatetags/{test_bookwyrm_tags.py => test_feed_page_tags.py} (61%) create mode 100644 bookwyrm/tests/templatetags/test_notification_page_tags.py diff --git a/bookwyrm/templates/about/about.html b/bookwyrm/templates/about/about.html index d39d7048..dd3417ab 100644 --- a/bookwyrm/templates/about/about.html +++ b/bookwyrm/templates/about/about.html @@ -2,7 +2,7 @@ {% load humanize %} {% load i18n %} {% load utilities %} -{% load bookwyrm_tags %} +{% load landing_page_tags %} {% load cache %} {% block title %} diff --git a/bookwyrm/templates/book/book.html b/bookwyrm/templates/book/book.html index d2ab99b4..43f2171c 100644 --- a/bookwyrm/templates/book/book.html +++ b/bookwyrm/templates/book/book.html @@ -1,6 +1,6 @@ {% extends 'layout.html' %} {% load i18n %} -{% load bookwyrm_tags %} +{% load book_display_tags %} {% load humanize %} {% load utilities %} {% load static %} diff --git a/bookwyrm/templates/book/file_links/links.html b/bookwyrm/templates/book/file_links/links.html index fbc95b56..2147bf6e 100644 --- a/bookwyrm/templates/book/file_links/links.html +++ b/bookwyrm/templates/book/file_links/links.html @@ -1,5 +1,5 @@ {% load i18n %} -{% load bookwyrm_tags %} +{% load book_display_tags %} {% load utilities %} {% get_book_file_links book as links %} diff --git a/bookwyrm/templates/discover/small-book.html b/bookwyrm/templates/discover/small-book.html index 76732ca1..2da93d52 100644 --- a/bookwyrm/templates/discover/small-book.html +++ b/bookwyrm/templates/discover/small-book.html @@ -1,4 +1,4 @@ -{% load bookwyrm_tags %} +{% load landing_page_tags %} {% load utilities %} {% load i18n %} {% load status_display %} diff --git a/bookwyrm/templates/feed/status.html b/bookwyrm/templates/feed/status.html index e7b9280d..ed828ae0 100644 --- a/bookwyrm/templates/feed/status.html +++ b/bookwyrm/templates/feed/status.html @@ -1,6 +1,6 @@ {% extends 'feed/layout.html' %} +{% load feed_page_tags %} {% load i18n %} -{% load bookwyrm_tags %} {% block opengraph_images %} diff --git a/bookwyrm/templates/feed/suggested_books.html b/bookwyrm/templates/feed/suggested_books.html index a3d3f1fa..435d4f51 100644 --- a/bookwyrm/templates/feed/suggested_books.html +++ b/bookwyrm/templates/feed/suggested_books.html @@ -1,5 +1,5 @@ {% load i18n %} -{% load bookwyrm_tags %} +{% load feed_page_tags %} {% suggested_books as suggested_books %}
diff --git a/bookwyrm/templates/groups/group.html b/bookwyrm/templates/groups/group.html index 1a71bda8..88f11420 100644 --- a/bookwyrm/templates/groups/group.html +++ b/bookwyrm/templates/groups/group.html @@ -1,6 +1,5 @@ {% extends 'groups/layout.html' %} {% load i18n %} -{% load bookwyrm_tags %} {% load bookwyrm_group_tags %} {% load markdown %} diff --git a/bookwyrm/templates/groups/members.html b/bookwyrm/templates/groups/members.html index 8b06d178..1d1ac314 100644 --- a/bookwyrm/templates/groups/members.html +++ b/bookwyrm/templates/groups/members.html @@ -1,7 +1,6 @@ {% load i18n %} {% load utilities %} {% load humanize %} -{% load bookwyrm_tags %} {% load bookwyrm_group_tags %}

Group Members

diff --git a/bookwyrm/templates/landing/landing.html b/bookwyrm/templates/landing/landing.html index c3771759..ec8bcee0 100644 --- a/bookwyrm/templates/landing/landing.html +++ b/bookwyrm/templates/landing/landing.html @@ -1,7 +1,7 @@ {% extends 'landing/layout.html' %} {% load i18n %} {% load cache %} -{% load bookwyrm_tags %} +{% load landing_page_tags %} {% block panel %} diff --git a/bookwyrm/templates/notifications/items/layout.html b/bookwyrm/templates/notifications/items/layout.html index 6ddbdcc3..d595bf20 100644 --- a/bookwyrm/templates/notifications/items/layout.html +++ b/bookwyrm/templates/notifications/items/layout.html @@ -1,4 +1,4 @@ -{% load bookwyrm_tags %} +{% load notification_page_tags %} {% related_status notification as related_status %}
diff --git a/bookwyrm/templates/snippets/create_status/layout.html b/bookwyrm/templates/snippets/create_status/layout.html index 8d24fa02..0585638d 100644 --- a/bookwyrm/templates/snippets/create_status/layout.html +++ b/bookwyrm/templates/snippets/create_status/layout.html @@ -1,4 +1,3 @@ -{% load bookwyrm_tags %} {% load i18n %} {% load utilities %} {% load status_display %} diff --git a/bookwyrm/templates/snippets/create_status/quotation.html b/bookwyrm/templates/snippets/create_status/quotation.html index cf472c11..a9ddb17f 100644 --- a/bookwyrm/templates/snippets/create_status/quotation.html +++ b/bookwyrm/templates/snippets/create_status/quotation.html @@ -1,5 +1,4 @@ {% extends "snippets/create_status/layout.html" %} -{% load bookwyrm_tags %} {% load utilities %} {% load status_display %} {% load i18n %} diff --git a/bookwyrm/templates/snippets/create_status/review.html b/bookwyrm/templates/snippets/create_status/review.html index 13d349ca..e52a7c1c 100644 --- a/bookwyrm/templates/snippets/create_status/review.html +++ b/bookwyrm/templates/snippets/create_status/review.html @@ -1,5 +1,4 @@ {% extends "snippets/create_status/layout.html" %} -{% load bookwyrm_tags %} {% load utilities %} {% load status_display %} {% load i18n %} diff --git a/bookwyrm/templates/snippets/status/content_status.html b/bookwyrm/templates/snippets/status/content_status.html index 6d9b9263..fed32584 100644 --- a/bookwyrm/templates/snippets/status/content_status.html +++ b/bookwyrm/templates/snippets/status/content_status.html @@ -1,4 +1,4 @@ -{% load bookwyrm_tags %} +{% load book_display_tags %} {% load rating_tags %} {% load markdown %} {% load i18n %} diff --git a/bookwyrm/templates/snippets/status/generated_status.html b/bookwyrm/templates/snippets/status/generated_status.html index 1234ae7c..f9175144 100644 --- a/bookwyrm/templates/snippets/status/generated_status.html +++ b/bookwyrm/templates/snippets/status/generated_status.html @@ -1,6 +1,6 @@ {% spaceless %} -{% load bookwyrm_tags %} +{% load book_display_tags %} {% load markdown %} {% load i18n %} {% load cache %} diff --git a/bookwyrm/templates/user/user_preview.html b/bookwyrm/templates/user/user_preview.html index c46563e5..23dd3ab5 100755 --- a/bookwyrm/templates/user/user_preview.html +++ b/bookwyrm/templates/user/user_preview.html @@ -1,7 +1,7 @@ {% load i18n %} {% load humanize %} {% load utilities %} -{% load bookwyrm_tags %} +{% load user_page_tags %}
diff --git a/bookwyrm/templatetags/book_display_tags.py b/bookwyrm/templatetags/book_display_tags.py new file mode 100644 index 00000000..9db79f8e --- /dev/null +++ b/bookwyrm/templatetags/book_display_tags.py @@ -0,0 +1,17 @@ +""" template filters """ +from django import template + + +register = template.Library() + + +@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.simple_tag(takes_context=False) +def get_book_file_links(book): + """links for a book""" + return book.file_links.filter(domain__status="approved") diff --git a/bookwyrm/templatetags/feed_page_tags.py b/bookwyrm/templatetags/feed_page_tags.py new file mode 100644 index 00000000..3d346b9a --- /dev/null +++ b/bookwyrm/templatetags/feed_page_tags.py @@ -0,0 +1,28 @@ +""" tags used on the feed pages """ +from django import template +from bookwyrm.views.feed import get_suggested_books + + +register = template.Library() + + +@register.filter(name="load_subclass") +def load_subclass(status): + """sometimes you didn't select_subclass""" + if hasattr(status, "quotation"): + return status.quotation + if hasattr(status, "review"): + return status.review + if hasattr(status, "comment"): + return status.comment + if hasattr(status, "generatednote"): + return status.generatednote + return status + + +@register.simple_tag(takes_context=True) +def suggested_books(context): + """get books for suggested books panel""" + # this happens here instead of in the view so that the template snippet can + # be cached in the template + return get_suggested_books(context["request"].user) diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/landing_page_tags.py similarity index 59% rename from bookwyrm/templatetags/bookwyrm_tags.py rename to bookwyrm/templatetags/landing_page_tags.py index ba6f7c2f..e7d94360 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/landing_page_tags.py @@ -3,32 +3,10 @@ from django import template from django.db.models import Avg, StdDev, Count, F, Q from bookwyrm import models -from bookwyrm.views.feed import get_suggested_books - register = template.Library() -@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="load_subclass") -def load_subclass(status): - """sometimes you didn't select_subclass""" - if hasattr(status, "quotation"): - return status.quotation - if hasattr(status, "review"): - return status.review - if hasattr(status, "comment"): - return status.comment - if hasattr(status, "generatednote"): - return status.generatednote - return status - - @register.simple_tag(takes_context=False) def get_book_superlatives(): """get book stats for the about page""" @@ -80,14 +58,6 @@ def get_book_superlatives(): return data -@register.simple_tag(takes_context=False) -def related_status(notification): - """for notifications""" - if not notification.related_status: - return None - return load_subclass(notification.related_status) - - @register.simple_tag(takes_context=False) def get_landing_books(): """list of books for the landing page""" @@ -104,26 +74,3 @@ def get_landing_books(): .order_by("-review__published_date")[:6] ) ) - - -@register.simple_tag(takes_context=True) -def mutuals_count(context, user): - """how many users that you follow, follow them""" - viewer = context["request"].user - if not viewer.is_authenticated: - return None - return user.followers.filter(followers=viewer).count() - - -@register.simple_tag(takes_context=True) -def suggested_books(context): - """get books for suggested books panel""" - # this happens here instead of in the view so that the template snippet can - # be cached in the template - return get_suggested_books(context["request"].user) - - -@register.simple_tag(takes_context=False) -def get_book_file_links(book): - """links for a book""" - return book.file_links.filter(domain__status="approved") diff --git a/bookwyrm/templatetags/notification_page_tags.py b/bookwyrm/templatetags/notification_page_tags.py new file mode 100644 index 00000000..28fa2afb --- /dev/null +++ b/bookwyrm/templatetags/notification_page_tags.py @@ -0,0 +1,14 @@ +""" tags used on the feed pages """ +from django import template +from bookwyrm.templatetags.feed_page_tags import load_subclass + + +register = template.Library() + + +@register.simple_tag(takes_context=False) +def related_status(notification): + """for notifications""" + if not notification.related_status: + return None + return load_subclass(notification.related_status) diff --git a/bookwyrm/templatetags/user_page_tags.py b/bookwyrm/templatetags/user_page_tags.py new file mode 100644 index 00000000..b3a82597 --- /dev/null +++ b/bookwyrm/templatetags/user_page_tags.py @@ -0,0 +1,14 @@ +""" template filters """ +from django import template + + +register = template.Library() + + +@register.simple_tag(takes_context=True) +def mutuals_count(context, user): + """how many users that you follow, follow them""" + viewer = context["request"].user + if not viewer.is_authenticated: + return None + return user.followers.filter(followers=viewer).count() diff --git a/bookwyrm/tests/templatetags/test_book_display_tags.py b/bookwyrm/tests/templatetags/test_book_display_tags.py new file mode 100644 index 00000000..36fdbcd0 --- /dev/null +++ b/bookwyrm/tests/templatetags/test_book_display_tags.py @@ -0,0 +1,51 @@ +""" style fixes and lookups for templates """ +from unittest.mock import patch + +from django.test import TestCase + +from bookwyrm import models +from bookwyrm.templatetags import bookwyrm_tags + + +@patch("bookwyrm.activitystreams.add_status_task.delay") +@patch("bookwyrm.activitystreams.remove_status_task.delay") +class BookWyrmTags(TestCase): + """lotta different things here""" + + def setUp(self): + """create some filler objects""" + with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( + "bookwyrm.activitystreams.populate_stream_task.delay" + ), patch("bookwyrm.lists_stream.populate_lists_task.delay"): + self.user = models.User.objects.create_user( + "mouse@example.com", + "mouse@mouse.mouse", + "mouseword", + local=True, + localname="mouse", + ) + with patch("bookwyrm.models.user.set_remote_server.delay"): + self.remote_user = models.User.objects.create_user( + "rat", + "rat@rat.rat", + "ratword", + remote_id="http://example.com/rat", + local=False, + ) + self.book = models.Edition.objects.create(title="Test Book") + + def test_get_book_description(self, *_): + """grab it from the edition or the parent""" + work = models.Work.objects.create(title="Test Work") + self.book.parent_work = work + self.book.save() + + self.assertIsNone(bookwyrm_tags.get_book_description(self.book)) + + work.description = "hi" + work.save() + self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hi") + + self.book.description = "hello" + self.book.save() + self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hello") diff --git a/bookwyrm/tests/templatetags/test_bookwyrm_tags.py b/bookwyrm/tests/templatetags/test_feed_page_tags.py similarity index 61% rename from bookwyrm/tests/templatetags/test_bookwyrm_tags.py rename to bookwyrm/tests/templatetags/test_feed_page_tags.py index d44d9784..ba4701a8 100644 --- a/bookwyrm/tests/templatetags/test_bookwyrm_tags.py +++ b/bookwyrm/tests/templatetags/test_feed_page_tags.py @@ -24,30 +24,8 @@ class BookWyrmTags(TestCase): local=True, localname="mouse", ) - with patch("bookwyrm.models.user.set_remote_server.delay"): - self.remote_user = models.User.objects.create_user( - "rat", - "rat@rat.rat", - "ratword", - remote_id="http://example.com/rat", - local=False, - ) self.book = models.Edition.objects.create(title="Test Book") - def test_get_book_description(self, *_): - """grab it from the edition or the parent""" - work = models.Work.objects.create(title="Test Work") - self.book.parent_work = work - self.book.save() - - self.assertIsNone(bookwyrm_tags.get_book_description(self.book)) - - work.description = "hi" - work.save() - self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hi") - - self.book.description = "hello" - self.book.save() self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hello") @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") @@ -71,14 +49,3 @@ class BookWyrmTags(TestCase): status = models.Status.objects.get(id=comment.id) self.assertIsInstance(status, models.Status) self.assertIsInstance(bookwyrm_tags.load_subclass(status), models.Comment) - - def test_related_status(self, *_): - """gets the subclass model for a notification status""" - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - status = models.Status.objects.create(content="hi", user=self.user) - notification = models.Notification.objects.create( - user=self.user, notification_type="MENTION", related_status=status - ) - - result = bookwyrm_tags.related_status(notification) - self.assertIsInstance(result, models.Status) diff --git a/bookwyrm/tests/templatetags/test_notification_page_tags.py b/bookwyrm/tests/templatetags/test_notification_page_tags.py new file mode 100644 index 00000000..11caaeb1 --- /dev/null +++ b/bookwyrm/tests/templatetags/test_notification_page_tags.py @@ -0,0 +1,38 @@ +""" style fixes and lookups for templates """ +from unittest.mock import patch + +from django.test import TestCase + +from bookwyrm import models +from bookwyrm.templatetags import bookwyrm_tags + + +@patch("bookwyrm.activitystreams.add_status_task.delay") +@patch("bookwyrm.activitystreams.remove_status_task.delay") +class BookWyrmTags(TestCase): + """lotta different things here""" + + def setUp(self): + """create some filler objects""" + with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( + "bookwyrm.activitystreams.populate_stream_task.delay" + ), patch("bookwyrm.lists_stream.populate_lists_task.delay"): + self.user = models.User.objects.create_user( + "mouse@example.com", + "mouse@mouse.mouse", + "mouseword", + local=True, + localname="mouse", + ) + + + def test_related_status(self, *_): + """gets the subclass model for a notification status""" + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): + status = models.Status.objects.create(content="hi", user=self.user) + notification = models.Notification.objects.create( + user=self.user, notification_type="MENTION", related_status=status + ) + + result = bookwyrm_tags.related_status(notification) + self.assertIsInstance(result, models.Status) From 95c262c49573d58b6a51f548d5492a0018814ed8 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 18 Jan 2022 12:30:46 -0800 Subject: [PATCH 07/13] Renames group tags --- bookwyrm/templates/groups/group.html | 2 +- bookwyrm/templates/groups/layout.html | 2 +- bookwyrm/templates/groups/members.html | 2 +- bookwyrm/templates/lists/embed-list.html | 2 +- bookwyrm/templates/lists/list.html | 2 +- bookwyrm/templates/snippets/add_to_group_button.html | 3 ++- bookwyrm/templates/snippets/join_invitation_buttons.html | 3 ++- bookwyrm/templates/snippets/remove_from_group_button.html | 3 ++- bookwyrm/templates/user/layout.html | 2 +- .../templatetags/{bookwyrm_group_tags.py => group_tags.py} | 0 bookwyrm/tests/templatetags/test_notification_page_tags.py | 1 - 11 files changed, 12 insertions(+), 10 deletions(-) rename bookwyrm/templatetags/{bookwyrm_group_tags.py => group_tags.py} (100%) diff --git a/bookwyrm/templates/groups/group.html b/bookwyrm/templates/groups/group.html index 88f11420..5f5b5860 100644 --- a/bookwyrm/templates/groups/group.html +++ b/bookwyrm/templates/groups/group.html @@ -1,6 +1,6 @@ {% extends 'groups/layout.html' %} {% load i18n %} -{% load bookwyrm_group_tags %} +{% load group_tags %} {% load markdown %} {% block panel %} diff --git a/bookwyrm/templates/groups/layout.html b/bookwyrm/templates/groups/layout.html index a25c1085..e688bc77 100644 --- a/bookwyrm/templates/groups/layout.html +++ b/bookwyrm/templates/groups/layout.html @@ -1,6 +1,6 @@ {% extends 'layout.html' %} {% load i18n %} -{% load bookwyrm_group_tags %} +{% load group_tags %} {% block title %}{{ group.name }}{% endblock %} diff --git a/bookwyrm/templates/groups/members.html b/bookwyrm/templates/groups/members.html index 1d1ac314..90236843 100644 --- a/bookwyrm/templates/groups/members.html +++ b/bookwyrm/templates/groups/members.html @@ -1,7 +1,7 @@ {% load i18n %} {% load utilities %} {% load humanize %} -{% load bookwyrm_group_tags %} +{% load group_tags %}

Group Members

{% if group.user == request.user %} diff --git a/bookwyrm/templates/lists/embed-list.html b/bookwyrm/templates/lists/embed-list.html index 9c07291c..ec4a9162 100644 --- a/bookwyrm/templates/lists/embed-list.html +++ b/bookwyrm/templates/lists/embed-list.html @@ -1,7 +1,7 @@ {% extends 'embed-layout.html' %} {% load i18n %} {% load rating_tags %} -{% load bookwyrm_group_tags %} +{% load group_tags %} {% load markdown %} {% block title %}{% blocktrans with list_name=list.name owner=list.user.display_name %}{{ list_name }}, a list by {{owner}}{% endblocktrans %}{% endblock title %} diff --git a/bookwyrm/templates/lists/list.html b/bookwyrm/templates/lists/list.html index eb351df3..b77f935c 100644 --- a/bookwyrm/templates/lists/list.html +++ b/bookwyrm/templates/lists/list.html @@ -1,7 +1,7 @@ {% extends 'lists/layout.html' %} {% load i18n %} {% load rating_tags %} -{% load bookwyrm_group_tags %} +{% load group_tags %} {% load markdown %} {% block breadcrumbs %} diff --git a/bookwyrm/templates/snippets/add_to_group_button.html b/bookwyrm/templates/snippets/add_to_group_button.html index 2785d7c0..20194623 100644 --- a/bookwyrm/templates/snippets/add_to_group_button.html +++ b/bookwyrm/templates/snippets/add_to_group_button.html @@ -1,5 +1,6 @@ {% load i18n %} -{% load bookwyrm_group_tags %} +{% load group_tags %} + {% if request.user == user or not request.user == group.user or not request.user.is_authenticated %} {% elif user in request.user.blocks.all %} {% include 'snippets/block_button.html' with blocks=True %} diff --git a/bookwyrm/templates/snippets/join_invitation_buttons.html b/bookwyrm/templates/snippets/join_invitation_buttons.html index 46c4071d..b77ce43c 100644 --- a/bookwyrm/templates/snippets/join_invitation_buttons.html +++ b/bookwyrm/templates/snippets/join_invitation_buttons.html @@ -1,5 +1,6 @@ {% load i18n %} -{% load bookwyrm_group_tags %} +{% load group_tags %} + {% if group|is_invited:request.user %}
diff --git a/bookwyrm/templates/snippets/remove_from_group_button.html b/bookwyrm/templates/snippets/remove_from_group_button.html index 1672e038..2e08760f 100644 --- a/bookwyrm/templates/snippets/remove_from_group_button.html +++ b/bookwyrm/templates/snippets/remove_from_group_button.html @@ -1,5 +1,6 @@ {% load i18n %} -{% load bookwyrm_group_tags %} +{% load group_tags %} + {% if request.user == user or not request.user == group.user or not request.user.is_authenticated %} {% else %} {% if user in request.user.blocks.all %} diff --git a/bookwyrm/templates/user/layout.html b/bookwyrm/templates/user/layout.html index 03e3dfce..65b6a9ac 100755 --- a/bookwyrm/templates/user/layout.html +++ b/bookwyrm/templates/user/layout.html @@ -4,7 +4,7 @@ {% load utilities %} {% load markdown %} {% load layout %} -{% load bookwyrm_group_tags %} +{% load group_tags %} {% block title %}{{ user.display_name }}{% endblock %} diff --git a/bookwyrm/templatetags/bookwyrm_group_tags.py b/bookwyrm/templatetags/group_tags.py similarity index 100% rename from bookwyrm/templatetags/bookwyrm_group_tags.py rename to bookwyrm/templatetags/group_tags.py diff --git a/bookwyrm/tests/templatetags/test_notification_page_tags.py b/bookwyrm/tests/templatetags/test_notification_page_tags.py index 11caaeb1..7853f4d1 100644 --- a/bookwyrm/tests/templatetags/test_notification_page_tags.py +++ b/bookwyrm/tests/templatetags/test_notification_page_tags.py @@ -25,7 +25,6 @@ class BookWyrmTags(TestCase): localname="mouse", ) - def test_related_status(self, *_): """gets the subclass model for a notification status""" with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): From ce0a49f64f981124f838adb8f23f4622ea247a9f Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 18 Jan 2022 12:52:21 -0800 Subject: [PATCH 08/13] Updates ratings and book display tests --- bookwyrm/templatetags/rating_tags.py | 2 +- .../templatetags/test_book_display_tags.py | 37 ++++++++----- .../tests/templatetags/test_rating_tags.py | 54 ++++++++++++++++--- 3 files changed, 73 insertions(+), 20 deletions(-) diff --git a/bookwyrm/templatetags/rating_tags.py b/bookwyrm/templatetags/rating_tags.py index b986f6b8..670599e2 100644 --- a/bookwyrm/templatetags/rating_tags.py +++ b/bookwyrm/templatetags/rating_tags.py @@ -15,7 +15,7 @@ def get_rating(book, user): return cache.get_or_set( f"book-rating-{book.parent_work.id}-{user.id}", lambda u, b: models.Review.privacy_filter(u) - .filter(book__parent_work__editions=b) + .filter(book__parent_work__editions=b, rating__gt=0) .aggregate(Avg("rating"))["rating__avg"] or 0, user, diff --git a/bookwyrm/tests/templatetags/test_book_display_tags.py b/bookwyrm/tests/templatetags/test_book_display_tags.py index 36fdbcd0..54ae8806 100644 --- a/bookwyrm/tests/templatetags/test_book_display_tags.py +++ b/bookwyrm/tests/templatetags/test_book_display_tags.py @@ -4,12 +4,13 @@ from unittest.mock import patch from django.test import TestCase from bookwyrm import models -from bookwyrm.templatetags import bookwyrm_tags +from bookwyrm.templatetags import book_display_tags @patch("bookwyrm.activitystreams.add_status_task.delay") @patch("bookwyrm.activitystreams.remove_status_task.delay") -class BookWyrmTags(TestCase): +@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") +class BookDisplayTags(TestCase): """lotta different things here""" def setUp(self): @@ -24,14 +25,6 @@ class BookWyrmTags(TestCase): local=True, localname="mouse", ) - with patch("bookwyrm.models.user.set_remote_server.delay"): - self.remote_user = models.User.objects.create_user( - "rat", - "rat@rat.rat", - "ratword", - remote_id="http://example.com/rat", - local=False, - ) self.book = models.Edition.objects.create(title="Test Book") def test_get_book_description(self, *_): @@ -40,12 +33,30 @@ class BookWyrmTags(TestCase): self.book.parent_work = work self.book.save() - self.assertIsNone(bookwyrm_tags.get_book_description(self.book)) + self.assertIsNone(book_display_tags.get_book_description(self.book)) work.description = "hi" work.save() - self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hi") + self.assertEqual(book_display_tags.get_book_description(self.book), "hi") self.book.description = "hello" self.book.save() - self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hello") + self.assertEqual(book_display_tags.get_book_description(self.book), "hello") + + def test_get_book_file_links(self, *_): + """load approved links""" + link = models.FileLink.objects.create( + book=self.book, + url="https://web.site/hello", + ) + links = book_display_tags.get_book_file_links(self.book) + # the link is pending + self.assertFalse(links.exists()) + + domain = link.domain + domain.status = "approved" + domain.save() + + links = book_display_tags.get_book_file_links(self.book) + self.assertTrue(links.exists()) + self.assertEqual(links[0], link) diff --git a/bookwyrm/tests/templatetags/test_rating_tags.py b/bookwyrm/tests/templatetags/test_rating_tags.py index fad864ee..52fc44d5 100644 --- a/bookwyrm/tests/templatetags/test_rating_tags.py +++ b/bookwyrm/tests/templatetags/test_rating_tags.py @@ -4,7 +4,7 @@ from unittest.mock import patch from django.test import TestCase from bookwyrm import models -from bookwyrm.templatetags import bookwyrm_tags +from bookwyrm.templatetags import rating_tags @patch("bookwyrm.activitystreams.add_status_task.delay") @@ -17,7 +17,7 @@ class RatingTags(TestCase): with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( "bookwyrm.activitystreams.populate_stream_task.delay" ), patch("bookwyrm.lists_stream.populate_lists_task.delay"): - self.user = models.User.objects.create_user( + self.local_user = models.User.objects.create_user( "mouse@example.com", "mouse@mouse.mouse", "mouseword", @@ -32,14 +32,56 @@ class RatingTags(TestCase): remote_id="http://example.com/rat", local=False, ) - self.book = models.Edition.objects.create(title="Test Book") + work = models.Work.objects.create(title="Work title") + self.book = models.Edition.objects.create( + title="Test Book", + parent_work=work, + ) + + @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") + def test_get_rating(self, *_): + """privacy filtered rating""" + # follows-only: not included + models.ReviewRating.objects.create( + user=self.remote_user, + rating=5, + book=self.book, + privacy="followers", + ) + self.assertEqual( + rating_tags.get_rating(self.book, self.local_user), 0 + ) + + # public: included + models.ReviewRating.objects.create( + user=self.remote_user, + rating=5, + book=self.book, + privacy="public", + ) + self.assertEqual( + rating_tags.get_rating(self.book, self.local_user), 5 + ) + + # rating unset: not included + models.Review.objects.create( + name="blah", + user=self.local_user, + rating=0, + book=self.book, + privacy="public", + ) + self.assertEqual( + rating_tags.get_rating(self.book, self.local_user), 5 + ) + def test_get_user_rating(self, *_): """get a user's most recent rating of a book""" with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - models.Review.objects.create(user=self.user, book=self.book, rating=3) - self.assertEqual(bookwyrm_tags.get_user_rating(self.book, self.user), 3) + models.Review.objects.create(user=self.local_user, book=self.book, rating=3) + self.assertEqual(rating_tags.get_user_rating(self.book, self.local_user), 3) def test_get_user_rating_doesnt_exist(self, *_): """there is no rating available""" - self.assertEqual(bookwyrm_tags.get_user_rating(self.book, self.user), 0) + self.assertEqual(rating_tags.get_user_rating(self.book, self.local_user), 0) From ffaeb3381a2401086ec71a925537a1473cd08c7c Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 18 Jan 2022 12:53:35 -0800 Subject: [PATCH 09/13] Updates tests --- bookwyrm/tests/templatetags/test_feed_page_tags.py | 10 +++++----- .../tests/templatetags/test_notification_page_tags.py | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bookwyrm/tests/templatetags/test_feed_page_tags.py b/bookwyrm/tests/templatetags/test_feed_page_tags.py index ba4701a8..388b2e16 100644 --- a/bookwyrm/tests/templatetags/test_feed_page_tags.py +++ b/bookwyrm/tests/templatetags/test_feed_page_tags.py @@ -4,7 +4,7 @@ from unittest.mock import patch from django.test import TestCase from bookwyrm import models -from bookwyrm.templatetags import bookwyrm_tags +from bookwyrm.templatetags import feed_page_tags @patch("bookwyrm.activitystreams.add_status_task.delay") @@ -26,7 +26,7 @@ class BookWyrmTags(TestCase): ) self.book = models.Edition.objects.create(title="Test Book") - self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hello") + self.assertEqual(feed_page_tags.get_book_description(self.book), "hello") @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") def test_load_subclass(self, *_): @@ -34,18 +34,18 @@ class BookWyrmTags(TestCase): review = models.Review.objects.create(user=self.user, book=self.book, rating=3) status = models.Status.objects.get(id=review.id) self.assertIsInstance(status, models.Status) - self.assertIsInstance(bookwyrm_tags.load_subclass(status), models.Review) + self.assertIsInstance(feed_page_tags.load_subclass(status), models.Review) quote = models.Quotation.objects.create( user=self.user, book=self.book, content="hi" ) status = models.Status.objects.get(id=quote.id) self.assertIsInstance(status, models.Status) - self.assertIsInstance(bookwyrm_tags.load_subclass(status), models.Quotation) + self.assertIsInstance(feed_page_tags.load_subclass(status), models.Quotation) comment = models.Comment.objects.create( user=self.user, book=self.book, content="hi" ) status = models.Status.objects.get(id=comment.id) self.assertIsInstance(status, models.Status) - self.assertIsInstance(bookwyrm_tags.load_subclass(status), models.Comment) + self.assertIsInstance(feed_page_tags.load_subclass(status), models.Comment) diff --git a/bookwyrm/tests/templatetags/test_notification_page_tags.py b/bookwyrm/tests/templatetags/test_notification_page_tags.py index 7853f4d1..3603febd 100644 --- a/bookwyrm/tests/templatetags/test_notification_page_tags.py +++ b/bookwyrm/tests/templatetags/test_notification_page_tags.py @@ -4,7 +4,7 @@ from unittest.mock import patch from django.test import TestCase from bookwyrm import models -from bookwyrm.templatetags import bookwyrm_tags +from bookwyrm.templatetags import notification_page_tags @patch("bookwyrm.activitystreams.add_status_task.delay") @@ -33,5 +33,5 @@ class BookWyrmTags(TestCase): user=self.user, notification_type="MENTION", related_status=status ) - result = bookwyrm_tags.related_status(notification) + result = notification_page_tags.related_status(notification) self.assertIsInstance(result, models.Status) From 3f487328e2031131ebd0ea5815b1f7aba90b99ef Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 18 Jan 2022 13:15:25 -0800 Subject: [PATCH 10/13] Adds more tests --- .../tests/templatetags/test_feed_page_tags.py | 2 +- .../test_notification_page_tags.py | 2 +- .../tests/templatetags/test_rating_tags.py | 13 +++------- .../tests/templatetags/test_status_display.py | 25 +++++++++++++++---- bookwyrm/tests/templatetags/test_utilities.py | 14 +++++++---- 5 files changed, 34 insertions(+), 22 deletions(-) diff --git a/bookwyrm/tests/templatetags/test_feed_page_tags.py b/bookwyrm/tests/templatetags/test_feed_page_tags.py index 388b2e16..f6777b77 100644 --- a/bookwyrm/tests/templatetags/test_feed_page_tags.py +++ b/bookwyrm/tests/templatetags/test_feed_page_tags.py @@ -9,7 +9,7 @@ from bookwyrm.templatetags import feed_page_tags @patch("bookwyrm.activitystreams.add_status_task.delay") @patch("bookwyrm.activitystreams.remove_status_task.delay") -class BookWyrmTags(TestCase): +class FeedPageTags(TestCase): """lotta different things here""" def setUp(self): diff --git a/bookwyrm/tests/templatetags/test_notification_page_tags.py b/bookwyrm/tests/templatetags/test_notification_page_tags.py index 3603febd..3c92181b 100644 --- a/bookwyrm/tests/templatetags/test_notification_page_tags.py +++ b/bookwyrm/tests/templatetags/test_notification_page_tags.py @@ -9,7 +9,7 @@ from bookwyrm.templatetags import notification_page_tags @patch("bookwyrm.activitystreams.add_status_task.delay") @patch("bookwyrm.activitystreams.remove_status_task.delay") -class BookWyrmTags(TestCase): +class NotificationPageTags(TestCase): """lotta different things here""" def setUp(self): diff --git a/bookwyrm/tests/templatetags/test_rating_tags.py b/bookwyrm/tests/templatetags/test_rating_tags.py index 52fc44d5..c00f2072 100644 --- a/bookwyrm/tests/templatetags/test_rating_tags.py +++ b/bookwyrm/tests/templatetags/test_rating_tags.py @@ -48,9 +48,7 @@ class RatingTags(TestCase): book=self.book, privacy="followers", ) - self.assertEqual( - rating_tags.get_rating(self.book, self.local_user), 0 - ) + self.assertEqual(rating_tags.get_rating(self.book, self.local_user), 0) # public: included models.ReviewRating.objects.create( @@ -59,9 +57,7 @@ class RatingTags(TestCase): book=self.book, privacy="public", ) - self.assertEqual( - rating_tags.get_rating(self.book, self.local_user), 5 - ) + self.assertEqual(rating_tags.get_rating(self.book, self.local_user), 5) # rating unset: not included models.Review.objects.create( @@ -71,10 +67,7 @@ class RatingTags(TestCase): book=self.book, privacy="public", ) - self.assertEqual( - rating_tags.get_rating(self.book, self.local_user), 5 - ) - + self.assertEqual(rating_tags.get_rating(self.book, self.local_user), 5) def test_get_user_rating(self, *_): """get a user's most recent rating of a book""" diff --git a/bookwyrm/tests/templatetags/test_status_display.py b/bookwyrm/tests/templatetags/test_status_display.py index 50c5571e..af2fc942 100644 --- a/bookwyrm/tests/templatetags/test_status_display.py +++ b/bookwyrm/tests/templatetags/test_status_display.py @@ -1,4 +1,5 @@ """ style fixes and lookups for templates """ +from datetime import datetime from unittest.mock import patch from django.test import TestCase @@ -35,6 +36,12 @@ class StatusDisplayTags(TestCase): ) self.book = models.Edition.objects.create(title="Test Book") + def test_get_mentions(self, *_): + """list of people mentioned""" + status = models.Status.objects.create(content="hi", user=self.remote_user) + result = status_display.get_mentions(status, self.user) + self.assertEqual(result, "@rat@example.com ") + @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") def test_get_replies(self, *_): """direct replies to a status""" @@ -83,8 +90,16 @@ class StatusDisplayTags(TestCase): self.assertIsInstance(boosted, models.Review) self.assertEqual(boosted, status) - def test_get_mentions(self, *_): - """list of people mentioned""" - status = models.Status.objects.create(content="hi", user=self.remote_user) - result = status_display.get_mentions(status, self.user) - self.assertEqual(result, "@rat@example.com ") + def test_get_published_date(self, *_): + """date formatting""" + date = datetime(2020, 1, 1, 0, 0, tzinfo=timezone.utc) + with patch("django.utils.timezone.now") as timezone_mock: + timezone_mock.return_value = datetime(2022, 1, 1, 0, 0, tzinfo=timezone.utc) + result = status_display.get_published_date(date) + self.assertEqual(result, "Jan. 1, 2020") + + date = datetime(2022, 1, 1, 0, 0, tzinfo=timezone.utc) + with patch("django.utils.timezone.now") as timezone_mock: + timezone_mock.return_value = datetime(2022, 1, 8, 0, 0, tzinfo=timezone.utc) + result = status_display.get_published_date(date) + self.assertEqual(result, "Jan 1") diff --git a/bookwyrm/tests/templatetags/test_utilities.py b/bookwyrm/tests/templatetags/test_utilities.py index e41cd21a..0136ca8c 100644 --- a/bookwyrm/tests/templatetags/test_utilities.py +++ b/bookwyrm/tests/templatetags/test_utilities.py @@ -35,6 +35,15 @@ class UtilitiesTags(TestCase): ) self.book = models.Edition.objects.create(title="Test Book") + def test_get_uuid(self, *_): + """uuid functionality""" + uuid = utilities.get_uuid("hi") + self.assertTrue(re.match(r"hi[A-Za-z0-9\-]", uuid)) + + def test_join(self, *_): + """concats things with underscores""" + self.assertEqual(utilities.join("hi", 5, "blah", 0.75), "hi_5_blah_0.75") + def test_get_user_identifer_local(self, *_): """fall back to the simplest uid available""" self.assertNotEqual(self.user.username, self.user.localname) @@ -46,11 +55,6 @@ class UtilitiesTags(TestCase): utilities.get_user_identifier(self.remote_user), "rat@example.com" ) - def test_get_uuid(self, *_): - """uuid functionality""" - uuid = utilities.get_uuid("hi") - self.assertTrue(re.match(r"hi[A-Za-z0-9\-]", uuid)) - def test_get_title(self, *_): """the title of a book""" self.assertEqual(utilities.get_title(None), "") From c888895327ba1c616bdc05189ffdfaceb4c6fe01 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 18 Jan 2022 14:04:55 -0800 Subject: [PATCH 11/13] Adds missing tag loaders --- bookwyrm/templates/landing/large-book.html | 1 + bookwyrm/templates/lists/list.html | 1 + 2 files changed, 2 insertions(+) diff --git a/bookwyrm/templates/landing/large-book.html b/bookwyrm/templates/landing/large-book.html index 6a27a4a3..9b4fd1f9 100644 --- a/bookwyrm/templates/landing/large-book.html +++ b/bookwyrm/templates/landing/large-book.html @@ -1,3 +1,4 @@ +{% load book_display_tags %} {% load rating_tags %} {% load markdown %} {% load i18n %} diff --git a/bookwyrm/templates/lists/list.html b/bookwyrm/templates/lists/list.html index b77f935c..c44d3fe3 100644 --- a/bookwyrm/templates/lists/list.html +++ b/bookwyrm/templates/lists/list.html @@ -1,6 +1,7 @@ {% extends 'lists/layout.html' %} {% load i18n %} {% load rating_tags %} +{% load book_display_tags %} {% load group_tags %} {% load markdown %} From 93caa95855bdcdc96dfc609240cebab26cc1a77b Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 18 Jan 2022 14:05:35 -0800 Subject: [PATCH 12/13] Removes stray asssert line --- bookwyrm/tests/templatetags/test_feed_page_tags.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bookwyrm/tests/templatetags/test_feed_page_tags.py b/bookwyrm/tests/templatetags/test_feed_page_tags.py index f6777b77..47c51891 100644 --- a/bookwyrm/tests/templatetags/test_feed_page_tags.py +++ b/bookwyrm/tests/templatetags/test_feed_page_tags.py @@ -26,7 +26,6 @@ class FeedPageTags(TestCase): ) self.book = models.Edition.objects.create(title="Test Book") - self.assertEqual(feed_page_tags.get_book_description(self.book), "hello") @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") def test_load_subclass(self, *_): From 8b8314f73643554a5d58bca743f3b978cb7963fb Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 18 Jan 2022 14:27:19 -0800 Subject: [PATCH 13/13] Fixes embed view --- bookwyrm/templates/lists/embed-list.html | 1 + bookwyrm/tests/templatetags/test_feed_page_tags.py | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/templates/lists/embed-list.html b/bookwyrm/templates/lists/embed-list.html index ec4a9162..18668167 100644 --- a/bookwyrm/templates/lists/embed-list.html +++ b/bookwyrm/templates/lists/embed-list.html @@ -1,5 +1,6 @@ {% extends 'embed-layout.html' %} {% load i18n %} +{% load book_display_tags %} {% load rating_tags %} {% load group_tags %} {% load markdown %} diff --git a/bookwyrm/tests/templatetags/test_feed_page_tags.py b/bookwyrm/tests/templatetags/test_feed_page_tags.py index 47c51891..5e5dc235 100644 --- a/bookwyrm/tests/templatetags/test_feed_page_tags.py +++ b/bookwyrm/tests/templatetags/test_feed_page_tags.py @@ -26,7 +26,6 @@ class FeedPageTags(TestCase): ) self.book = models.Edition.objects.create(title="Test Book") - @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") def test_load_subclass(self, *_): """get a status' real type"""