Merge pull request #443 from mouse-reeve/template-bugs

Template bugs and fixes
This commit is contained in:
Mouse Reeve 2020-12-31 07:41:21 -08:00 committed by GitHub
commit dd84ed7ebc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 71 additions and 46 deletions

View file

@ -35,6 +35,11 @@ class BookWyrmModel(models.Model):
''' this is just here to provide default fields for other models ''' ''' this is just here to provide default fields for other models '''
abstract = True abstract = True
@property
def local_path(self):
''' how to link to this object in the local app '''
return self.get_remote_id().replace('https://%s' % DOMAIN, '')
@receiver(models.signals.post_save) @receiver(models.signals.post_save)
#pylint: disable=unused-argument #pylint: disable=unused-argument

View file

@ -179,6 +179,11 @@ class User(OrderedCollectionPageMixin, AbstractUser):
return super().save(*args, **kwargs) return super().save(*args, **kwargs)
@property
def local_path(self):
''' this model doesn't inherit bookwyrm model, so here we are '''
return '/user/%s' % (self.localname or self.username)
class KeyPair(ActivitypubMixin, BookWyrmModel): class KeyPair(ActivitypubMixin, BookWyrmModel):
''' public and private keys for a user ''' ''' public and private keys for a user '''

View file

@ -8,7 +8,7 @@
</div> </div>
{% if request.user.is_authenticated and perms.bookwyrm.edit_book %} {% if request.user.is_authenticated and perms.bookwyrm.edit_book %}
<div class="column is-narrow"> <div class="column is-narrow">
<a href="/author/{{ author.id }}/edit"> <a href="{{ author.local_path }}/edit">
<span class="icon icon-pencil"> <span class="icon icon-pencil">
<span class="is-sr-only">Edit Author</span> <span class="is-sr-only">Edit Author</span>
</span> </span>

View file

@ -166,10 +166,10 @@
{% for rating in ratings %} {% for rating in ratings %}
<div class="column"> <div class="column">
<div class="media"> <div class="media">
<div class="media-left">{% include 'snippets/avatar.html' %}</div> <div class="media-left">{% include 'snippets/avatar.html' with user=rating.user %}</div>
<div class="media-content"> <div class="media-content">
<div> <div>
{% include 'snippets/username.html' %} {% include 'snippets/username.html' with user=rating.user %}
</div> </div>
<div class="field is-grouped mb-0"> <div class="field is-grouped mb-0">
<div>rated it</div> <div>rated it</div>

View file

@ -31,7 +31,7 @@
</div> </div>
{% endfor %} {% endfor %}
{% if not following.count %} {% if not following.count %}
<div>No one is following {{ user|username }}</div> <div>{{ user|username }} isn't following any users</div>
{% endif %} {% endif %}
</div> </div>

View file

@ -22,16 +22,16 @@
{% include 'snippets/username.html' with user=notification.related_user %} {% include 'snippets/username.html' with user=notification.related_user %}
{% if notification.notification_type == 'FAVORITE' %} {% if notification.notification_type == 'FAVORITE' %}
favorited your favorited your
<a href="{{ notification.related_status.remote_id}}">status</a> <a href="{{ notification.related_status.local_path }}">status</a>
{% elif notification.notification_type == 'MENTION' %} {% elif notification.notification_type == 'MENTION' %}
mentioned you in a mentioned you in a
<a href="{{ notification.related_status.remote_id}}">status</a> <a href="{{ notification.related_status.local_path }}">status</a>
{% elif notification.notification_type == 'REPLY' %} {% elif notification.notification_type == 'REPLY' %}
<a href="{{ notification.related_status.remote_id}}">replied</a> <a href="{{ notification.related_status.local_path }}">replied</a>
to your to your
<a href="{{ notification.related_status.reply_parent.remote_id}}">status</a> <a href="{{ notification.related_status.reply_parent.local_path }}">status</a>
{% elif notification.notification_type == 'FOLLOW' %} {% elif notification.notification_type == 'FOLLOW' %}
followed you followed you
{% elif notification.notification_type == 'FOLLOW_REQUEST' %} {% elif notification.notification_type == 'FOLLOW_REQUEST' %}
@ -41,7 +41,7 @@
</div> </div>
{% elif notification.notification_type == 'BOOST' %} {% elif notification.notification_type == 'BOOST' %}
boosted your <a href="{{ notification.related_status.remote_id}}">status</a> boosted your <a href="{{ notification.related_status.local_path }}">status</a>
{% endif %} {% endif %}
{% else %} {% else %}
your <a href="/import-status/{{ notification.related_import.id }}">import</a> completed. your <a href="/import-status/{{ notification.related_import.id }}">import</a> completed.
@ -54,7 +54,7 @@
<div class="notification py-2 {% if notification.id in unread %}is-primary is-light{% else %}has-background-white{% if notification.notification_type == 'REPLY' or notification.notification_type == 'MENTION' %} has-text-black{% else %}-bis has-text-grey-dark{% endif %}{% endif %}"> <div class="notification py-2 {% if notification.id in unread %}is-primary is-light{% else %}has-background-white{% if notification.notification_type == 'REPLY' or notification.notification_type == 'MENTION' %} has-text-black{% else %}-bis has-text-grey-dark{% endif %}{% endif %}">
<div class="columns"> <div class="columns">
<div class="column"> <div class="column">
<a href="{{ notification.related_status.remote_id }}">{{ notification.related_status.content | safe | truncatewords_html:10 }}</a> <a href="{{ notification.related_status.local_path }}">{{ notification.related_status.content | safe | truncatewords_html:10 }}</a>
</div> </div>
<div class="column is-narrow {% if notification.notification_type == 'REPLY' or notification.notification_type == 'MENTION' %}has-text-black{% else %}has-text-grey-dark{% endif %}"> <div class="column is-narrow {% if notification.notification_type == 'REPLY' or notification.notification_type == 'MENTION' %}has-text-black{% else %}has-text-grey-dark{% endif %}">
{{ notification.related_status.published_date | post_date }} {{ notification.related_status.published_date | post_date }}

