User generated local paths

This commit is contained in:
Mouse Reeve 2020-12-30 17:36:35 -08:00
parent d821a08cff
commit dc68fdd53e
14 changed files with 50 additions and 38 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

@ -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="/user/{{ notification.related_status.user }}/status/{{ notification.related_status.id}}">status</a> <a href="{{ notification.related_status.local_path }}">status</a>
{% elif notification.notification_type == 'REPLY' %} {% elif notification.notification_type == 'REPLY' %}
<a href="/user/{{ notification.related_status.user }}/status/{{ notification.related_status.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

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

View file

@ -1,20 +1,20 @@
{% 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 %}
<div class="field"> <div class="field">
<textarea class="textarea" name="content" placeholder="Leave a comment..." id="id_content_{{ activity.id }}-{{ uuid }}" required="true">{{ activity|mentions:request.user }}</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

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

@ -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',

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(),
}) })