2024-07-14 22:19:37 +00:00
|
|
|
"""Views for the membership app."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2023-01-03 16:00:07 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2024-01-14 11:27:36 +00:00
|
|
|
from django_view_decorator import namespaced_decorator_factory
|
2024-07-14 22:19:37 +00:00
|
|
|
from utils.view_utils import RenderConfig
|
2024-02-29 20:25:59 +00:00
|
|
|
from utils.view_utils import RowAction
|
|
|
|
from utils.view_utils import render
|
2021-02-28 22:00:11 +00:00
|
|
|
|
2023-01-03 16:00:07 +00:00
|
|
|
from .permissions import ADMINISTRATE_MEMBERS
|
2023-01-03 20:36:34 +00:00
|
|
|
from .selectors import get_member
|
|
|
|
from .selectors import get_members
|
2023-01-02 22:06:00 +00:00
|
|
|
from .selectors import get_memberships
|
2023-01-14 22:33:58 +00:00
|
|
|
from .selectors import get_subscription_periods
|
2021-02-28 22:00:11 +00:00
|
|
|
|
2024-07-14 22:19:37 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from django.http import HttpRequest
|
|
|
|
from django.http import HttpResponse
|
|
|
|
|
2024-01-14 11:27:36 +00:00
|
|
|
member_view = namespaced_decorator_factory(namespace="member", base_path="membership")
|
|
|
|
|
|
|
|
|
|
|
|
@member_view(
|
|
|
|
paths="",
|
|
|
|
name="membership-overview",
|
|
|
|
login_required=True,
|
|
|
|
)
|
2024-07-14 22:19:37 +00:00
|
|
|
def membership_overview(request: HttpRequest) -> HttpResponse:
|
|
|
|
"""View to show the membership overview."""
|
2023-09-16 14:27:15 +00:00
|
|
|
memberships = get_memberships(member=request.user)
|
2021-02-28 22:00:11 +00:00
|
|
|
current_membership = memberships.current()
|
|
|
|
previous_memberships = memberships.previous()
|
|
|
|
|
2023-01-02 21:13:25 +00:00
|
|
|
current_period = current_membership.period.period if current_membership else None
|
|
|
|
|
2023-01-21 15:20:50 +00:00
|
|
|
context = {
|
2022-08-09 12:39:02 +00:00
|
|
|
"current_membership": current_membership,
|
2023-01-02 21:13:25 +00:00
|
|
|
"current_period": current_period,
|
2022-08-09 12:39:02 +00:00
|
|
|
"previous_memberships": previous_memberships,
|
|
|
|
}
|
2021-02-28 22:00:11 +00:00
|
|
|
|
2023-01-02 21:13:25 +00:00
|
|
|
return render(
|
|
|
|
request=request,
|
2023-01-02 22:06:00 +00:00
|
|
|
template_name="membership/membership_overview.html",
|
|
|
|
context=context,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-14 11:27:36 +00:00
|
|
|
admin_members_view = namespaced_decorator_factory(
|
|
|
|
namespace="admin-members",
|
|
|
|
base_path="admin",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@admin_members_view(
|
|
|
|
paths="members/",
|
|
|
|
name="list",
|
|
|
|
login_required=True,
|
|
|
|
permissions=[ADMINISTRATE_MEMBERS.path],
|
|
|
|
)
|
2024-07-14 22:19:37 +00:00
|
|
|
def members_admin(request: HttpRequest) -> HttpResponse:
|
|
|
|
"""View to list all members."""
|
2023-01-03 20:36:34 +00:00
|
|
|
users = get_members()
|
2023-01-02 22:06:00 +00:00
|
|
|
|
2024-07-14 22:19:37 +00:00
|
|
|
render_config = RenderConfig(
|
2023-10-02 18:50:39 +00:00
|
|
|
entity_name="member",
|
|
|
|
entity_name_plural="members",
|
2023-01-12 16:31:34 +00:00
|
|
|
paginate_by=20,
|
2023-01-03 16:00:07 +00:00
|
|
|
objects=users,
|
|
|
|
columns=[
|
|
|
|
("username", _("Username")),
|
|
|
|
("first_name", _("First name")),
|
|
|
|
("last_name", _("Last name")),
|
|
|
|
("email", _("Email")),
|
2023-01-03 20:36:34 +00:00
|
|
|
("active_membership", _("Active membership")),
|
2023-01-03 16:00:07 +00:00
|
|
|
],
|
2023-01-03 20:36:34 +00:00
|
|
|
row_actions=[
|
|
|
|
RowAction(
|
|
|
|
label=_("View"),
|
2024-01-14 11:27:36 +00:00
|
|
|
url_name="admin-members:detail",
|
2023-01-03 20:36:34 +00:00
|
|
|
url_kwargs={"member_id": "id"},
|
2023-01-11 20:55:58 +00:00
|
|
|
),
|
2023-01-03 20:36:34 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2024-07-14 22:19:37 +00:00
|
|
|
return render_config.render_list(
|
|
|
|
request=request,
|
|
|
|
)
|
|
|
|
|
2023-01-03 20:36:34 +00:00
|
|
|
|
2024-01-14 11:27:36 +00:00
|
|
|
@admin_members_view(
|
|
|
|
paths="<int:member_id>/",
|
|
|
|
name="detail",
|
|
|
|
login_required=True,
|
|
|
|
permissions=[ADMINISTRATE_MEMBERS.path],
|
|
|
|
)
|
2024-07-14 22:19:37 +00:00
|
|
|
def members_admin_detail(request: HttpRequest, member_id: int) -> HttpResponse:
|
|
|
|
"""View to show the details of a member."""
|
2023-01-03 20:36:34 +00:00
|
|
|
member = get_member(member_id=member_id)
|
2023-01-14 22:33:58 +00:00
|
|
|
subscription_periods = get_subscription_periods(member=member)
|
2023-01-03 20:36:34 +00:00
|
|
|
|
2023-01-21 15:20:50 +00:00
|
|
|
context = {
|
2023-01-14 22:33:58 +00:00
|
|
|
"member": member,
|
|
|
|
"subscription_periods": subscription_periods,
|
2024-01-14 11:27:36 +00:00
|
|
|
"base_path": "admin-members:list",
|
2023-01-14 22:33:58 +00:00
|
|
|
}
|
2023-01-03 20:36:34 +00:00
|
|
|
|
|
|
|
return render(
|
|
|
|
request=request,
|
|
|
|
template_name="membership/members_admin_detail.html",
|
|
|
|
context=context,
|
2023-01-02 21:13:25 +00:00
|
|
|
)
|