From 64ba5cc6f40eedfa886130819e205dd4a305bb41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Sat, 14 Jan 2023 23:33:58 +0100 Subject: [PATCH] Begin making member admin page more detailed. --- src/membership/models.py | 4 +-- src/membership/selectors.py | 22 ++++++++++--- .../membership/members_admin_detail.html | 33 ++++++++++++++++++- src/membership/views.py | 7 +++- src/utils/templates/utils/list.html | 29 ++++++++-------- 5 files changed, 73 insertions(+), 22 deletions(-) diff --git a/src/membership/models.py b/src/membership/models.py index 85b0bb6..b2f1b5b 100644 --- a/src/membership/models.py +++ b/src/membership/models.py @@ -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()) diff --git a/src/membership/selectors.py b/src/membership/selectors.py index a23590d..21a8326 100644 --- a/src/membership/selectors.py +++ b/src/membership/selectors.py @@ -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) diff --git a/src/membership/templates/membership/members_admin_detail.html b/src/membership/templates/membership/members_admin_detail.html index 2844d5f..429c215 100644 --- a/src/membership/templates/membership/members_admin_detail.html +++ b/src/membership/templates/membership/members_admin_detail.html @@ -3,6 +3,37 @@ {% block content %} - {{ member.username }} + +

+ {{ member.username }} +

+ +
+ +

{% trans "Membership" %}

+ + + + + + + + + + + + {% for period in subscription_periods %} + + + + + + + {% endfor %} + +
{% trans "Start" %}{% trans "End" %}{% trans "Has membership" %}{% trans "Actions" %}
{{ period.period.lower }}{{ period.period.upper }}{{ period.membership_exists }}
+ + + {% endblock %} diff --git a/src/membership/views.py b/src/membership/views.py index 43881d4..03ae0f0 100644 --- a/src/membership/views.py +++ b/src/membership/views.py @@ -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, diff --git a/src/utils/templates/utils/list.html b/src/utils/templates/utils/list.html index bb0d5e7..277e398 100644 --- a/src/utils/templates/utils/list.html +++ b/src/utils/templates/utils/list.html @@ -9,19 +9,21 @@ - - {% for column, column_label in columns %} - - {% endfor %} - {% if row_actions %} - - {% endif %} - + + {% for column, column_label in columns %} + + {% endfor %} + {% if row_actions %} + + {% endif %} + + + {% for row in rows %} {% for value in row.data.values %} @@ -38,6 +40,7 @@ {% endif %} {% endfor %} +
- - {{ column_label }} - - {% trans "Actions" %}
+ + {{ column_label }} + + {% trans "Actions" %}
{% if is_paginated %} @@ -76,7 +79,7 @@ {% if page.paginator.num_pages > page.number|add:'3' %}
  • - + {% endif %} {% if not page.has_next %}