Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add export/import links to admin app-list menu
To activate, 'import_export' must be included in INSTALLED_APPS before 'django.contrib.admin'. This prioritizes custom app_list template, enabling the links.
- Loading branch information
1 parent
65670df
commit 873aad1
Showing
3 changed files
with
120 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
{% load i18n %} | ||
{% load admin_urls %} | ||
{% load import_export_filters %} | ||
|
||
<style> | ||
td>span.disabled { | ||
color: gray; | ||
filter: grayscale(100%); | ||
padding-right: .6em; | ||
} | ||
</style> | ||
|
||
{% if app_list %} | ||
{% for app in app_list %} | ||
<div class="app-{{ app.app_label }} module | ||
{% if app.app_url in request.path|urlencode %}current-app{% endif %}"> | ||
<table> | ||
<caption> | ||
<a href="{{ app.app_url }}" | ||
class="section" | ||
title="{% blocktranslate with name=app.name %} | ||
Models in the {{ name }} application | ||
{% endblocktranslate %}">{{ app.name }}</a> | ||
</caption> | ||
{% for model in app.models %} | ||
<tr class="model-{{ model.object_name|lower }} | ||
{% if model.admin_url in request.path|urlencode %}current-model{% endif %}"> | ||
{% if model.admin_url %} | ||
<th scope="row"> | ||
<a href="{{ model.admin_url }}" | ||
{% if model.admin_url in request.path|urlencode %}aria-current="page"{% endif %}>{{ model.name }}</a> | ||
</th> | ||
{% else %} | ||
<th scope="row">{{ model.name }}</th> | ||
{% endif %} | ||
|
||
{% if model.add_url %} | ||
<td> | ||
<a href="{{ model.add_url }}" class="addlink">{% translate "Add" %}</a> | ||
</td> | ||
{% else %} | ||
<td></td> | ||
{% endif %} | ||
|
||
{% if model|is_importable:request and model.add_url %} | ||
<td> | ||
<a href='{% url model|get_opts|admin_urlname:"import" %}' | ||
class="addlink">{% trans "Import" %}</a> | ||
</td> | ||
{% elif model.add_url %} | ||
<td> | ||
<span class="addlink disabled">{% translate "Import" %}</span> | ||
</td> | ||
{% else %} | ||
<td></td> | ||
{% endif %} | ||
|
||
{% if model.admin_url and show_changelinks %} | ||
{% if model.view_only %} | ||
<td> | ||
<a href="{{ model.admin_url }}" class="viewlink">{% translate "View" %}</a> | ||
</td> | ||
{% else %} | ||
<td> | ||
<a href="{{ model.admin_url }}" class="changelink">{% translate "Change" %}</a> | ||
</td> | ||
{% endif %} | ||
{% elif show_changelinks %} | ||
<td></td> | ||
{% endif %} | ||
|
||
{% if model|is_exportable:request and model.admin_url and show_changelinks %} | ||
<td> | ||
<a href='{% url model|get_opts|admin_urlname:"export" %}' | ||
class="viewlink">{% translate "Export" %}</a> | ||
</td> | ||
{% elif model.admin_url %} | ||
<td> | ||
<span class="hidelink disabled">{% translate "Export" %}</span> | ||
</td> | ||
{% elif show_changelinks %} | ||
<td></td> | ||
{% endif %} | ||
</tr> | ||
{% endfor %} | ||
</table> | ||
</div> | ||
{% endfor %} | ||
{% else %} | ||
<p>{% translate "You don’t have permission to view or edit anything." %}</p> | ||
{% endif %} |
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,28 @@ | ||
from django import template | ||
from django.contrib import admin | ||
|
||
register = template.Library() | ||
|
||
|
||
@register.filter | ||
def is_exportable(obj, request): | ||
model_class = obj["model"] | ||
admin_class = admin.site._registry[model_class] | ||
return hasattr( | ||
admin_class, "has_export_permission" | ||
) and admin_class.has_export_permission(request) | ||
|
||
|
||
@register.filter | ||
def is_importable(obj, request): | ||
model_class = obj["model"] | ||
admin_class = admin.site._registry[model_class] | ||
return hasattr( | ||
admin_class, "has_import_permission" | ||
) and admin_class.has_import_permission(request) | ||
|
||
|
||
@register.filter | ||
def get_opts(obj): | ||
model_class = obj["model"] | ||
return model_class._meta |
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