We want a list of users, not memberships.

This commit is contained in:
Víðir Valberg Guðmundsson 2023-01-03 17:00:07 +01:00
parent bc00b32c46
commit cf99c3f40e
8 changed files with 77 additions and 32 deletions

View file

@ -38,9 +38,9 @@ class Permission:
) )
ADMINISTRATE_MEMBERSHIPS = Permission( ADMINISTRATE_MEMBERS = Permission(
name=_("Can administrate memberships"), name=_("Can administrate members"),
codename="administrate_memberships", codename="administrate_members",
app_label="membership", app_label="membership",
model="membership", model="membership",
) )

View file

@ -24,3 +24,7 @@ def get_memberships(
memberships = memberships.filter(period=period) memberships = memberships.filter(period=period)
return memberships return memberships
def get_users():
return User.objects.all()

View file

@ -1,13 +0,0 @@
{% extends "base.html" %}
{% load i18n %}
{% block content %}
{% for period in subscription_periods %}
<h3>{{ period }}</h3>
{% for membership in period.membership_set.all %}
<p>{{ membership.user.username }}</p>
{% endfor %}
{% endfor %}
{% endblock %}

View file

@ -1,11 +1,13 @@
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.contrib.auth.decorators import permission_required from django.contrib.auth.decorators import permission_required
from django.utils.translation import gettext_lazy as _
from zen_queries import render from zen_queries import render
from .permissions import ADMINISTRATE_MEMBERSHIPS from .permissions import ADMINISTRATE_MEMBERS
from .selectors import get_memberships from .selectors import get_memberships
from .selectors import get_subscription_periods from .selectors import get_users
from utils.view_utils import base_view_context from utils.view_utils import base_view_context
from utils.view_utils import render_list
@login_required @login_required
@ -30,16 +32,17 @@ def membership_overview(request):
@login_required @login_required
@permission_required(ADMINISTRATE_MEMBERSHIPS.path) @permission_required(ADMINISTRATE_MEMBERS.path)
def membership_admin(request): def members_admin(request):
subscription_periods = get_subscription_periods() users = get_users()
context = base_view_context(request) | { return render_list(
"subscription_periods": subscription_periods,
}
return render(
request=request, request=request,
template_name="membership/membership_admin.html", objects=users,
context=context, columns=[
("username", _("Username")),
("first_name", _("First name")),
("last_name", _("Last name")),
("email", _("Email")),
],
) )

View file

@ -192,8 +192,8 @@
<span>{% trans "Admin" %}</span> <span>{% trans "Admin" %}</span>
</h6> </h6>
<ul class="nav flex-column mb-2"> <ul class="nav flex-column mb-2">
<li class="nav-item {% active_path "membership-admin" "active" %}"> <li class="nav-item {% active_path "admin-members" "active" %}">
<a class="nav-link" href="{% url "membership-admin" %}"> <a class="nav-link" href="{% url "admin-members" %}">
<span data-feather="file-text"></span> <span data-feather="file-text"></span>
{% trans "Members" %} {% trans "Members" %}
</a> </a>

View file

@ -7,14 +7,14 @@ from django.urls import path
from .views import index from .views import index
from .views import services_overview from .views import services_overview
from membership.views import membership_admin from membership.views import members_admin
from membership.views import membership_overview from membership.views import membership_overview
urlpatterns = [ urlpatterns = [
path("", login_required(index), name="index"), path("", login_required(index), name="index"),
path("services/", login_required(services_overview), name="services-overview"), path("services/", login_required(services_overview), name="services-overview"),
path("membership/", membership_overview, name="membership-overview"), path("membership/", membership_overview, name="membership-overview"),
path("membership/admin/", membership_admin, name="membership-admin"), path("admin/members/", members_admin, name="admin-members"),
path("accounts/", include("allauth.urls")), path("accounts/", include("allauth.urls")),
path("admin/", admin.site.urls), path("admin/", admin.site.urls),
] ]

View file

@ -0,0 +1,23 @@
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<table class="table table-striped">
<thead>
<tr>
{% for column in columns %}
<th scope="col">{{ column }}</th>
{% endfor %}
</tr>
</thead>
{% for row in object_rows %}
<tr>
{% for value in row %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endblock %}

View file

@ -1,6 +1,34 @@
from django.contrib.sites.shortcuts import get_current_site as django_get_current_site from django.contrib.sites.shortcuts import get_current_site as django_get_current_site
from django.db.models import Model
from django.http import HttpRequest
from django.http import HttpResponse
from zen_queries import render
def base_view_context(request): def base_view_context(request):
"""Include the current site in the context.""" """Include the current site in the context."""
return {"site": django_get_current_site(request)} return {"site": django_get_current_site(request)}
def render_list(
request: HttpRequest,
objects: list["Model"],
columns: list[tuple[str, str]],
) -> HttpResponse:
# TODO: Actions per object
# TODO: Listwide actions
object_rows = [[getattr(obj, column[0]) for column in columns] for obj in objects]
column_labels = [column[1] for column in columns]
context = base_view_context(request) | {
"object_rows": object_rows,
"columns": column_labels,
}
return render(
request=request,
template_name="utils/list.html",
context=context,
)