Toggle full name display of users (#5927)

* Add option to display full names instead of usernames

* InvenTree Setting added to allow for admins to choose full names
  over usernames for display in the web GUI

* Fix FA icon for settings page

* Remove debug print
This commit is contained in:
Lavissa 2023-11-16 03:18:01 +01:00 committed by GitHub
parent 0b168c1d9a
commit e2b29cbd95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 2 deletions

View File

@ -1874,6 +1874,12 @@ class InvenTreeSetting(BaseInvenTreeSetting):
] ]
}, },
'DISPLAY_FULL_NAMES': {
'name': _('Display Users full names'),
'description': _('Display Users full names instead of usernames'),
'default': False,
'validator': bool
}
} }
typ = 'inventree' typ = 'inventree'

View File

@ -17,6 +17,7 @@
{% include "InvenTree/settings/setting.html" with key="INVENTREE_INSTANCE" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_INSTANCE" icon="fa-info-circle" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_INSTANCE_TITLE" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_INSTANCE_TITLE" icon="fa-info-circle" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_RESTRICT_ABOUT" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_RESTRICT_ABOUT" icon="fa-info-circle" %}
{% include "InvenTree/settings/setting.html" with key="DISPLAY_FULL_NAMES" icon="fa-font" %}
<tr><td colspan='5'></td></tr> <tr><td colspan='5'></td></tr>
{% include "InvenTree/settings/setting.html" with key="INVENTREE_UPDATE_CHECK_INTERVAL" icon="fa-calendar-alt" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_UPDATE_CHECK_INTERVAL" icon="fa-calendar-alt" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_DOWNLOAD_FROM_URL" icon="fa-cloud-download-alt" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_DOWNLOAD_FROM_URL" icon="fa-cloud-download-alt" %}

View File

@ -6,7 +6,7 @@ import logging
from django.conf import settings from django.conf import settings
from django.contrib import admin from django.contrib import admin
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, Permission from django.contrib.auth.models import Group, Permission, User
from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core.cache import cache from django.core.cache import cache
@ -21,6 +21,7 @@ from django.utils.translation import gettext_lazy as _
from rest_framework.authtoken.models import Token as AuthToken from rest_framework.authtoken.models import Token as AuthToken
import common.models as common_models
import InvenTree.helpers import InvenTree.helpers
import InvenTree.models import InvenTree.models
from InvenTree.ready import canAppAccessDatabase from InvenTree.ready import canAppAccessDatabase
@ -28,6 +29,22 @@ from InvenTree.ready import canAppAccessDatabase
logger = logging.getLogger("inventree") logger = logging.getLogger("inventree")
# OVERRIDE START
# Overrides Django User model __str__ with a custom function to be able to change
# string representation of a user
def user_model_str(self):
"""Function to override the default Django User __str__"""
if common_models.InvenTreeSetting.get_setting('DISPLAY_FULL_NAMES'):
if self.first_name or self.last_name:
return f'{self.first_name} {self.last_name}'
return self.username
User.add_to_class("__str__", user_model_str) # Overriding User.__str__
# OVERRIDE END
def default_token(): def default_token():
"""Generate a default value for the token""" """Generate a default value for the token"""
return ApiToken.generate_key() return ApiToken.generate_key()
@ -785,10 +802,16 @@ class Owner(models.Model):
def __str__(self): def __str__(self):
"""Defines the owner string representation.""" """Defines the owner string representation."""
return f'{self.owner} ({self.owner_type.name})' if self.owner_type.name == 'user' and common_models.InvenTreeSetting.get_setting('DISPLAY_FULL_NAMES'):
display_name = self.owner.get_full_name()
else:
display_name = str(self.owner)
return f'{display_name} ({self.owner_type.name})'
def name(self): def name(self):
"""Return the 'name' of this owner.""" """Return the 'name' of this owner."""
if self.owner_type.name == 'user' and common_models.InvenTreeSetting.get_setting('DISPLAY_FULL_NAMES'):
return self.owner.get_full_name()
return str(self.owner) return str(self.owner)
def label(self): def label(self):