[WIP] remove unnecessary authentic redundant attributes accessor
gitea-wip/hobo/pipeline/pr-main This commit looks good Details

This commit is contained in:
Paul Marillonnet 2023-01-31 09:52:40 +01:00
parent b4ce8e96e8
commit 90f441d72d
5 changed files with 6 additions and 38 deletions

View File

@ -4,7 +4,6 @@ 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
@ -29,36 +28,6 @@ def get_full_name(user):
def cached_extra_attributes(user):
if 'authentic2' in settings.INSTALLED_APPS:
from django.contrib.contenttypes.models import ContentType
try:
from authentic2.custom_user.models import User
from authentic2.models import Attribute, AttributeValue
except ImportError:
return
try:
user_ct = ContentType.objects.get_for_model(User)
except ContentType.DoesNotExist:
return
extra_attributes = {}
# we're in authentic, attributes are looked up differently
for attribute in Attribute.objects.filter(disabled=False):
atvs = AttributeValue.objects.filter(
content_type=user_ct,
object_id=user.id,
attribute=attribute,
)
if not atvs:
continue
if not attribute.multiple:
atv = atvs.first()
extra_attributes[attribute.name] = atv.to_python()
else:
extra_attributes[attribute.name] = [atv.to_python() for atv in atvs]
return extra_attributes
try:
return user.extra_attributes.data
except django.db.models.ObjectDoesNotExist:
@ -83,9 +52,6 @@ class UserNameConfig(AppConfig):
User.original_get_full_name = User.get_full_name
# to replace the rendering everywhere in a consistent manner
User.get_full_name = get_full_name
# 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

View File

@ -1 +1 @@
{{ user.first_name }} {{ user.cached_extra_attributes.foo }}
{{ user.first_name }} {{ user.attributes.foo }}

View File

@ -20,13 +20,13 @@ def test_user_original_get_full_name(user):
def test_user_cached_extra_attributes(user):
assert user.cached_extra_attributes == {'foo': 'bar'}
assert user.attributes == {'foo': 'bar'}
def test_user_cached_extra_attributes_missing_fallbacks_to_empty_dict(user):
user.extra_attributes.delete()
user.refresh_from_db()
assert user.cached_extra_attributes == {}
assert user.attributes == {}
def test_user_get_full_name_from_template(user):

View File

@ -1 +1 @@
{{ user.first_name }} {{ user.cached_extra_attributes.nicknames.0 }} {{ user.cached_extra_attributes.nicknames.2 }}
{{ user.first_name }} {{ user.attributes.nicknames.0 }} {{ user.attributes.nicknames.2 }}

View File

@ -41,6 +41,7 @@ def test_get_full_name_from_template_utils_from_multiple_attrs(db, tenant, setti
user.attributes.nicknames = ['Milly', 'Molly', 'Minnie']
user.attributes.foo = 'bar'
user.save()
user.refresh_from_db()
templates_conf_individual = settings.TEMPLATES.copy()
templates_conf_individual[0]['DIRS'] = TEMPLATES_DIR_CONFIG_INDIVIDUAL
@ -80,6 +81,7 @@ def test_get_full_name_from_template_accessor_from_multiple_attrs(db, tenant, se
user.attributes.nicknames = ['Milly', 'Molly', 'Minnie']
user.attributes.foo = 'bar'
user.save()
user.refresh_from_db()
templates_conf_individual = settings.TEMPLATES.copy()
templates_conf_individual[0]['DIRS'] = TEMPLATES_DIR_CONFIG_INDIVIDUAL