View file

@ -122,7 +122,7 @@
<div class="block"> <div class="block">
<div> <div>
{% include 'snippets/shelf.html' with shelf=shelf ratings=ratings %} {% include 'snippets/shelf.html' with shelf=shelf books=books ratings=ratings %}
</div> </div>
</div> </div>
{% endblock %} {% endblock %}

View file

@ -1,5 +1,6 @@
{% load bookwyrm_tags %} {% load bookwyrm_tags %}
{% if request.user|follow_request_exists:user %} {% if request.user|follow_request_exists:user %}
<div class="field is-grouped">
<form action="/accept-follow-request/" method="POST"> <form action="/accept-follow-request/" method="POST">
{% csrf_token %} {% csrf_token %}
<input type="hidden" name="user" value="{{ user.username }}"> <input type="hidden" name="user" value="{{ user.username }}">
@ -10,4 +11,5 @@
<input type="hidden" name="user" value="{{ user.username }}"> <input type="hidden" name="user" value="{{ user.username }}">
<button class="button is-danger is-light is-small" type="submit" class="warning">Delete</button> <button class="button is-danger is-light is-small" type="submit" class="warning">Delete</button>
</form> </form>
</div>
{% endif %} {% endif %}

View file

@ -1,20 +1,21 @@
{% load bookwyrm_tags %} {% load bookwyrm_tags %}
{% with activity.id|uuid as uuid %} {% with status.id|uuid as uuid %}
<form class="is-flex-grow-1" name="reply" action="/reply" method="post" onsubmit="return reply(event)"> <form class="is-flex-grow-1" name="reply" action="/reply" method="post" onsubmit="return reply(event)">
<div class="columns"> <div class="columns">
{% csrf_token %} {% csrf_token %}
<input type="hidden" name="reply_parent" value="{{ activity.id }}"> <input type="hidden" name="reply_parent" value="{{ status.id }}">
<input type="hidden" name="user" value="{{ request.user.id }}"> <input type="hidden" name="user" value="{{ request.user.id }}">
<div class="column"> <div class="column">
{% include 'snippets/content_warning_field.html' with parent_status=activity %} {% include 'snippets/content_warning_field.html' with parent_status=status %}
<label for="id_content_{{ status.id }}-{{ uuid }}" class="is-sr-only">Reply</label>
<div class="field"> <div class="field">
<textarea class="textarea" name="content" placeholder="Leave a comment..." id="id_content_{{ activity.id }}-{{ uuid }}" required="true"></textarea> <textarea class="textarea" name="content" placeholder="Leave a comment..." id="id_content_{{ status.id }}-{{ uuid }}" required="true">{{ status|mentions:request.user }}</textarea>
</div> </div>
</div> </div>
<div class="column is-narrow"> <div class="column is-narrow">
<div class="field"> <div class="field">
{% include 'snippets/privacy_select.html' with current=activity.privacy %} {% include 'snippets/privacy_select.html' with current=status.privacy %}
</div> </div>
<div class="field"> <div class="field">
<button class="button is-primary" type="submit"> <button class="button is-primary" type="submit">

View file

@ -1,6 +1,6 @@
{% load humanize %} {% load humanize %}
{% load bookwyrm_tags %} {% load bookwyrm_tags %}
{% if shelf.books.all|length > 0 %} {% if books|length > 0 %}
<table class="table is-striped is-fullwidth"> <table class="table is-striped is-fullwidth">
<tr class="book-preview"> <tr class="book-preview">
@ -34,7 +34,7 @@
</th> </th>
{% endif %} {% endif %}
</tr> </tr>
{% for book in shelf.books.all %} {% for book in books %}
<tr class="book-preview"> <tr class="book-preview">
<td> <td>
{% include 'snippets/book_cover.html' with book=book size="small" %} {% include 'snippets/book_cover.html' with book=book size="small" %}

View file

@ -5,7 +5,7 @@
<input type="hidden" name="name" value="{{ tag.tag.name }}"> <input type="hidden" name="name" value="{{ tag.tag.name }}">
<div class="tags has-addons"> <div class="tags has-addons">
<a class="tag" href="/tag/{{ tag.tag.identifier|urlencode }}"> <a class="tag" href="{{ tag.tag.local_path }}">
{{ tag.tag.name }} {{ tag.tag.name }}
</a> </a>
{% if tag.tag.identifier in user_tags %} {% if tag.tag.identifier in user_tags %}

View file

@ -5,7 +5,7 @@
<div class="column is-narrow"> <div class="column is-narrow">
<div class="media"> <div class="media">
<div class="media-left"> <div class="media-left">
<a href="/user/{{ user|username }}"> <a href="{{ user.local_path }}">
{% include 'snippets/avatar.html' with user=user large=True %} {% include 'snippets/avatar.html' with user=user large=True %}
</a> </a>
</div> </div>
@ -14,8 +14,8 @@
<p><a href="{{ user.remote_id }}">{{ user.username }}</a></p> <p><a href="{{ user.remote_id }}">{{ user.username }}</a></p>
<p>Joined {{ user.created_date | naturaltime }}</p> <p>Joined {{ user.created_date | naturaltime }}</p>
<p> <p>
<a href="/user/{{ user | username }}/followers">{{ user.followers.count }} follower{{ user.followers.count | pluralize }}</a>, <a href="{{ user.local_path }}/followers">{{ user.followers.count }} follower{{ user.followers.count | pluralize }}</a>,
<a href="/user/{{ user | username }}/following">{{ user.following.count }} following</a> <a href="{{ user.local_path }}/following">{{ user.following.count }} following</a>
</p> </p>
</div> </div>
</div> </div>

View file

@ -1,2 +1,2 @@
{% load bookwyrm_tags %} {% load bookwyrm_tags %}
<a href="/user/{{ user | username }}" class="user">{% if user.name %}{{ user.name }}{% else %}{{ user | username }}{% endif %}</a>{% if possessive %}'s{% endif %}{% if show_full and user.name or show_full and user.localname %} ({{ user.username }}){% endif %} <a href="{{ user.local_path }}" class="user">{% if user.name %}{{ user.name }}{% else %}{{ user | username }}{% endif %}</a>{% if possessive %}'s{% endif %}{% if show_full and user.name or show_full and user.localname %} ({{ user.username }}){% endif %}

View file

@ -24,11 +24,11 @@
{% for shelf in shelves %} {% for shelf in shelves %}
<div class="column is-narrow"> <div class="column is-narrow">
<h3>{{ shelf.name }} <h3>{{ shelf.name }}
{% if shelf.size > 3 %}<small>(<a href="{{ shelf.remote_id }}">See all {{ shelf.size }}</a>)</small>{% endif %}</h3> {% if shelf.size > 3 %}<small>(<a href="{{ shelf.local_path }}">See all {{ shelf.size }}</a>)</small>{% endif %}</h3>
<div class="is-mobile field is-grouped"> <div class="is-mobile field is-grouped">
{% for book in shelf.books %} {% for book in shelf.books %}
<div class="control"> <div class="control">
<a href="/book/{{ book.id }}"> <a href="{{ book.local_path }}">
{% include 'snippets/book_cover.html' with book=book size="medium" %} {% include 'snippets/book_cover.html' with book=book size="medium" %}
</a> </a>
</div> </div>
@ -37,7 +37,7 @@
</div> </div>
{% endfor %} {% endfor %}
</div> </div>
<small><a href="/user/{{ user.localname }}/shelves">See all {{ shelf_count }} shelves</a></small> <small><a href="{{ user.local_path }}/shelves">See all {{ shelf_count }} shelves</a></small>
</div> </div>
<div> <div>

