fixup! fixup! user: allow customization of User.get_full_name() through templates (#72945)
gitea-wip/hobo/pipeline/pr-main There was a failure building this commit Details

This commit is contained in:
Agate 2023-01-05 09:40:36 +01:00
parent 299a1c236f
commit 5c828a7524
2 changed files with 12 additions and 0 deletions

View File

@ -288,6 +288,8 @@ if PROJECT_NAME != 'wcs' and 'authentic2' not in INSTALLED_APPS:
'mellon.middleware.PassiveAuthenticationMiddleware',
'hobo.provisionning.middleware.ProvisionningMiddleware',
)
if PROJECT_NAME != 'wcs':
INSTALLED_APPS += ('hobo.user_name.apps.UserNameConfig',)
if 'authentic2' in INSTALLED_APPS:
MIDDLEWARE = MIDDLEWARE + ('hobo.agent.authentic2.middleware.ProvisionningMiddleware',)

View File

@ -4,6 +4,7 @@ import logging
import django.db
import django.template.exceptions
from django.apps import AppConfig
from django.conf import settings
from django.contrib.auth import get_user_model
from . import utils
@ -28,6 +29,9 @@ def get_full_name(user):
def cached_extra_attributes(user):
if 'authentic2' in settings.INSTALLED_APPS:
# we're in authentic, attributes are looked up differently
return {k: v.to_python() for k, v in user.attributes.values.items()}
try:
return user.extra_attributes.data
except django.db.models.ObjectDoesNotExist:
@ -57,3 +61,9 @@ class UserNameConfig(AppConfig):
# for easier access in templates
User.cached_extra_attributes = functools.cached_property(cached_extra_attributes)
User.cached_extra_attributes.__set_name__(User, 'cached_extra_attributes')
# to avoid performance/recursion issues
User.__str__ = User.original_get_full_name
if 'attributes' not in User.__dict__:
User.attributes = functools.cached_property(cached_extra_attributes)
User.attributes.__set_name__(User, 'attributes')