-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#9072: Implement a mechanism for dynamically registering model detail…
… views
- Loading branch information
1 parent
664d5db
commit 0d7851e
Showing
7 changed files
with
158 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{% for tab in tabs %} | ||
<li role="presentation" class="nav-item"> | ||
<a href="{{ tab.url }}" class="nav-link{% if tab.is_active %} active{% endif %}"> | ||
{{ tab.label }} | ||
{% if tab.badge_value %}{% badge tab.badge_value %}{% endif %} | ||
</a> | ||
</li> | ||
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from django import template | ||
from django.core.exceptions import ImproperlyConfigured | ||
from django.urls import reverse | ||
|
||
from extras.registry import registry | ||
|
||
register = template.Library() | ||
|
||
|
||
# | ||
# Object detail view tabs | ||
# | ||
|
||
@register.inclusion_tag('tabs/model_view_tabs.html', takes_context=True) | ||
def model_view_tabs(context, instance): | ||
app_label = instance._meta.app_label | ||
model_name = instance._meta.model_name | ||
user = context['request'].user | ||
tabs = [] | ||
|
||
# Retrieve registered views for this model | ||
try: | ||
views = registry['views'][app_label][model_name] | ||
except KeyError: | ||
# No views have been registered for this model | ||
views = [] | ||
|
||
# Compile a list of tabs to be displayed in the UI | ||
for view in views: | ||
if view['tab_label'] and (not view['tab_permission'] or user.has_perm(view['tab_permission'])): | ||
|
||
# Determine the value of the tab's badge (if any) | ||
if view['tab_badge'] and callable(view['tab_badge']): | ||
badge_value = view['tab_badge'](instance) | ||
elif view['tab_badge']: | ||
badge_value = view['tab_badge'] | ||
else: | ||
badge_value = None | ||
|
||
tabs.append({ | ||
'name': view['name'], | ||
'url': reverse(f"{app_label}:{model_name}_{view['name']}", args=[instance.pk]), | ||
'label': view['tab_label'], | ||
'badge_value': badge_value, | ||
'is_active': context.get('active_tab') == view['name'], | ||
}) | ||
|
||
return { | ||
'tabs': tabs, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from django.urls import path | ||
from django.utils.module_loading import import_string | ||
from django.views.generic import View | ||
|
||
from extras.registry import registry | ||
|
||
|
||
def get_model_urls(app_label, model_name): | ||
""" | ||
Return a list of URL paths for detail views registered to the given model. | ||
Args: | ||
app_label: App/plugin name | ||
model_name: Model name | ||
""" | ||
paths = [] | ||
|
||
# Retrieve registered views for this model | ||
try: | ||
views = registry['views'][app_label][model_name] | ||
except KeyError: | ||
# No views have been registered for this model | ||
views = [] | ||
|
||
for view in views: | ||
# Import the view class or function | ||
callable = import_string(view['path']) | ||
if issubclass(callable, View): | ||
callable = callable.as_view() | ||
# Create a path to the view | ||
paths.append( | ||
path(f"{view['name']}/", callable, name=f"{model_name}_{view['name']}", kwargs=view['kwargs']) | ||
) | ||
|
||
return paths |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters