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

feat: audit log #902

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add core model history views
  • Loading branch information
cdubz committed Nov 8, 2024
commit b1b7fa215d63351a637379fb73308eb5162b0438
6 changes: 3 additions & 3 deletions babybuddy/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,13 @@ def strtobool(val):

IMPORT_EXPORT_USE_TRANSACTIONS = True

# Audit log condfiguration
# Audit log configuration
# See https://django-auditlog.readthedocs.io/en/latest/index.html

AUDITLOG_INCLUDE_ALL_MODELS = True

AUDITLOG_EXCLUDE_TRACKING_FIELDS = ("id", "last_used")

AUDITLOG_INCLUDE_ALL_MODELS = True

# Axes configuration
# See https://django-axes.readthedocs.io/en/latest/4_configuration.html

Expand Down
7 changes: 7 additions & 0 deletions babybuddy/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@

EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend"

# Audit log configuration
# See https://django-auditlog.readthedocs.io/en/latest/index.html

AUDITLOG_EXCLUDE_TRACKING_FIELDS = ()

AUDITLOG_INCLUDE_ALL_MODELS = False

# Axes configuration
# See https://django-axes.readthedocs.io/en/latest/4_configuration.html

Expand Down
99 changes: 92 additions & 7 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.core.validators import RegexValidator
from django.db import models
from django.db.models.functions import Lower
from django.urls import reverse_lazy
from django.utils import timezone
from django.utils.text import format_lazy, slugify
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -105,6 +106,12 @@ class Meta:
verbose_name = _("Tag")
verbose_name_plural = _("Tags")

def get_absolute_url(self):
return reverse_lazy("core:tag", kwargs={"slug": self.slug})

def get_list_url(self):
return reverse_lazy("core:tag-list")

@property
def complementary_color(self):
if not self.color:
Expand Down Expand Up @@ -167,6 +174,12 @@ def __str__(self):
def clean(self):
validate_date(self.date, "date")

def get_absolute_url(self):
return reverse_lazy("core:bmi-update", kwargs={"pk": self.pk})

def get_list_url(self):
return reverse_lazy("core:bmi-list")


class Child(models.Model):
model_name = "child"
Expand Down Expand Up @@ -211,6 +224,12 @@ def delete(self, using=None, keep_parents=False):
super(Child, self).delete(using, keep_parents)
cache.set(self.cache_key_count, Child.objects.count(), None)

def get_absolute_url(self):
return reverse_lazy("core:child", kwargs={"slug": self.slug})

def get_list_url(self):
return reverse_lazy("core:child-list")

def name(self, reverse=False):
if not self.last_name:
return self.first_name
Expand Down Expand Up @@ -284,6 +303,12 @@ def attributes(self):
def clean(self):
validate_time(self.time, "time")

def get_absolute_url(self):
return reverse_lazy("core:diaperchange-update", kwargs={"pk": self.pk})

def get_list_url(self):
return reverse_lazy("core:diaperchange-list")


class Feeding(models.Model):
model_name = "feeding"
Expand Down Expand Up @@ -353,6 +378,12 @@ def clean(self):
validate_duration(self)
validate_unique_period(Feeding.objects.filter(child=self.child), self)

def get_absolute_url(self):
return reverse_lazy("core:feeding-update", kwargs={"pk": self.pk})

def get_list_url(self):
return reverse_lazy("core:feeding-list")


class HeadCircumference(models.Model):
model_name = "head_circumference"
Expand Down Expand Up @@ -386,6 +417,12 @@ def __str__(self):
def clean(self):
validate_date(self.date, "date")

def get_absolute_url(self):
return reverse_lazy("core:head-circumference-update", kwargs={"pk": self.pk})

def get_list_url(self):
return reverse_lazy("core:head-circumference-list")


class Height(models.Model):
model_name = "height"
Expand Down Expand Up @@ -417,6 +454,12 @@ def __str__(self):
def clean(self):
validate_date(self.date, "date")

def get_absolute_url(self):
return reverse_lazy("core:height-update", kwargs={"pk": self.pk})

def get_list_url(self):
return reverse_lazy("core:height-list")


class HeightPercentile(models.Model):
model_name = "height percentile"
Expand Down Expand Up @@ -469,6 +512,12 @@ class Meta:
def __str__(self):
return str(_("Note"))

def get_absolute_url(self):
return reverse_lazy("core:note-update", kwargs={"pk": self.pk})

def get_list_url(self):
return reverse_lazy("core:note-list")


class Pumping(models.Model):
model_name = "pumping"
Expand Down Expand Up @@ -521,6 +570,12 @@ def clean(self):
validate_duration(self)
validate_unique_period(Pumping.objects.filter(child=self.child), self)

def get_absolute_url(self):
return reverse_lazy("core:pumping-update", kwargs={"pk": self.pk})

def get_list_url(self):
return reverse_lazy("core:pumping-list")


class Sleep(models.Model):
model_name = "sleep"
Expand Down Expand Up @@ -573,6 +628,12 @@ def clean(self):
validate_duration(self)
validate_unique_period(Sleep.objects.filter(child=self.child), self)

def get_absolute_url(self):
return reverse_lazy("core:sleep-update", kwargs={"pk": self.pk})

def get_list_url(self):
return reverse_lazy("core:sleep-list")


class Temperature(models.Model):
model_name = "temperature"
Expand Down Expand Up @@ -606,6 +667,12 @@ def __str__(self):
def clean(self):
validate_time(self.time, "time")

