Skip to content

Commit

Permalink
add profile integration point.
Browse files Browse the repository at this point in the history
  • Loading branch information
jchate6 committed Dec 19, 2024
1 parent 820fa5d commit d401a03
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
12 changes: 12 additions & 0 deletions tom_common/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ def ready(self):
plotly_theme = 'plotly_white'

pio.templates.default = plotly_theme

def profile_details(self):
"""
Integration point for adding items to the user profile page.
This method should return a list of dictionaries that include a `partial` key pointing to the path of the html
profile partial. The `context` key should point to the dot separated string path to the templatetag that will
return a dictionary containing new context for the accompanying partial.
Typically, this partial will be a bootstrap card displaying some app specific user data.
"""
return [{'partial': 'tom_common/partials/user_data.html',
'context': 'tom_common.templatetags.user_extras.user_data'}]
19 changes: 19 additions & 0 deletions tom_common/templates/tom_common/partials/profile_app_addons.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% load user_extras %}
{% load bootstrap4 %}

<div class="container">

<div class="row">
{% for profile in profile_list %}
<div class="col-6">
{% include profile %}
</div>
{% if forloop.counter|divisibleby:2 %}
</div>
<div class="row">
{% endif %}
{% empty %}
This user has no profile.
{% endfor %}
</div>
</div>
10 changes: 1 addition & 9 deletions tom_common/templates/tom_common/user_profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ <h3>
{% endif %}
</h3>

<div class="container">
<div class="row">
<div class="col-lg">
{% user_data user %}
</div>
<div class="col-lg">
</div>
</div>
</div>
{% profile_app_addons user %}

{% endblock %}
37 changes: 37 additions & 0 deletions tom_common/templatetags/user_extras.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import logging
from django import template
from django.contrib.auth.models import Group, User
from django.forms.models import model_to_dict
from django.apps import apps
from django.utils.module_loading import import_string

register = template.Library()
logger = logging.getLogger(__name__)


@register.inclusion_tag('auth/partials/group_list.html', takes_context=True)
Expand Down Expand Up @@ -42,3 +46,36 @@ def user_data(user):
'user_data': user_dict,
'profile_data': profile_dict,
}


@register.inclusion_tag('tom_common/partials/profile_app_addons.html', takes_context=True)
def profile_app_addons(context, user):
"""
Imports the profile content from relevant apps.
Each profile should be contained in a list of dictionaries in an app's apps.py `profile_details` method.
Each profile dictionary should contain a 'context' key with the path to the context processor class (typically a
templatetag), and a 'partial' key with the path to the html partial template.
"""
partial_list = []
for app in apps.get_app_configs():
try:
profile_details = app.profile_details()
if profile_details:
for profile in profile_details:
try:
clazz = import_string(profile['context'])
except ImportError:
logger.warning(f'WARNING: Could not import context for {app.name} profile from '
f'{profile["context"]}.\n'
f'Are you sure you have the right path?')
continue
new_context = clazz(user)
for item in new_context:
context[item] = new_context[item]
partial_list.append(profile['partial'])
except AttributeError:
pass
context['user'] = user
context['profile_list'] = partial_list
return context

0 comments on commit d401a03

Please sign in to comment.