Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for pagination on the related models #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions papertrail/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.conf.urls import url
from django.contrib import admin
from django.core import serializers
from django.core.paginator import Paginator
from django.db.models import Q
from django.shortcuts import get_object_or_404, render
from django.utils.encoding import force_text
Expand Down Expand Up @@ -143,8 +144,8 @@ def get_actions(self, request):
return actions

def view_papertrail_item(self, request, object_id, extra_context=None):
get_object_or_404(self.model, id=object_id)
return self.view_papertrail(request, self.model.objects.filter(id=object_id))
get_object_or_404(self.model, pk=object_id)
return self.view_papertrail(request, self.model.objects.filter(pk=object_id))

def view_papertrail(self, request, queryset, extra_context=None):
'''
Expand Down Expand Up @@ -178,9 +179,13 @@ def view_papertrail(self, request, queryset, extra_context=None):
obj = None
title = _('Paper Trail: %s %s') % (queryset.count(), opts.verbose_name_plural)

paginator = Paginator(action_list, self.list_per_page)
page_number = request.GET.get("page") or 1
page = paginator.get_page(page_number)
page.page_num = page.number
context = {
'title': title,
'action_list': action_list,
'action_list': page,
'module_name': capfirst(force_text(opts.verbose_name_plural)),
'app_label': app_label,
'opts': opts,
Expand Down
2 changes: 1 addition & 1 deletion papertrail/templates/admin/change_form.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends 'admin:admin/change_form.html' %}
{% extends 'admin/change_form.html' %}
{% load i18n admin_urls papertrail %}

{% block object-tools-items %}
Expand Down
21 changes: 21 additions & 0 deletions papertrail/templates/admin/object_papertrail.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,25 @@
{% endif %}
</div>
</div>

{% block pagination %}
<div class="pagination">
<span class="step-links">
{% if action_list.has_previous %}
<a href="?page={{ action_list.previous_page_number }}">{% trans "previous" %}</a>
{% endif %}

<span class="current">
{% blocktrans with page=action_list.number num_pages=action_list.paginator.num_pages %}
Page {{ page }} of {{ num_pages }}.
{% endblocktrans %}
</span>

{% if action_list.has_next %}
<a href="?page={{ action_list.next_page_number }}">{% trans "next" %}</a>
{% endif %}

</span>
</div>
{% endblock %}
{% endblock %}
2 changes: 2 additions & 0 deletions test_site/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
django
jsonfield
8 changes: 7 additions & 1 deletion test_site/test_app/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from django.contrib import admin

# Register your models here.
from papertrail.admin import AdminEventLoggerMixin
from .models import Simple


@admin.register(Simple)
class SimpleAdmin(AdminEventLoggerMixin, admin.ModelAdmin):
list_per_page = 5
5 changes: 5 additions & 0 deletions test_site/test_app/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from django.db import models

# Create your models here.


class Simple(models.Model):
name = models.CharField(max_length=200, blank=False, null=False)
version = models.IntegerField
36 changes: 18 additions & 18 deletions test_site/test_site/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
ALLOWED_HOSTS = ["localhost"]


# Application definition

INSTALLED_APPS = [
'papertrail',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'test_app',
'papertrail',
]

MIDDLEWARE = [
Expand All @@ -55,18 +55,22 @@

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [os.path.join(BASE_DIR, "../../templates")],
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
"loaders": [
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
],
"string_if_invalid": '<< MISSING VARIABLE "%s" >>' if DEBUG else "",
},
},
}
]

WSGI_APPLICATION = 'test_site.wsgi.application'
Expand All @@ -77,12 +81,8 @@

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'papertrail-test',
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'mydatabase',
}
}

Expand Down