Translate django admin log entry messages

172 Views Asked by At

I've a small django mezzanine application in https://github.com/miettinj/mezzanine It translates admin interface correctly, but it also should translate the page history to understandable form. (I guess it comes directly from django_admin_log in database)

Now the admin log looks like this:

{
  "model": "admin.logentry",
  "pk": 2,
  "fields": {
    "action_time": "2022-11-18T08:34:03.136Z",
    "user": 1,
    "content_type": 18,
    "object_id": "1",
    "object_repr": "janes post",
    "action_flag": 2,
    "change_message": "[{\"changed\": {\"fields\": [\"Content\", \"Keywords\"]}}]"
  }
}

How can I change it so that it translates the action 'changed', remains the field names unchanged and totally removes the unuserfrienly characters: ("[{\) ?

In the attachment the lines surrounded with blue are correct and lines surrounded with read are current functionality.enter image description here

1

There are 1 best solutions below

0
Svestis On

Might be coming late to the game but I had the same question recently.

First of all, you first show a json (which is also a dict in python) which can easily be parsed in python:

{
  "model": "admin.logentry",
  "pk": 2,
  "fields": {
    "action_time": "2022-11-18T08:34:03.136Z",
    "user": 1,
    "content_type": 18,
    "object_id": "1",
    "object_repr": "janes post",
    "action_flag": 2,
    "change_message": "[{\"changed\": {\"fields\": [\"Content\", \"Keywords\"]}}]"
  }
}

This can be parsed in python to keep only the "change_message" field either by considering it as json representation (JSON double parsing) or a dictionary representation:

Dict:

    data = {
  "model": "admin.logentry",
  "pk": 2,
  "fields": {
    "action_time": "2022-11-18T08:34:03.136Z",
    "user": 1,
    "content_type": 18,
    "object_id": "1",
    "object_repr": "janes post",
    "action_flag": 2,
    "change_message": "[{\"changed\": {\"fields\": [\"Content\", \"Keywords\"]}}]"
  }
}

change_message = data['fields']['change_message']

print(change_message)

With the output being: [{"changed": {"fields": ["Content", "Keywords"]}}]


JSON

import json

# Your JSON string
json_str = """
{
  "model": "admin.logentry",
  "pk": 2,
  "fields": {
    "action_time": "2022-11-18T08:34:03.136Z",
    "user": 1,
    "content_type": 18,
    "object_id": "1",
    "object_repr": "janes post",
    "action_flag": 2,
    "change_message": "[{\\"changed\\": {\\"fields\\": [\\"Content\\", \\"Keywords\\"]}}]"
  }
}
"""

# Parse the JSON string into a Python object
data = json.loads(json_str)

# Extract the 'change_message' field
change_message = data['fields']['change_message']

print(change_message)

With the output being [{"changed": {"fields": ["Content", "Keywords"]}}]


Final Parsing

Now, either you take the whole JSON file and then you parse it to take the "change_message" or you take directly the "change_message" from the LogEntry, then you will need to further parse it and define the display that you want. You can do for example:

import json

# Your JSON string
json_str = "[{\"changed\": {\"fields\": [\"Content\", \"Keywords\"]}}]"

# Parse the JSON string into a Python object
parsed = json.loads(json_str)

# Extract 'fields' from 'changed' and join with a comma
fields = ', '.join(parsed[0]['changed']['fields'])

output = f"Changed: {fields}"

print(output)

With the output being: Changed: Content, Keywords