Moves remaining tags into new files

This commit is contained in:
Mouse Reeve 2022-01-18 12:28:59 -08:00
parent 3d63509e20
commit 33c6e5aabe
24 changed files with 173 additions and 102 deletions

View file

@ -2,7 +2,7 @@
{% load humanize %}
{% load i18n %}
{% load utilities %}
{% load bookwyrm_tags %}
{% load landing_page_tags %}
{% load cache %}
{% block title %}

View file

@ -1,6 +1,6 @@
{% extends 'layout.html' %}
{% load i18n %}
{% load bookwyrm_tags %}
{% load book_display_tags %}
{% load humanize %}
{% load utilities %}
{% load static %}

View file

@ -1,5 +1,5 @@
{% load i18n %}
{% load bookwyrm_tags %}
{% load book_display_tags %}
{% load utilities %}
{% get_book_file_links book as links %}

View file

@ -1,4 +1,4 @@
{% load bookwyrm_tags %}
{% load landing_page_tags %}
{% load utilities %}
{% load i18n %}
{% load status_display %}

View file

@ -1,6 +1,6 @@
{% extends 'feed/layout.html' %}
{% load feed_page_tags %}
{% load i18n %}
{% load bookwyrm_tags %}
{% block opengraph_images %}

View file

@ -1,5 +1,5 @@
{% load i18n %}
{% load bookwyrm_tags %}
{% load feed_page_tags %}
{% suggested_books as suggested_books %}
<section class="block">

View file

@ -1,6 +1,5 @@
{% extends 'groups/layout.html' %}
{% load i18n %}
{% load bookwyrm_tags %}
{% load bookwyrm_group_tags %}
{% load markdown %}

View file

@ -1,7 +1,6 @@
{% load i18n %}
{% load utilities %}
{% load humanize %}
{% load bookwyrm_tags %}
{% load bookwyrm_group_tags %}
<h2 class="title is-5">Group Members</h2>

View file

@ -1,7 +1,7 @@
{% extends 'landing/layout.html' %}
{% load i18n %}
{% load cache %}
{% load bookwyrm_tags %}
{% load landing_page_tags %}
{% block panel %}

View file

@ -1,4 +1,4 @@
{% load bookwyrm_tags %}
{% load notification_page_tags %}
{% related_status notification as related_status %}
<div class="notification {% if notification.id in unread %}has-background-primary{% endif %}">
<div class="columns is-mobile {% if notification.id in unread %}has-text-white{% else %}has-text-grey{% endif %}">

View file

@ -1,4 +1,3 @@
{% load bookwyrm_tags %}
{% load i18n %}
{% load utilities %}
{% load status_display %}

View file

@ -1,5 +1,4 @@
{% extends "snippets/create_status/layout.html" %}
{% load bookwyrm_tags %}
{% load utilities %}
{% load status_display %}
{% load i18n %}

View file

@ -1,5 +1,4 @@
{% extends "snippets/create_status/layout.html" %}
{% load bookwyrm_tags %}
{% load utilities %}
{% load status_display %}
{% load i18n %}

View file

@ -1,4 +1,4 @@
{% load bookwyrm_tags %}
{% load book_display_tags %}
{% load rating_tags %}
{% load markdown %}
{% load i18n %}

View file

@ -1,6 +1,6 @@
{% spaceless %}
{% load bookwyrm_tags %}
{% load book_display_tags %}
{% load markdown %}
{% load i18n %}
{% load cache %}

View file

@ -1,7 +1,7 @@
{% load i18n %}
{% load humanize %}
{% load utilities %}
{% load bookwyrm_tags %}
{% load user_page_tags %}
<div class="media block">
<div class="media-left">

View file

@ -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")

View file

@ -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)

View file

@ -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")

View file

@ -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)

View file

@ -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()

View file

@ -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")

View file

@ -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)

View file

@ -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)