From ecdcd7d0aa17a419987b5df68d015008979fca9c Mon Sep 17 00:00:00 2001 From: Esau Rodriguez Date: Fri, 5 Jul 2019 11:29:50 +0100 Subject: [PATCH 1/2] Support pagination and fix a few issues. --- papertrail/admin.py | 11 ++++-- papertrail/templates/admin/change_form.html | 2 +- .../templates/admin/object_papertrail.html | 17 +++++++++ test_site/requirements.txt | 2 ++ test_site/test_app/admin.py | 8 ++++- test_site/test_app/models.py | 5 +++ test_site/test_site/settings.py | 36 +++++++++---------- 7 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 test_site/requirements.txt diff --git a/papertrail/admin.py b/papertrail/admin.py index d30b8b8..2958807 100644 --- a/papertrail/admin.py +++ b/papertrail/admin.py @@ -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 @@ -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): ''' @@ -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, diff --git a/papertrail/templates/admin/change_form.html b/papertrail/templates/admin/change_form.html index b30672e..e64a88c 100644 --- a/papertrail/templates/admin/change_form.html +++ b/papertrail/templates/admin/change_form.html @@ -1,4 +1,4 @@ -{% extends 'admin:admin/change_form.html' %} +{% extends 'admin/change_form.html' %} {% load i18n admin_urls papertrail %} {% block object-tools-items %} diff --git a/papertrail/templates/admin/object_papertrail.html b/papertrail/templates/admin/object_papertrail.html index 018a651..25c203d 100644 --- a/papertrail/templates/admin/object_papertrail.html +++ b/papertrail/templates/admin/object_papertrail.html @@ -100,4 +100,21 @@ {% endif %} + + {% endblock %} diff --git a/test_site/requirements.txt b/test_site/requirements.txt new file mode 100644 index 0000000..d21b5ac --- /dev/null +++ b/test_site/requirements.txt @@ -0,0 +1,2 @@ +django +jsonfield diff --git a/test_site/test_app/admin.py b/test_site/test_app/admin.py index 8c38f3f..42ec377 100644 --- a/test_site/test_app/admin.py +++ b/test_site/test_app/admin.py @@ -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 diff --git a/test_site/test_app/models.py b/test_site/test_app/models.py index 71a8362..afe76aa 100644 --- a/test_site/test_app/models.py +++ b/test_site/test_app/models.py @@ -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 diff --git a/test_site/test_site/settings.py b/test_site/test_site/settings.py index ae74753..f917de1 100644 --- a/test_site/test_site/settings.py +++ b/test_site/test_site/settings.py @@ -25,12 +25,13 @@ # 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', @@ -38,7 +39,6 @@ 'django.contrib.messages', 'django.contrib.staticfiles', 'test_app', - 'papertrail', ] MIDDLEWARE = [ @@ -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' @@ -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', } } From 3ca5864c379980eea4dcb06c6daeac98ced8aed5 Mon Sep 17 00:00:00 2001 From: Esau Rodriguez Date: Fri, 5 Jul 2019 11:51:54 +0100 Subject: [PATCH 2/2] Adding translations and extensibility --- papertrail/templates/admin/object_papertrail.html | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/papertrail/templates/admin/object_papertrail.html b/papertrail/templates/admin/object_papertrail.html index 25c203d..25f4fe1 100644 --- a/papertrail/templates/admin/object_papertrail.html +++ b/papertrail/templates/admin/object_papertrail.html @@ -101,20 +101,24 @@ + {% block pagination %} + {% endblock %} {% endblock %}