|
| 1 | +from typing import Any |
| 2 | + |
1 | 3 | from django import http
|
2 | 4 | from django.apps import apps as django_apps
|
3 | 5 | from django.conf import settings
|
|
6 | 8 | from django.contrib.admin.utils import unquote
|
7 | 9 | from django.contrib.auth import get_permission_codename, get_user_model
|
8 | 10 | from django.core.exceptions import PermissionDenied
|
| 11 | +from django.db.models import QuerySet |
9 | 12 | from django.shortcuts import get_object_or_404, render
|
10 | 13 | from django.urls import re_path, reverse
|
11 | 14 | from django.utils.encoding import force_str
|
12 | 15 | from django.utils.html import mark_safe
|
13 | 16 | from django.utils.text import capfirst
|
14 | 17 | from django.utils.translation import gettext as _
|
15 | 18 |
|
| 19 | +from .manager import HistoryManager |
16 | 20 | from .utils import get_history_manager_for_model, get_history_model_for_model
|
17 | 21 |
|
18 | 22 | SIMPLE_HISTORY_EDIT = getattr(settings, "SIMPLE_HISTORY_EDIT", False)
|
@@ -47,10 +51,9 @@ def history_view(self, request, object_id, extra_context=None):
|
47 | 51 | pk_name = opts.pk.attname
|
48 | 52 | history = getattr(model, model._meta.simple_history_manager_attribute)
|
49 | 53 | object_id = unquote(object_id)
|
50 |
| - historical_records = history.filter(**{pk_name: object_id}) |
51 |
| - if not isinstance(history.model.history_user, property): |
52 |
| - # Only select_related when history_user is a ForeignKey (not a property) |
53 |
| - historical_records = historical_records.select_related("history_user") |
| 54 | + historical_records = self.get_history_queryset( |
| 55 | + request, history, pk_name, object_id |
| 56 | + ) |
54 | 57 | history_list_display = getattr(self, "history_list_display", [])
|
55 | 58 | # If no history was found, see whether this object even exists.
|
56 | 59 | try:
|
@@ -99,6 +102,25 @@ def history_view(self, request, object_id, extra_context=None):
|
99 | 102 | request, self.object_history_template, context, **extra_kwargs
|
100 | 103 | )
|
101 | 104 |
|
| 105 | + def get_history_queryset( |
| 106 | + self, request, history_manager: HistoryManager, pk_name: str, object_id: Any |
| 107 | + ) -> QuerySet: |
| 108 | + """ |
| 109 | + Return a ``QuerySet`` of all historical records that should be listed in the |
| 110 | + ``object_history_list_template`` template. |
| 111 | + This is used by ``history_view()``. |
| 112 | +
|
| 113 | + :param request: |
| 114 | + :param history_manager: |
| 115 | + :param pk_name: The name of the original model's primary key field. |
| 116 | + :param object_id: The primary key of the object whose history is listed. |
| 117 | + """ |
| 118 | + qs = history_manager.filter(**{pk_name: object_id}) |
| 119 | + if not isinstance(history_manager.model.history_user, property): |
| 120 | + # Only select_related when history_user is a ForeignKey (not a property) |
| 121 | + qs = qs.select_related("history_user") |
| 122 | + return qs |
| 123 | + |
102 | 124 | def history_view_title(self, request, obj):
|
103 | 125 | if self.revert_disabled(request, obj) and not SIMPLE_HISTORY_EDIT:
|
104 | 126 | return _("View history: %s") % force_str(obj)
|
|
0 commit comments