Skip to content

Commit

Permalink
feat: Add export/import links to admin app-list menu
Browse files Browse the repository at this point in the history
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
mh-firouzjah committed Apr 13, 2024
1 parent 65670df commit 873aad1
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
91 changes: 91 additions & 0 deletions import_export/templates/admin/app_list.html
@@ -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 %}
28 changes: 28 additions & 0 deletions import_export/templatetags/import_export_filters.py
@@ -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
2 changes: 1 addition & 1 deletion tests/settings.py
Expand Up @@ -4,13 +4,13 @@
import django

INSTALLED_APPS = [
"import_export",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.sites",
"import_export",
"core",
]

Expand Down

0 comments on commit 873aad1

Please sign in to comment.