From 6b3a3d37e2ea0cb4ba8d63aea4c1f81ec6ca83ae Mon Sep 17 00:00:00 2001 From: Joey Chatelain Date: Tue, 21 Jan 2025 15:44:19 -0800 Subject: [PATCH] generalize methods for displaying partial context from apps --- .../auth/partials/app_user_lists.html | 5 ++ tom_common/templates/auth/user_list.html | 1 + .../tom_common/partials/app_profiles.html | 2 +- ...ile_card.html => include_app_partial.html} | 8 ++-- tom_common/templatetags/user_extras.py | 47 ++++++++++++++++--- 5 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 tom_common/templates/auth/partials/app_user_lists.html rename tom_common/templates/tom_common/partials/{include_profile_card.html => include_app_partial.html} (52%) diff --git a/tom_common/templates/auth/partials/app_user_lists.html b/tom_common/templates/auth/partials/app_user_lists.html new file mode 100644 index 000000000..73d986661 --- /dev/null +++ b/tom_common/templates/auth/partials/app_user_lists.html @@ -0,0 +1,5 @@ +{% load user_extras %} +{% for user_list in user_lists_to_display %} + {% show_individual_app_partial user_list %} +
+{% endfor %} \ No newline at end of file diff --git a/tom_common/templates/auth/user_list.html b/tom_common/templates/auth/user_list.html index f0cc91395..ae111fd1a 100644 --- a/tom_common/templates/auth/user_list.html +++ b/tom_common/templates/auth/user_list.html @@ -2,6 +2,7 @@ {% load user_extras %} {% block title %}User Management{% endblock %} {% block content %} +{% include_app_user_lists %} {% group_list %}
{% user_list %} diff --git a/tom_common/templates/tom_common/partials/app_profiles.html b/tom_common/templates/tom_common/partials/app_profiles.html index 0d3b8ebd3..c4353b035 100644 --- a/tom_common/templates/tom_common/partials/app_profiles.html +++ b/tom_common/templates/tom_common/partials/app_profiles.html @@ -5,7 +5,7 @@
{% for profile in profiles_to_display %} - {% show_individual_app_profile profile %} + {% show_individual_app_partial profile %} {% comment %} Start new column halfway through the list of profiles. diff --git a/tom_common/templates/tom_common/partials/include_profile_card.html b/tom_common/templates/tom_common/partials/include_app_partial.html similarity index 52% rename from tom_common/templates/tom_common/partials/include_profile_card.html rename to tom_common/templates/tom_common/partials/include_app_partial.html index eef696a95..aeed84cd3 100644 --- a/tom_common/templates/tom_common/partials/include_profile_card.html +++ b/tom_common/templates/tom_common/partials/include_app_partial.html @@ -1,7 +1,7 @@ {% comment %} - This partial template includes another partial from the context specific to a specific App's profile. - This allows the partial to be rendered with only the context specified by the app, without interference from - other app profile contexts. + This partial template includes another partial from the context specific to a specific App's partial. + This allows the partial to be rendered with only the context specified by the app, without interference from contexts + from other apps. {% endcomment %} -{% include profile_partial %} +{% include app_partial %} diff --git a/tom_common/templatetags/user_extras.py b/tom_common/templatetags/user_extras.py index 85b1731e6..647fb31ff 100644 --- a/tom_common/templatetags/user_extras.py +++ b/tom_common/templatetags/user_extras.py @@ -31,6 +31,41 @@ def user_list(context): } +@register.inclusion_tag('auth/partials/app_user_lists.html', takes_context=True) +def include_app_user_lists(context): + """ + Imports the user list content from relevant apps into the template. + + Each user_list should be contained in a list of dictionaries in an app's apps.py `user_lists` method. + Each user_list 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. + + FOR EXAMPLE: + [{'partial': 'path/to/partial.html', + 'context': 'path/to/context/data/method'}] + """ + user_lists_to_display = [] + for app in apps.get_app_configs(): + try: + user_lists = app.user_lists() + except AttributeError: + continue + if user_lists: + for app_users in user_lists: + try: + context_method = import_string(app_users['context']) + except ImportError: + logger.warning(f'WARNING: Could not import context for {app.name} user list from ' + f'{app_users["context"]}.\n' + f'Are you sure you have the right path?') + continue + new_context = context_method(context) + user_lists_to_display.append({'partial': app_users['partial'], 'context': new_context}) + + context['user_lists_to_display'] = user_lists_to_display + return context + + @register.inclusion_tag('tom_common/partials/user_data.html') def user_data(user): """ @@ -84,12 +119,12 @@ def show_app_profiles(context, user): return context -@register.inclusion_tag('tom_common/partials/include_profile_card.html', takes_context=True) -def show_individual_app_profile(context, profile_data): +@register.inclusion_tag('tom_common/partials/include_app_partial.html', takes_context=True) +def show_individual_app_partial(context, app_partial_data): """ - An Inclusion tag for setting the unique context for each app's user profile. + An Inclusion tag for setting the unique context for an app's partial. """ - for item in profile_data['context']: - context[item] = profile_data['context'][item] - context['profile_partial'] = profile_data['partial'] + for item in app_partial_data['context']: + context[item] = app_partial_data['context'][item] + context['app_partial'] = app_partial_data['partial'] return context