View file

@ -110,7 +110,7 @@ def get_uuid(identifier):
return '%s%s' % (identifier, uuid4()) return '%s%s' % (identifier, uuid4())
@register.filter(name="post_date") @register.filter(name='post_date')
def time_since(date): def time_since(date):
''' concise time ago function ''' ''' concise time ago function '''
if not isinstance(date, datetime): if not isinstance(date, datetime):
@ -133,13 +133,20 @@ def time_since(date):
return '%ds' % delta.seconds return '%ds' % delta.seconds
@register.filter(name="to_markdown") @register.filter(name='to_markdown')
def get_markdown(content): def get_markdown(content):
''' convert markdown to html ''' ''' convert markdown to html '''
if content: if content:
return to_markdown(content) return to_markdown(content)
return None return None
@register.filter(name='mentions')
def get_mentions(status, user):
''' anyone tagged or replied to in this status '''
mentions = set([status.user] + list(status.mention_users.all()))
return ' '.join(
'@' + get_user_identifier(m) for m in mentions if not m == user)
@register.simple_tag(takes_context=True) @register.simple_tag(takes_context=True)
def active_shelf(context, book): def active_shelf(context, book):
''' check what shelf a user has a book on, if any ''' ''' check what shelf a user has a book on, if any '''

View file

@ -5,11 +5,10 @@ from django.urls import path, re_path
from bookwyrm import incoming, outgoing, views, settings, wellknown from bookwyrm import incoming, outgoing, views, settings, wellknown
from bookwyrm import view_actions as actions from bookwyrm import view_actions as actions
from bookwyrm.utils import regex
username_regex = r'(?P<username>[\w\-_\.]+@[\w\-\_\.]+)' user_path = r'^user/(?P<username>%s)' % regex.username
localname_regex = r'(?P<username>[\w\-_\.]+)' local_user_path = r'^user/(?P<username>%s)' % regex.localname
user_path = r'^user/%s' % username_regex
local_user_path = r'^user/%s' % localname_regex
status_types = [ status_types = [
'status', 'status',
@ -20,7 +19,7 @@ status_types = [
'generatednote' 'generatednote'
] ]
status_path = r'%s/(%s)/(?P<status_id>\d+)' % \ status_path = r'%s/(%s)/(?P<status_id>\d+)' % \
(local_user_path, '|'.join(status_types)) (user_path, '|'.join(status_types))
book_path = r'^book/(?P<book_id>\d+)' book_path = r'^book/(?P<book_id>\d+)'

View file

@ -1,5 +1,6 @@
''' defining regexes for regularly used concepts ''' ''' defining regexes for regularly used concepts '''
domain = r'[a-z-A-Z0-9_\-]+\.[a-z]+' domain = r'[a-z-A-Z0-9_\-]+\.[a-z]+'
username = r'@[a-zA-Z_\-\.0-9]+(@%s)?' % domain localname = r'@?[a-zA-Z_\-\.0-9]+'
full_username = r'@?[a-zA-Z_\-\.0-9]+@%s' % domain username = r'%s(@%s)?' % (localname, domain)
full_username = r'%s@%s' % (localname, domain)

View file

@ -405,7 +405,7 @@ def user_page(request, username):
continue continue
shelf_preview.append({ shelf_preview.append({
'name': user_shelf.name, 'name': user_shelf.name,
'remote_id': user_shelf.remote_id, 'local_path': user_shelf.local_path,
'books': user_shelf.books.all()[:3], 'books': user_shelf.books.all()[:3],
'size': user_shelf.books.count(), 'size': user_shelf.books.count(),
}) })
@ -765,12 +765,17 @@ def shelf_page(request, username, shelf_identifier):
if is_api_request(request): if is_api_request(request):
return ActivitypubResponse(shelf.to_activity(**request.GET)) return ActivitypubResponse(shelf.to_activity(**request.GET))
books = models.ShelfBook.objects.filter(
added_by=user, shelf=shelf
).order_by('-updated_date').all()
data = { data = {
'title': '%s\'s %s shelf' % (user.display_name, shelf.name), 'title': '%s\'s %s shelf' % (user.display_name, shelf.name),
'user': user, 'user': user,
'is_self': is_self, 'is_self': is_self,
'shelves': shelves.all(), 'shelves': shelves.all(),
'shelf': shelf, 'shelf': shelf,
'books': [b.book for b in books],
} }
return TemplateResponse(request, 'shelf.html', data) return TemplateResponse(request, 'shelf.html', data)