Skip to content

Commit

Permalink
display verbose name for profile fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jchate6 committed Dec 3, 2024
1 parent f046f4e commit 6bd8604
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
8 changes: 7 additions & 1 deletion tom_common/templates/tom_common/partials/user_data.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ <h4 class="card-title">User Info</h4>
</div>
<div class="card-body">
<dl class="row">
{% for key, value in profile_data.items %}
{% for key, value in user_data.items %}
{% if value %}
<dt class="col-sm-6" >{% verbose_name user key %}</dt>
<dd class="col-sm-6">{{ value }}</dd>
{% endif %}
{% endfor %}
{% for key, value in profile_data.items %}
{% if value %}
<dt class="col-sm-6" >{% verbose_name profile key %}</dt>
<dd class="col-sm-6">{{ value }}</dd>
{% endif %}
{% endfor %}
</dl>
</div>
</div>
Expand Down
6 changes: 5 additions & 1 deletion tom_common/templatetags/tom_common_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.conf import settings
from django_comments.models import Comment
from django.apps import apps
from django.core.exceptions import FieldDoesNotExist
from guardian.shortcuts import get_objects_for_user

from tom_targets.models import Target
Expand Down Expand Up @@ -56,7 +57,10 @@ def verbose_name(instance, field_name):
"""
Displays the more descriptive field name from a Django model field
"""
return instance._meta.get_field(field_name).verbose_name.title()
try:
return instance._meta.get_field(field_name).verbose_name.title()
except FieldDoesNotExist:
return field_name.title()


@register.simple_tag
Expand Down
10 changes: 7 additions & 3 deletions tom_common/templatetags/user_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ def user_data(user):
"""
Returns the user information as a dictionary.
"""
exlcude_fields = ['password', 'last_login', 'id', 'is_active']
user_fields = [field.name for field in user._meta.fields if field.name not in exlcude_fields]

exclude_fields = ['password', 'last_login', 'id', 'is_active', 'user']
user_dict = model_to_dict(user, exclude=exclude_fields)
profile_dict = model_to_dict(user.profile, exclude=exclude_fields)
return {
'user': user,
'profile_data': model_to_dict(user, fields=user_fields),
'profile': user.profile,
'user_data': user_dict,
'profile_data': profile_dict,
}

0 comments on commit 6bd8604

Please sign in to comment.