fix group references in templates

Let's do this the sensible way huh, by using backwards references to memberships etc

Also adds filters for is_member and is_invited so we don't have to do weird things in group Views
This commit is contained in:
Hugh Rundle 2021-10-02 10:47:42 +10:00
parent 2f42161dda
commit 0f3be40957
8 changed files with 35 additions and 21 deletions

View file

@ -11,7 +11,7 @@
{% block searchresults %}
{% endblock %}
{% include "groups/members.html" %}
{% include "groups/members.html" with group=group %}
<h2 class="title is-5">Lists</h2>
{% if not lists %}

View file

@ -7,7 +7,8 @@
<p class="subtitle is-6">{% trans "Members can add and remove books on your group's book lists" %}</p>
<div class="column is-flex is-flex-grow-0">
{% for member in group.members.all %}
{% for membership in group.memberships.all %}
{% with member=membership.user %}
<div class="box has-text-centered is-shadowless has-background-white-bis my-0 mx-2">
<a href="{{ member.local_path }}" class="has-text-black">
{% include 'snippets/avatar.html' with user=member large=True %}
@ -19,7 +20,7 @@
<span class="is-sr-only">Manager</span>
</span>
{% endif %}
{% include 'snippets/add_to_group_button.html' with user=member minimal=True %}
{% include 'snippets/add_to_group_button.html' with user=member group=group minimal=True %}
{% if member.mutuals %}
<p class="help">
{% blocktrans trimmed with mutuals=member.mutuals|intcomma count counter=member.mutuals %}
@ -41,5 +42,6 @@
</p>
{% endif %}
</div>
{% endwith %}
{% endfor %}
</div>

View file

@ -12,7 +12,7 @@
<span title="{{ user.display_name }}" class="is-block is-6 has-text-weight-bold">{{ user.display_name|truncatechars:10 }}</span>
<span title="@{{ user|username }}" class="is-block pb-3">@{{ user|username|truncatechars:8 }}</span>
</a>
{% include 'snippets/add_to_group_button.html' with user=user minimal=True %}
{% include 'snippets/add_to_group_button.html' with user=user group=group minimal=True %}
{% if user.mutuals %}
<p class="help">
{% blocktrans trimmed with mutuals=user.mutuals|intcomma count counter=user.mutuals %}

View file

@ -3,7 +3,7 @@
{% load interaction %}
<div class="columns is-multiline">
{% for group in user.bookwyrm_groups.all %}
{% for group in groups %}
<div class="column is-one-quarter">
<div class="card is-stretchable">
<header class="card-header">

View file

@ -1,4 +1,5 @@
{% load i18n %}
{% load bookwyrm_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 %}
@ -6,30 +7,30 @@
<div class="field{% if not minimal %} has-addons{% else %} mb-0{% endif %}">
<div class="control">
<form action="{% url 'add-group-member' %}" method="POST" class="interaction add_{{ user.id }} {% if user in group.members.all %}is-hidden{%endif %}" data-id="add_{{ user.id }}">
<form action="{% url 'invite-group-member' %}" method="POST" class="interaction add_{{ user.id }} {% if group|is_member:user or group|is_invited:user %}is-hidden{%endif %}" data-id="add_{{ user.id }}">
{% csrf_token %}
<input type="hidden" name="group" value="{{ group.id }}">
<input type="hidden" name="user" value="{{ user.username }}">
{% if user.local %}
<button class="button is-small{% if not minimal %} is-link{% endif %}" type="submit">
{% if show_username %}
{% blocktrans with username=user.localname %}Add @{{ username }}{% endblocktrans %}
{% blocktrans with username=user.localname %}Invite @{{ username }}{% endblocktrans %}
{% else %}
{% trans "Add" %}
{% trans "Invite" %}
{% endif %}
</button>
{% else %}
<!-- TODO: This is an incredibly ugly hack until group are AP enabled -->
<!-- TODO: This is an incredibly ugly hack until groups are AP enabled -->
<span><em>Remote User</em></span>
{% endif %}
</form>
<form action="{% url 'remove-group-member' %}" method="POST" class="interaction add_{{ user.id }} {% if user not in group.members.all %}is-hidden{%endif %}" data-id="add_{{ user.id }}">
<form action="{% url 'uninvite-group-member' %}" method="POST" class="interaction add_{{ user.id }} {% if not group|is_member:user or not group|is_invited:user %}is-hidden{% endif %}" data-id="add_{{ user.id }}">
{% csrf_token %}
<input type="hidden" name="group" value="{{ group.id }}">
<input type="hidden" name="user" value="{{ user.username }}">
{% if user.manually_approves_followers and request.user not in user.followers.all %}
{% if group|is_invited:user %}
<button class="button is-small is-danger is-light" type="submit">
{% trans "Undo follow request" %}
{% trans "Undo Invitation" %}
</button>
{% else %}
<button class="button is-small is-danger is-light" type="submit">
@ -42,10 +43,5 @@
{% endif %}
</form>
</div>
{% if not minimal %}
<div class="control">
{% include 'snippets/user_options.html' with user=user class="is-small" %}
</div>
{% endif %}
</div>
{% endif %}

View file

@ -24,7 +24,7 @@
{% block panel %}
<section class="block">
<form name="create-group" method="post" action="{% url 'user-groups' 'empty_arg' %}" class="box is-hidden" id="create_group">
<form name="create-group" method="post" action="{% url 'user-groups' request.user.name %}" class="box is-hidden" id="create_group">
<header class="columns">
<h3 class="title column">{% trans "Create group" %}</h3>
<div class="column is-narrow">

View file

@ -22,9 +22,6 @@
{% block panel %}
{% if user.bookwyrm_user %}
{% for group in user.bookwyrm_groups.all %}
<div>{{ group.name }}</div>
{% endfor %}
<div class="block">
<h2 class="title">
{% include 'user/shelf/books_header.html' %}

View file

@ -0,0 +1,19 @@
""" template filters """
from django import template
from bookwyrm import models
register = template.Library()
@register.filter(name="is_member")
def is_member(group, user):
"""whether or not the user is a member of this group"""
return models.BookwyrmGroupMember.objects.filter(group=group,user=user).exists()
@register.filter(name="is_invited")
def is_invited(group, user):
"""whether or not the user has a pending invitation to join this group"""
return models.GroupMemberInvitation.objects.filter(group=group,user=user).exists()