Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nouveau champ User.logs #1984

Merged
merged 3 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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