Begin making member admin page more detailed.

This commit is contained in:
Víðir Valberg Guðmundsson 2023-01-14 23:33:58 +01:00
parent 659577aaac
commit 64ba5cc6f4
5 changed files with 73 additions and 22 deletions

View file

@ -58,8 +58,8 @@ class Membership(CreatedModifiedAbstract):
"""
class QuerySet(models.QuerySet):
def for_user(self, user):
return self.filter(user=user)
def for_member(self, member: Member):
return self.filter(user=member)
def _current(self):
return self.filter(period__period__contains=timezone.now())

View file

@ -1,6 +1,7 @@
import contextlib
from django.contrib.auth.models import User
from django.db.models import Exists
from django.db.models import OuterRef
from django.utils import timezone
from membership.models import Member
@ -8,11 +9,22 @@ from membership.models import Membership
from membership.models import SubscriptionPeriod
def get_subscription_periods() -> list[SubscriptionPeriod]:
def get_subscription_periods(member: Member | None = None) -> list[SubscriptionPeriod]:
subscription_periods = SubscriptionPeriod.objects.prefetch_related(
"membership_set",
"membership_set__user",
).all()
if member:
subscription_periods = subscription_periods.annotate(
membership_exists=Exists(
Membership.objects.filter(
user=member,
period=OuterRef("pk"),
),
),
)
return list(subscription_periods)
@ -26,13 +38,13 @@ def get_current_subscription_period() -> SubscriptionPeriod | None:
def get_memberships(
*,
user: User | None = None,
member: Member | None = None,
period: SubscriptionPeriod | None = None,
) -> Membership.QuerySet:
memberships = Membership.objects.select_related("membership_type").all()
if user:
memberships = memberships.for_user(user)
if member:
memberships = memberships.for_member(member=member)
if period:
memberships = memberships.filter(period=period)

View file

@ -3,6 +3,37 @@
{% block content %}
{{ member.username }}
<h1>
{{ member.username }}
</h1>
<hr>
<h3>{% trans "Membership" %}</h3>
<table class="table">
<thead>
<tr>
<th>{% trans "Start" %}</th>
<th>{% trans "End" %}</th>
<th>{% trans "Has membership" %}</th>
<th>{% trans "Actions" %}</th>
</tr>
</thead>
<tbody>
{% for period in subscription_periods %}
<tr {% if not period.period.upper %}class="table-active"{% endif %}>
<td>{{ period.period.lower }}</td>
<td>{{ period.period.upper }}</td>
<td>{{ period.membership_exists }}</td>
<td></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}

View file

@ -7,6 +7,7 @@ from .permissions import ADMINISTRATE_MEMBERS
from .selectors import get_member
from .selectors import get_members
from .selectors import get_memberships
from .selectors import get_subscription_periods
from utils.view_utils import base_view_context
from utils.view_utils import render_list
from utils.view_utils import RowAction
@ -63,8 +64,12 @@ def members_admin(request):
@permission_required(ADMINISTRATE_MEMBERS.path)
def members_admin_detail(request, member_id):
member = get_member(member_id=member_id)
subscription_periods = get_subscription_periods(member=member)
context = base_view_context(request) | {"member": member}
context = base_view_context(request) | {
"member": member,
"subscription_periods": subscription_periods,
}
return render(
request=request,

View file

@ -9,19 +9,21 @@
<table class="table table-striped">
<thead>
<tr>
{% for column, column_label in columns %}
<th scope="col">
<a href="?order_by={% if order_by == column %}-{% endif %}{{ column }}">
{{ column_label }}
</a>
</th>
{% endfor %}
{% if row_actions %}
<th scope="col">{% trans "Actions" %}</th>
{% endif %}
</tr>
<tr>
{% for column, column_label in columns %}
<th scope="col">
<a href="?order_by={% if order_by == column %}-{% endif %}{{ column }}">
{{ column_label }}
</a>
</th>
{% endfor %}
{% if row_actions %}
<th scope="col">{% trans "Actions" %}</th>
{% endif %}
</tr>
</thead>
<tbody>
{% for row in rows %}
<tr>
{% for value in row.data.values %}
@ -38,6 +40,7 @@
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
{% if is_paginated %}
@ -76,7 +79,7 @@
{% if page.paginator.num_pages > page.number|add:'3' %}
<li class="page-item">
<a class="page-link" href="?page={{ page.number|add:'4' }}">&hellip;</a></licl>
<a class="page-link" href="?page={{ page.number|add:'4' }}">&hellip;</a></licl>
{% endif %}
{% if not page.has_next %}