@@ -475,29 +475,41 @@ def create_persistent_share(context, target):
475475 return {'form' : form , 'target' : target }
476476
477477
478- @register .inclusion_tag ('tom_targets/partials/module_buttons.html' )
479- def get_buttons (target ):
478+ @register .inclusion_tag ('tom_targets/partials/module_buttons.html' , takes_context = True )
479+ def get_buttons (context ):
480480 """
481- Returns a list of buttons from imported modules to be displayed on the target detail page.
482- In order to add a button to the target detail page, an app must contain an integration points attribute.
483- The Integration Points attribute must be a dictionary with a key of 'target_detail_button':
484- 'target_detail_button' = {'namespace': <<redirect path, i.e. 'app:name'>>,
485- 'title': <<Button title>>,
486- 'class': <<Button class i.e 'btn btn-info'>>,
487- 'text': <<What you want the button to actually say>>,
488- }
481+ Imports the target detail Button content from relevant apps into the template.
489482
483+ Each target_button should be contained in a list of dictionaries in an app's apps.py `target_detail_buttons` method.
484+ Each target_button dictionary should contain a 'partial' key with the path to the html partial template and
485+ optionally a 'context' key with the path to the context processor class (typically a templatetag).
486+
487+ FOR EXAMPLE:
488+ [{'partial': 'path/to/partial.html',
489+ 'context': 'path/to/context/data/method'}]
490490 """
491- button_list = []
491+ target_buttons_to_display = []
492492 for app in apps .get_app_configs ():
493493 try :
494- button_info = app .target_detail_buttons ()
495- if button_info :
496- button_list .append (button_info )
494+ target_buttons = app .target_detail_buttons ()
497495 except AttributeError :
498- pass
499-
500- return {'target' : target , 'button_list' : button_list }
496+ continue
497+ if target_buttons :
498+ for button in target_buttons :
499+ new_context = {}
500+ if button .get ('context' ):
501+ try :
502+ context_method = import_string (button .get ('context' ))
503+ except ImportError as e :
504+ logger .warning (f'WARNING: Could not import context for { app .name } target detail button from '
505+ f'{ button ["context" ]} .\n '
506+ f'{ e } ' )
507+ continue
508+ new_context = context_method (context )
509+ target_buttons_to_display .append ({'partial' : button ['partial' ],
510+ 'context' : new_context })
511+ context ['target_buttons_to_display' ] = target_buttons_to_display
512+ return context
501513
502514
503515@register .filter
0 commit comments