Begin making member admin page more detailed.
All checks were successful
continuous-integration/drone/push Build is passing

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): class QuerySet(models.QuerySet):
def for_user(self, user): def for_member(self, member: Member):
return self.filter(user=user) return self.filter(user=member)
def _current(self): def _current(self):
return self.filter(period__period__contains=timezone.now()) return self.filter(period__period__contains=timezone.now())

View file

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

View file

@ -3,6 +3,37 @@
{% block content %} {% 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 %} {% endblock %}

View file

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

View file

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