2015-12-26 18:51:22 +00:00
|
|
|
|
import json
|
2015-12-26 17:46:51 +00:00
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
from django.contrib.admin.models import ADDITION, CHANGE, DELETION, LogEntry
|
|
|
|
|
from django.contrib.admin.utils import quote
|
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
|
from django.test import TestCase, override_settings
|
2015-12-30 15:51:16 +00:00
|
|
|
|
from django.urls import reverse
|
2016-12-29 15:27:49 +00:00
|
|
|
|
from django.utils import translation
|
2015-12-26 17:46:51 +00:00
|
|
|
|
from django.utils.html import escape
|
|
|
|
|
|
|
|
|
|
from .models import Article, ArticleProxy, Site
|
|
|
|
|
|
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
|
@override_settings(ROOT_URLCONF="admin_utils.urls")
|
2015-12-26 17:46:51 +00:00
|
|
|
|
class LogEntryTests(TestCase):
|
2018-11-24 02:24:25 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def setUpTestData(cls):
|
2022-02-03 19:24:19 +00:00
|
|
|
|
cls.user = User.objects.create_superuser(
|
|
|
|
|
username="super", password="secret", email="super@example.com"
|
|
|
|
|
)
|
|
|
|
|
cls.site = Site.objects.create(domain="example.org")
|
2018-11-24 02:24:25 +00:00
|
|
|
|
cls.a1 = Article.objects.create(
|
|
|
|
|
site=cls.site,
|
2015-12-26 17:46:51 +00:00
|
|
|
|
title="Title",
|
2016-10-01 17:32:32 +00:00
|
|
|
|
created=datetime(2008, 3, 12, 11, 54),
|
2015-12-26 17:46:51 +00:00
|
|
|
|
)
|
|
|
|
|
content_type_pk = ContentType.objects.get_for_model(Article).pk
|
|
|
|
|
LogEntry.objects.log_action(
|
2022-02-03 19:24:19 +00:00
|
|
|
|
cls.user.pk,
|
|
|
|
|
content_type_pk,
|
|
|
|
|
cls.a1.pk,
|
|
|
|
|
repr(cls.a1),
|
|
|
|
|
CHANGE,
|
|
|
|
|
change_message="Changed something",
|
2015-12-26 17:46:51 +00:00
|
|
|
|
)
|
2018-11-24 02:24:25 +00:00
|
|
|
|
|
|
|
|
|
def setUp(self):
|
2015-12-26 17:46:51 +00:00
|
|
|
|
self.client.force_login(self.user)
|
|
|
|
|
|
|
|
|
|
def test_logentry_save(self):
|
|
|
|
|
"""
|
|
|
|
|
LogEntry.action_time is a timestamp of the date when the entry was
|
|
|
|
|
created. It shouldn't be updated on a subsequent save().
|
|
|
|
|
"""
|
|
|
|
|
logentry = LogEntry.objects.get(content_type__model__iexact="article")
|
|
|
|
|
action_time = logentry.action_time
|
|
|
|
|
logentry.save()
|
|
|
|
|
self.assertEqual(logentry.action_time, action_time)
|
|
|
|
|
|
2015-12-26 18:51:22 +00:00
|
|
|
|
def test_logentry_change_message(self):
|
|
|
|
|
"""
|
|
|
|
|
LogEntry.change_message is stored as a dumped JSON structure to be able
|
|
|
|
|
to get the message dynamically translated at display time.
|
|
|
|
|
"""
|
|
|
|
|
post_data = {
|
2022-02-03 19:24:19 +00:00
|
|
|
|
"site": self.site.pk,
|
|
|
|
|
"title": "Changed",
|
|
|
|
|
"hist": "Some content",
|
|
|
|
|
"created_0": "2008-03-12",
|
|
|
|
|
"created_1": "11:54",
|
2015-12-26 18:51:22 +00:00
|
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
|
change_url = reverse(
|
|
|
|
|
"admin:admin_utils_article_change", args=[quote(self.a1.pk)]
|
|
|
|
|
)
|
2015-12-26 18:51:22 +00:00
|
|
|
|
response = self.client.post(change_url, post_data)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
self.assertRedirects(response, reverse("admin:admin_utils_article_changelist"))
|
|
|
|
|
logentry = LogEntry.objects.filter(
|
|
|
|
|
content_type__model__iexact="article"
|
|
|
|
|
).latest("id")
|
|
|
|
|
self.assertEqual(logentry.get_change_message(), "Changed Title and History.")
|
|
|
|
|
with translation.override("fr"):
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
logentry.get_change_message(), "Modification de Title et Historique."
|
|
|
|
|
)
|
2015-12-26 18:51:22 +00:00
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
|
add_url = reverse("admin:admin_utils_article_add")
|
|
|
|
|
post_data["title"] = "New"
|
2015-12-26 18:51:22 +00:00
|
|
|
|
response = self.client.post(add_url, post_data)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
self.assertRedirects(response, reverse("admin:admin_utils_article_changelist"))
|
|
|
|
|
logentry = LogEntry.objects.filter(
|
|
|
|
|
content_type__model__iexact="article"
|
|
|
|
|
).latest("id")
|
|
|
|
|
self.assertEqual(logentry.get_change_message(), "Added.")
|
|
|
|
|
with translation.override("fr"):
|
|
|
|
|
self.assertEqual(logentry.get_change_message(), "Ajout.")
|
2015-12-26 18:51:22 +00:00
|
|
|
|
|
2017-06-19 17:42:05 +00:00
|
|
|
|
def test_logentry_change_message_not_json(self):
|
|
|
|
|
"""LogEntry.change_message was a string before Django 1.10."""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
logentry = LogEntry(change_message="non-JSON string")
|
2017-06-19 17:42:05 +00:00
|
|
|
|
self.assertEqual(logentry.get_change_message(), logentry.change_message)
|
|
|
|
|
|
2016-10-01 17:32:32 +00:00
|
|
|
|
def test_logentry_change_message_localized_datetime_input(self):
|
|
|
|
|
"""
|
|
|
|
|
Localized date/time inputs shouldn't affect changed form data detection.
|
|
|
|
|
"""
|
|
|
|
|
post_data = {
|
2022-02-03 19:24:19 +00:00
|
|
|
|
"site": self.site.pk,
|
|
|
|
|
"title": "Changed",
|
|
|
|
|
"hist": "Some content",
|
|
|
|
|
"created_0": "12/03/2008",
|
|
|
|
|
"created_1": "11:54",
|
2016-10-01 17:32:32 +00:00
|
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
|
with translation.override("fr"):
|
|
|
|
|
change_url = reverse(
|
|
|
|
|
"admin:admin_utils_article_change", args=[quote(self.a1.pk)]
|
|
|
|
|
)
|
2016-10-01 17:32:32 +00:00
|
|
|
|
response = self.client.post(change_url, post_data)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
self.assertRedirects(
|
|
|
|
|
response, reverse("admin:admin_utils_article_changelist")
|
|
|
|
|
)
|
|
|
|
|
logentry = LogEntry.objects.filter(
|
|
|
|
|
content_type__model__iexact="article"
|
|
|
|
|
).latest("id")
|
|
|
|
|
self.assertEqual(logentry.get_change_message(), "Changed Title and History.")
|
2016-10-01 17:32:32 +00:00
|
|
|
|
|
2015-12-26 18:51:22 +00:00
|
|
|
|
def test_logentry_change_message_formsets(self):
|
|
|
|
|
"""
|
|
|
|
|
All messages for changed formsets are logged in a change message.
|
|
|
|
|
"""
|
|
|
|
|
a2 = Article.objects.create(
|
|
|
|
|
site=self.site,
|
|
|
|
|
title="Title second article",
|
|
|
|
|
created=datetime(2012, 3, 18, 11, 54),
|
|
|
|
|
)
|
|
|
|
|
post_data = {
|
2022-02-03 19:24:19 +00:00
|
|
|
|
"domain": "example.com", # domain changed
|
|
|
|
|
"admin_articles-TOTAL_FORMS": "5",
|
|
|
|
|
"admin_articles-INITIAL_FORMS": "2",
|
|
|
|
|
"admin_articles-MIN_NUM_FORMS": "0",
|
|
|
|
|
"admin_articles-MAX_NUM_FORMS": "1000",
|
2015-12-26 18:51:22 +00:00
|
|
|
|
# Changed title for 1st article
|
2022-02-03 19:24:19 +00:00
|
|
|
|
"admin_articles-0-id": str(self.a1.pk),
|
|
|
|
|
"admin_articles-0-site": str(self.site.pk),
|
|
|
|
|
"admin_articles-0-title": "Changed Title",
|
2015-12-26 18:51:22 +00:00
|
|
|
|
# Second article is deleted
|
2022-02-03 19:24:19 +00:00
|
|
|
|
"admin_articles-1-id": str(a2.pk),
|
|
|
|
|
"admin_articles-1-site": str(self.site.pk),
|
|
|
|
|
"admin_articles-1-title": "Title second article",
|
|
|
|
|
"admin_articles-1-DELETE": "on",
|
2015-12-26 18:51:22 +00:00
|
|
|
|
# A new article is added
|
2022-02-03 19:24:19 +00:00
|
|
|
|
"admin_articles-2-site": str(self.site.pk),
|
|
|
|
|
"admin_articles-2-title": "Added article",
|
2015-12-26 18:51:22 +00:00
|
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
|
change_url = reverse(
|
|
|
|
|
"admin:admin_utils_site_change", args=[quote(self.site.pk)]
|
|
|
|
|
)
|
2015-12-26 18:51:22 +00:00
|
|
|
|
response = self.client.post(change_url, post_data)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
self.assertRedirects(response, reverse("admin:admin_utils_site_changelist"))
|
2022-10-07 11:05:35 +00:00
|
|
|
|
self.assertSequenceEqual(Article.objects.filter(pk=a2.pk), [])
|
2022-02-03 19:24:19 +00:00
|
|
|
|
logentry = LogEntry.objects.filter(content_type__model__iexact="site").latest(
|
|
|
|
|
"action_time"
|
|
|
|
|
)
|
2015-12-26 18:51:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
json.loads(logentry.change_message),
|
|
|
|
|
[
|
2019-06-14 16:20:29 +00:00
|
|
|
|
{"changed": {"fields": ["Domain"]}},
|
2017-06-09 16:36:09 +00:00
|
|
|
|
{"added": {"object": "Added article", "name": "article"}},
|
2022-02-03 19:24:19 +00:00
|
|
|
|
{
|
|
|
|
|
"changed": {
|
|
|
|
|
"fields": ["Title", "not_a_form_field"],
|
|
|
|
|
"object": "Changed Title",
|
|
|
|
|
"name": "article",
|
|
|
|
|
}
|
|
|
|
|
},
|
2017-06-09 16:36:09 +00:00
|
|
|
|
{"deleted": {"object": "Title second article", "name": "article"}},
|
2022-02-03 19:24:19 +00:00
|
|
|
|
],
|
2015-12-26 18:51:22 +00:00
|
|
|
|
)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
logentry.get_change_message(),
|
2022-02-03 19:24:19 +00:00
|
|
|
|
"Changed Domain. Added article “Added article”. "
|
|
|
|
|
"Changed Title and not_a_form_field for article “Changed Title”. "
|
|
|
|
|
"Deleted article “Title second article”.",
|
2015-12-26 18:51:22 +00:00
|
|
|
|
)
|
2016-08-01 19:40:46 +00:00
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
|
with translation.override("fr"):
|
2015-12-26 18:51:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
logentry.get_change_message(),
|
2019-06-14 16:20:29 +00:00
|
|
|
|
"Modification de Domain. Ajout de article « Added article ». "
|
|
|
|
|
"Modification de Title et not_a_form_field pour l'objet "
|
|
|
|
|
"article « Changed Title ». "
|
2022-02-03 19:24:19 +00:00
|
|
|
|
"Suppression de article « Title second article ».",
|
2015-12-26 18:51:22 +00:00
|
|
|
|
)
|
|
|
|
|
|
2015-12-26 17:46:51 +00:00
|
|
|
|
def test_logentry_get_edited_object(self):
|
|
|
|
|
"""
|
|
|
|
|
LogEntry.get_edited_object() returns the edited object of a LogEntry
|
|
|
|
|
object.
|
|
|
|
|
"""
|
|
|
|
|
logentry = LogEntry.objects.get(content_type__model__iexact="article")
|
|
|
|
|
edited_obj = logentry.get_edited_object()
|
|
|
|
|
self.assertEqual(logentry.object_id, str(edited_obj.pk))
|
|
|
|
|
|
|
|
|
|
def test_logentry_get_admin_url(self):
|
|
|
|
|
"""
|
|
|
|
|
LogEntry.get_admin_url returns a URL to edit the entry's object or
|
2017-02-03 01:43:21 +00:00
|
|
|
|
None for nonexistent (possibly deleted) models.
|
2015-12-26 17:46:51 +00:00
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
logentry = LogEntry.objects.get(content_type__model__iexact="article")
|
|
|
|
|
expected_url = reverse(
|
|
|
|
|
"admin:admin_utils_article_change", args=(quote(self.a1.pk),)
|
|
|
|
|
)
|
2015-12-26 17:46:51 +00:00
|
|
|
|
self.assertEqual(logentry.get_admin_url(), expected_url)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
self.assertIn("article/%d/change/" % self.a1.pk, logentry.get_admin_url())
|
2015-12-26 17:46:51 +00:00
|
|
|
|
|
2017-02-03 01:43:21 +00:00
|
|
|
|
logentry.content_type.model = "nonexistent"
|
2015-12-26 17:46:51 +00:00
|
|
|
|
self.assertIsNone(logentry.get_admin_url())
|
|
|
|
|
|
|
|
|
|
def test_logentry_unicode(self):
|
|
|
|
|
log_entry = LogEntry()
|
|
|
|
|
|
|
|
|
|
log_entry.action_flag = ADDITION
|
2022-02-03 19:24:19 +00:00
|
|
|
|
self.assertTrue(str(log_entry).startswith("Added "))
|
2015-12-26 17:46:51 +00:00
|
|
|
|
|
|
|
|
|
log_entry.action_flag = CHANGE
|
2022-02-03 19:24:19 +00:00
|
|
|
|
self.assertTrue(str(log_entry).startswith("Changed "))
|
2015-12-26 17:46:51 +00:00
|
|
|
|
|
|
|
|
|
log_entry.action_flag = DELETION
|
2022-02-03 19:24:19 +00:00
|
|
|
|
self.assertTrue(str(log_entry).startswith("Deleted "))
|
2015-12-26 17:46:51 +00:00
|
|
|
|
|
|
|
|
|
# Make sure custom action_flags works
|
|
|
|
|
log_entry.action_flag = 4
|
2022-02-03 19:24:19 +00:00
|
|
|
|
self.assertEqual(str(log_entry), "LogEntry Object")
|
2015-12-26 17:46:51 +00:00
|
|
|
|
|
2017-06-19 17:42:05 +00:00
|
|
|
|
def test_logentry_repr(self):
|
|
|
|
|
logentry = LogEntry.objects.first()
|
|
|
|
|
self.assertEqual(repr(logentry), str(logentry.action_time))
|
|
|
|
|
|
2016-09-30 21:55:51 +00:00
|
|
|
|
def test_log_action(self):
|
|
|
|
|
content_type_pk = ContentType.objects.get_for_model(Article).pk
|
|
|
|
|
log_entry = LogEntry.objects.log_action(
|
2022-02-03 19:24:19 +00:00
|
|
|
|
self.user.pk,
|
|
|
|
|
content_type_pk,
|
|
|
|
|
self.a1.pk,
|
|
|
|
|
repr(self.a1),
|
|
|
|
|
CHANGE,
|
|
|
|
|
change_message="Changed something else",
|
2016-09-30 21:55:51 +00:00
|
|
|
|
)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
self.assertEqual(log_entry, LogEntry.objects.latest("id"))
|
2016-09-30 21:55:51 +00:00
|
|
|
|
|
2015-12-26 17:46:51 +00:00
|
|
|
|
def test_recentactions_without_content_type(self):
|
|
|
|
|
"""
|
|
|
|
|
If a LogEntry is missing content_type it will not display it in span
|
|
|
|
|
tag under the hyperlink.
|
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
response = self.client.get(reverse("admin:index"))
|
|
|
|
|
link = reverse("admin:admin_utils_article_change", args=(quote(self.a1.pk),))
|
|
|
|
|
should_contain = """<a href="%s">%s</a>""" % (
|
|
|
|
|
escape(link),
|
|
|
|
|
escape(repr(self.a1)),
|
|
|
|
|
)
|
2015-12-26 17:46:51 +00:00
|
|
|
|
self.assertContains(response, should_contain)
|
|
|
|
|
should_contain = "Article"
|
|
|
|
|
self.assertContains(response, should_contain)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
logentry = LogEntry.objects.get(content_type__model__iexact="article")
|
2015-12-26 17:46:51 +00:00
|
|
|
|
# If the log entry doesn't have a content type it should still be
|
|
|
|
|
# possible to view the Recent Actions part (#10275).
|
|
|
|
|
logentry.content_type = None
|
|
|
|
|
logentry.save()
|
|
|
|
|
|
2018-02-07 19:20:04 +00:00
|
|
|
|
should_contain = should_contain.encode()
|
|
|
|
|
counted_presence_before = response.content.count(should_contain)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
response = self.client.get(reverse("admin:index"))
|
2018-02-07 19:20:04 +00:00
|
|
|
|
counted_presence_after = response.content.count(should_contain)
|
2015-12-26 17:46:51 +00:00
|
|
|
|
self.assertEqual(counted_presence_before - 1, counted_presence_after)
|
|
|
|
|
|
|
|
|
|
def test_proxy_model_content_type_is_used_for_log_entries(self):
|
|
|
|
|
"""
|
|
|
|
|
Log entries for proxy models should have the proxy model's contenttype
|
|
|
|
|
(#21084).
|
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
proxy_content_type = ContentType.objects.get_for_model(
|
|
|
|
|
ArticleProxy, for_concrete_model=False
|
|
|
|
|
)
|
2015-12-26 17:46:51 +00:00
|
|
|
|
post_data = {
|
2022-02-03 19:24:19 +00:00
|
|
|
|
"site": self.site.pk,
|
|
|
|
|
"title": "Foo",
|
|
|
|
|
"hist": "Bar",
|
|
|
|
|
"created_0": "2015-12-25",
|
|
|
|
|
"created_1": "00:00",
|
2015-12-26 17:46:51 +00:00
|
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
|
changelist_url = reverse("admin:admin_utils_articleproxy_changelist")
|
2015-12-26 17:46:51 +00:00
|
|
|
|
|
|
|
|
|
# add
|
2022-02-03 19:24:19 +00:00
|
|
|
|
proxy_add_url = reverse("admin:admin_utils_articleproxy_add")
|
2015-12-26 17:46:51 +00:00
|
|
|
|
response = self.client.post(proxy_add_url, post_data)
|
|
|
|
|
self.assertRedirects(response, changelist_url)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
proxy_addition_log = LogEntry.objects.latest("id")
|
2015-12-26 17:46:51 +00:00
|
|
|
|
self.assertEqual(proxy_addition_log.action_flag, ADDITION)
|
|
|
|
|
self.assertEqual(proxy_addition_log.content_type, proxy_content_type)
|
|
|
|
|
|
|
|
|
|
# change
|
|
|
|
|
article_id = proxy_addition_log.object_id
|
2022-02-03 19:24:19 +00:00
|
|
|
|
proxy_change_url = reverse(
|
|
|
|
|
"admin:admin_utils_articleproxy_change", args=(article_id,)
|
|
|
|
|
)
|
|
|
|
|
post_data["title"] = "New"
|
2015-12-26 17:46:51 +00:00
|
|
|
|
response = self.client.post(proxy_change_url, post_data)
|
|
|
|
|
self.assertRedirects(response, changelist_url)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
proxy_change_log = LogEntry.objects.latest("id")
|
2015-12-26 17:46:51 +00:00
|
|
|
|
self.assertEqual(proxy_change_log.action_flag, CHANGE)
|
|
|
|
|
self.assertEqual(proxy_change_log.content_type, proxy_content_type)
|
|
|
|
|
|
|
|
|
|
# delete
|
2022-02-03 19:24:19 +00:00
|
|
|
|
proxy_delete_url = reverse(
|
|
|
|
|
"admin:admin_utils_articleproxy_delete", args=(article_id,)
|
|
|
|
|
)
|
|
|
|
|
response = self.client.post(proxy_delete_url, {"post": "yes"})
|
2015-12-26 17:46:51 +00:00
|
|
|
|
self.assertRedirects(response, changelist_url)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
proxy_delete_log = LogEntry.objects.latest("id")
|
2015-12-26 17:46:51 +00:00
|
|
|
|
self.assertEqual(proxy_delete_log.action_flag, DELETION)
|
|
|
|
|
self.assertEqual(proxy_delete_log.content_type, proxy_content_type)
|
2018-05-03 12:31:31 +00:00
|
|
|
|
|
|
|
|
|
def test_action_flag_choices(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
|
tests = ((1, "Addition"), (2, "Change"), (3, "Deletion"))
|
2018-05-03 12:31:31 +00:00
|
|
|
|
for action_flag, display_name in tests:
|
|
|
|
|
with self.subTest(action_flag=action_flag):
|
|
|
|
|
log = LogEntry(action_flag=action_flag)
|
|
|
|
|
self.assertEqual(log.get_action_flag_display(), display_name)
|