Skip to content
Snippets Groups Projects
Commit 0bee5420 authored by Roang's avatar Roang
Browse files

Update log_activity mixin with rich Markdown

Use the rich library to output the debug information in a more
human-readable format. Unfortunately, the logger does not support rich
Markdown text, so we need to output this via the console.
parent 61bf1b03
Branches
Tags v10.0.4
No related merge requests found
...@@ -2,13 +2,12 @@ import logging ...@@ -2,13 +2,12 @@ import logging
from typing import TYPE_CHECKING, TypedDict from typing import TYPE_CHECKING, TypedDict
from pydantic import ConfigDict, TypeAdapter, ValidationError from pydantic import ConfigDict, TypeAdapter, ValidationError
from rich.console import Console
from rich.markdown import Markdown
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.db import models from django.db import models
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
if TYPE_CHECKING: if TYPE_CHECKING:
...@@ -180,24 +179,25 @@ class ActivityLogMixin: ...@@ -180,24 +179,25 @@ class ActivityLogMixin:
changes=changes, changes=changes,
) )
# debug output if logger.isEnabledFor(logging.TRACE):
if settings.DEBUG: # log the changes in a human-readable format
msg = ( # We need to output this via console as the logger does not support rich Markdown text
format_html('<b>DEBUG INFO:</b> <em>changes</em> on: {0}\n', self) console = Console()
+ mark_safe(' <table class="table table-border table-striped table-sm"><thead><tr><th>field</th><th>value</th></tr></thead><tbody>\n') md = f'**DEBUG INFO**: *changes* on: {self}\n\n'
+ mark_safe( md += '| field | value |\n'
''.join( md += '| --- | --- |\n'
format_html(
' <tr><td>{0}</td><td>{1}</td></tr>\n', for k, v in changes.items():
k, if isinstance(v, dict):
format_html('<s>{}</s> {}', *v) if isinstance(v, tuple) else v, md += f'| {k} | '
) if 'old' in v:
for k, v in changes.items() md += f'~~{v["old"]}~~ '
) if 'new' in v:
) md += f'{v["new"]}'
+ mark_safe(' </tbody></table>') md += ' |\n'
) else:
logging.debug(msg) md += f'| {k} | {v} |\n'
console.print(Markdown(md))
def model_form_valid_activitylog( def model_form_valid_activitylog(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment