This commit is contained in:
4 files changed
+48
-2
No files matched your search
@@ -1,4 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
from wagtail.users.apps import WagtailUsersAppConfig
|
||||
|
||||
|
||||
class DnsCmsConfig(AppConfig):
|
||||
@@ -9,3 +10,7 @@ class DnsCmsConfig(AppConfig):
|
||||
from dnscms.revalidation import register_signal_handlers
|
||||
|
||||
register_signal_handlers()
|
||||
|
||||
|
||||
class UsersConfig(WagtailUsersAppConfig):
|
||||
user_viewset = "dnscms.oidc.GaltinnUserViewSet"
|
||||
@@ -17,10 +17,14 @@ from django.db.models import Q
|
||||
from django.http import Http404
|
||||
from django.shortcuts import redirect
|
||||
from django.urls import reverse
|
||||
from django.utils.functional import cached_property
|
||||
from django.utils.http import url_has_allowed_host_and_scheme
|
||||
from wagtail.admin.forms.account import NameEmailForm
|
||||
from joserfc.errors import JoseError
|
||||
from wagtail.admin.ui.tables import Column
|
||||
from wagtail.admin.views.account import AccountView, NameEmailSettingsPanel
|
||||
from wagtail.users.views.users import IndexView as UserIndexView
|
||||
from wagtail.users.views.users import UserViewSet
|
||||
|
||||
from dnscms.models import GaltinnUser
|
||||
|
||||
@@ -171,6 +175,30 @@ class GaltinnAccountView(AccountView):
|
||||
]
|
||||
|
||||
|
||||
class GaltinnUserIndexView(UserIndexView):
|
||||
"""User listing with the linked Galtinn ID next to the username"""
|
||||
|
||||
@cached_property
|
||||
def columns(self):
|
||||
columns = super().columns
|
||||
position = [column.name for column in columns].index(self.model.USERNAME_FIELD) + 1
|
||||
galtinn_id = Column(
|
||||
"galtinn_id",
|
||||
accessor=lambda user: getattr(getattr(user, "galtinn_user", None), "galtinn_id", ""),
|
||||
label="Galtinn-ID",
|
||||
sort_key="galtinn_user__galtinn_id",
|
||||
width="10%",
|
||||
)
|
||||
return [*columns[:position], galtinn_id, *columns[position:]]
|
||||
|
||||
def get_base_queryset(self):
|
||||
return super().get_base_queryset().select_related("galtinn_user")
|
||||
|
||||
|
||||
class GaltinnUserViewSet(UserViewSet):
|
||||
index_view_class = GaltinnUserIndexView
|
||||
|
||||
|
||||
def oidc_login(request):
|
||||
if not is_enabled():
|
||||
raise Http404
|
||||
|
||||
@@ -24,7 +24,7 @@ BASE_DIR = os.path.dirname(PROJECT_DIR)
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
"dnscms",
|
||||
"dnscms.apps.DnsCmsConfig",
|
||||
# adding more cms apps? may want to add it to GRAPPLE as well
|
||||
"images",
|
||||
"generic",
|
||||
@@ -46,7 +46,7 @@ INSTALLED_APPS = [
|
||||
"wagtail.contrib.settings",
|
||||
"wagtail.embeds",
|
||||
"wagtail.sites",
|
||||
"wagtail.users",
|
||||
"dnscms.apps.UsersConfig", # wagtail.users with the Galtinn ID in the user listing
|
||||
"wagtail.snippets",
|
||||
"wagtail.documents",
|
||||
"wagtail.images",
|
||||
|
||||
@@ -375,3 +375,16 @@ def test_account_page_requires_login(client):
|
||||
|
||||
assert response.status_code == 302
|
||||
assert reverse("wagtailadmin_login") in response.url
|
||||
|
||||
|
||||
def test_user_listing_shows_galtinn_id_after_username(client, django_user_model):
|
||||
admin = django_user_model.objects.create_superuser(username="admin", password="pw")
|
||||
link(django_user_model.objects.create_user(username="kari"), galtinn_id="15284")
|
||||
client.force_login(admin)
|
||||
|
||||
response = client.get(reverse("wagtailusers_users:index"))
|
||||
|
||||
names = list(response.context["table"].columns)
|
||||
assert names.index("galtinn_id") == names.index("username") + 1
|
||||
assert "Galtinn-ID" in response.content.decode()
|
||||
assert ">15284<" in re.sub(r"\s+", "", response.content.decode())
|
||||
Reference in new issue
Block a user