membersystem/src/membership/views.py
Víðir Valberg Guðmundsson f18469833a
All checks were successful
continuous-integration/drone/push Build is passing
The ruffening.
2024-07-15 00:19:37 +02:00

116 lines
3.1 KiB
Python

"""Views for the membership app."""
from __future__ import annotations
from typing import TYPE_CHECKING
from django.utils.translation import gettext_lazy as _
from django_view_decorator import namespaced_decorator_factory
from utils.view_utils import RenderConfig
from utils.view_utils import RowAction
from utils.view_utils import render
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
if TYPE_CHECKING:
from django.http import HttpRequest
from django.http import HttpResponse
member_view = namespaced_decorator_factory(namespace="member", base_path="membership")
@member_view(
paths="",
name="membership-overview",
login_required=True,
)
def membership_overview(request: HttpRequest) -> HttpResponse:
"""View to show the membership overview."""
memberships = get_memberships(member=request.user)
current_membership = memberships.current()
previous_memberships = memberships.previous()
current_period = current_membership.period.period if current_membership else None
context = {
"current_membership": current_membership,
"current_period": current_period,
"previous_memberships": previous_memberships,
}
return render(
request=request,
template_name="membership/membership_overview.html",
context=context,
)
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],
)
def members_admin(request: HttpRequest) -> HttpResponse:
"""View to list all members."""
users = get_members()
render_config = RenderConfig(
entity_name="member",
entity_name_plural="members",
paginate_by=20,
objects=users,
columns=[
("username", _("Username")),
("first_name", _("First name")),
("last_name", _("Last name")),
("email", _("Email")),
("active_membership", _("Active membership")),
],
row_actions=[
RowAction(
label=_("View"),
url_name="admin-members:detail",
url_kwargs={"member_id": "id"},
),
],
)
return render_config.render_list(
request=request,
)
@admin_members_view(
paths="<int:member_id>/",
name="detail",
login_required=True,
permissions=[ADMINISTRATE_MEMBERS.path],
)
def members_admin_detail(request: HttpRequest, member_id: int) -> HttpResponse:
"""View to show the details of a member."""
member = get_member(member_id=member_id)
subscription_periods = get_subscription_periods(member=member)
context = {
"member": member,
"subscription_periods": subscription_periods,
"base_path": "admin-members:list",
}
return render(
request=request,
template_name="membership/members_admin_detail.html",
context=context,
)