Skip to content

Commit

Permalink
Nouveau champ User.logs (#1984)
Browse files Browse the repository at this point in the history
* User: new logs field

* Add to admin
  • Loading branch information
raphodn authored Jun 25, 2023
1 parent fbcaf60 commit a4036ed
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
16 changes: 16 additions & 0 deletions core/utils/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.apps import apps
from django.core import serializers
from django.utils import timezone
from django.utils.html import format_html


DATE_FORMAT = "%Y-%m-%d"
Expand Down Expand Up @@ -176,3 +177,18 @@ def get_choice_key(choices, value):
if choices[key] == value:
return key
return None


def pretty_print_readonly_jsonfield(jsonfield_data):
"""
Display a pretty readonly version of a JSONField
https://stackoverflow.com/a/60219265
"""
result = ""

if jsonfield_data:
result = json.dumps(jsonfield_data, indent=4, ensure_ascii=False)
# result = mark_safe(f"<pre>{escape(result)}</pre>")
result = format_html("<pre>{}</pre>", result)

return result
10 changes: 10 additions & 0 deletions users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.utils.translation import gettext_lazy as _

from core.admin import admin_site
from core.utils.utilities import pretty_print_readonly_jsonfield
from users import constants
from users.models import User, UserCard

Expand Down Expand Up @@ -71,6 +72,7 @@ class UserAdmin(UserAdmin):
"question_count",
"quiz_count",
"has_user_card",
"logs_display",
"created",
"updated",
]
Expand All @@ -97,6 +99,7 @@ class UserAdmin(UserAdmin):
)
},
),
("Autres", {"fields": ("logs_display",)}),
("Dates", {"fields": ("last_login", "created", "updated")}),
)
add_fieldsets = (
Expand Down Expand Up @@ -148,6 +151,13 @@ def has_user_card(self, user):
has_user_card.short_description = "User card"
has_user_card.boolean = True

def logs_display(self, tender=None):
if tender:
return pretty_print_readonly_jsonfield(tender.logs)
return "-"

logs_display.short_description = User._meta.get_field("logs").verbose_name


class UserCardAdmin(admin.ModelAdmin):
list_display = [
Expand Down
18 changes: 18 additions & 0 deletions users/migrations/0009_user_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2 on 2023-06-23 17:11

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("users", "0008_historicalusercard"),
]

operations = [
migrations.AddField(
model_name="user",
name="logs",
field=models.JSONField(default=list, editable=False, verbose_name="Logs historiques"),
),
]
2 changes: 2 additions & 0 deletions users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ class User(AbstractUser):
default=list,
)

logs = models.JSONField(verbose_name="Logs historiques", editable=False, default=list)

# is_active, is_staff, is_superuser
# date_joined, last_login

Expand Down

0 comments on commit a4036ed

Please sign in to comment.