def get_absolute_url(self):
return reverse_lazy("core:temperature-update", kwargs={"pk": self.pk})

def get_list_url(self):
return reverse_lazy("core:temperature-list")


class Timer(models.Model):
model_name = "timer"
Expand Down Expand Up @@ -643,6 +710,19 @@ class Meta:
def __str__(self):
return self.name or str(format_lazy(_("Timer #{id}"), id=self.id))

def save(self, *args, **kwargs):
self.name = self.name or None
super(Timer, self).save(*args, **kwargs)

def clean(self):
validate_time(self.start, "start")

def get_absolute_url(self):
return reverse_lazy("core:timer-detail", kwargs={"pk": self.pk})

def get_list_url(self):
return reverse_lazy("core:timer-list")

@property
def title_with_child(self):
"""Get Timer title with child name in parenthesis."""
Expand Down Expand Up @@ -671,13 +751,6 @@ def stop(self):
"""Stop (delete) the timer."""
self.delete()

def save(self, *args, **kwargs):
self.name = self.name or None
super(Timer, self).save(*args, **kwargs)

def clean(self):
validate_time(self.start, "start")


class TummyTime(models.Model):
model_name = "tummytime"
Expand Down Expand Up @@ -727,6 +800,12 @@ def clean(self):
validate_duration(self)
validate_unique_period(TummyTime.objects.filter(child=self.child), self)

def get_absolute_url(self):
return reverse_lazy("core:tummytime-update", kwargs={"pk": self.pk})

def get_list_url(self):
return reverse_lazy("core:tummytime-list")


class Weight(models.Model):
model_name = "weight"
Expand Down Expand Up @@ -758,6 +837,12 @@ def __str__(self):
def clean(self):
validate_date(self.date, "date")

def get_absolute_url(self):
return reverse_lazy("core:weight-update", kwargs={"pk": self.pk})

def get_list_url(self):
return reverse_lazy("core:weight-list")


class WeightPercentile(models.Model):
model_name = "weight percentile"
Expand Down
59 changes: 59 additions & 0 deletions core/templates/core/object_history.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{% extends 'babybuddy/page.html' %}
{% load breadcrumb datetime i18n %}
{% block title %}
{{ object }} {% trans "History" %}
{% endblock %}
{% block breadcrumbs %}
<li class="breadcrumb-item">
<a href="{{ object.get_absolute_url }}">{{ object|verbose_name_plural }}</a>
</li>
<li class="breadcrumb-item">
<a href="{{ object.get_absolute_url }}">{{ object }}</a>
</li>
<li class="breadcrumb-item active" aria-current="page">{% trans "History" %}</li>
{% endblock %}
{% block content %}
<h1>{{ object }} {% trans "History" %}</h1>
{% for entry in object.history.all %}
<h2 class="fs-4">
{% if entry.action == 0 %}
{% trans "Entry Created" %}
{% elif entry.action == 1 %}
{% trans "Entry Updated" %}
{% elif entry.action == 1 %}
{% trans "Entry Deleted" %}
{% endif %}
<div class="fs-5 text-body-secondary">
{{ entry.timestamp }}
{% if entry.actor is not None %}
{% blocktrans trimmed with actor=entry.actor %}
by {{ actor }}
{% endblocktrans %}
{% endif %}
</div>
</h2>
<div class="table-responsive">
<table class="table table-sm table-instances table-borderless table-striped table-hover align-middle">
<thead>
<tr>
<th>{% trans "Field" %}</th>
<th>{% trans "From" %}</th>
<th>{% trans "To" %}</th>
</tr>
</thead>
<tbody>
{% for key, value in entry.changes_display_dict.items %}
<tr>
<td>{{ key }}</td>
<td>{{ value.0 }}</td>
<td>{{ value.1 }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<hr />
{% empty %}
<p>{% trans "No history has been logged yet." %}</p>
{% endfor %}
{% endblock %}
2 changes: 1 addition & 1 deletion core/templates/core/tag_confirm_delete.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<a href="{% url 'core:tag-list' %}">{% trans "Tags" %}</a>
</li>
<li class="breadcrumb-item">
<a href="{% url 'core:tag-detail' tag.slug %}">{{ object }}</a>
<a href="{% url 'core:tag' tag.slug %}">{{ object }}</a>
</li>
<li class="breadcrumb-item active" aria-current="page">{% trans "Delete" %}</li>
{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion core/templates/core/tag_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</li>
{% if object %}
<li class="breadcrumb-item" aria-current="page">
<a href="{% url 'core:tag-detail' object.slug %}">{{ object }}</a>
<a href="{% url 'core:tag' object.slug %}">{{ object }}</a>
</li>
<li class="breadcrumb-item active" aria-current="page">{% trans "Update" %}</li>
{% else %}
Expand Down
2 changes: 1 addition & 1 deletion core/templates/core/tag_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ <h1>
</div>
</td>
<td>
<a href="{% url 'core:tag-detail' tag.slug %}">{{ tag.name }}</a>
<a href="{% url 'core:tag' tag.slug %}">{{ tag.name }}</a>
</td>
<td class="text-center">
<span class="badge badge-pill me-1"
Expand Down
5 changes: 5 additions & 0 deletions core/templatetags/breadcrumb.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ def child_quick_switch(current_child, target_url):
"current_child": current_child,
"target_url": target_url,
}


@register.filter
def verbose_name_plural(obj):
return obj._meta.verbose_name_plural
Loading