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
gitea-wip/hobo/pipeline/pr-main There was a failure building this commit
Details
This commit is contained in:
parent
299a1c236f
commit
5c828a7524
|
@ -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',)
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue