Skip to content

Commit c7329af

Browse files
committed
show the user their DRF API token
1 parent 469395d commit c7329af

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

tom_common/templates/tom_common/partials/user_data.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ <h4 class="card-title">User Info</h4>
2424
<dd class="col-sm-6">{{ value }}</dd>
2525
{% endif %}
2626
{% endfor %}
27+
{% if drf_api_token %}
28+
<dt class="col-sm-6" >API Token</dt>
29+
<dd class="col-sm-6">{{ drf_api_token }}</dd>
30+
{% endif %}
2731
</dl>
2832
</div>
2933
</div>

tom_common/templatetags/user_extras.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,20 @@ def user_data(user):
7575
exclude_fields = ['password', 'last_login', 'id', 'is_active', 'user']
7676
user_dict = model_to_dict(user, exclude=exclude_fields)
7777
profile_dict = model_to_dict(user.profile, exclude=exclude_fields)
78-
return {
78+
79+
# Get the auth_token from the Python descriptor attached (at runtime)
80+
# to the User model by Django as a reverse relation (the related_name)
81+
# to the rest_framework.authtoken.models.Token
82+
drf_api_token = getattr(user, 'auth_token', None)
83+
84+
user_data = {
7985
'user': user,
8086
'profile': user.profile,
8187
'user_data': user_dict,
8288
'profile_data': profile_dict,
89+
'drf_api_token': drf_api_token,
8390
}
91+
return user_data
8492

8593

8694
@register.inclusion_tag('tom_common/partials/app_profiles.html', takes_context=True)

0 commit comments

Comments
 (0)