2021-11-15 17:41:52 +01:00
|
|
|
"""
|
|
|
|
Author: Michel Peltriaux
|
|
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
|
|
Created on: 15.11.21
|
|
|
|
|
|
|
|
"""
|
2021-07-29 15:49:19 +02:00
|
|
|
import uuid
|
|
|
|
|
2021-07-08 11:07:33 +02:00
|
|
|
from django.db import models
|
2021-11-15 17:41:52 +01:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-07-29 15:49:19 +02:00
|
|
|
|
2022-02-01 18:41:02 +01:00
|
|
|
from konova.sub_settings.django_settings import DEFAULT_DATE_FORMAT, DEFAULT_DATE_TIME_FORMAT
|
|
|
|
|
2021-07-29 15:49:19 +02:00
|
|
|
|
2021-08-03 17:22:41 +02:00
|
|
|
class UserAction(models.TextChoices):
|
|
|
|
"""
|
|
|
|
Defines different possible user actions for UserActionLogEntry
|
|
|
|
"""
|
|
|
|
CHECKED = "checked", _("Checked")
|
|
|
|
RECORDED = "recorded", _("Recorded")
|
2021-08-10 17:19:42 +02:00
|
|
|
UNRECORDED = "unrecorded", _("Unrecorded")
|
2021-08-03 17:22:41 +02:00
|
|
|
CREATED = "created", _("Created")
|
2021-08-05 12:54:28 +02:00
|
|
|
EDITED = "edited", _("Edited")
|
2021-08-03 17:22:41 +02:00
|
|
|
DELETED = "deleted", _("Deleted")
|
|
|
|
|
|
|
|
|
2021-07-29 15:49:19 +02:00
|
|
|
class UserActionLogEntry(models.Model):
|
|
|
|
""" Wraps a user action log entry
|
|
|
|
|
|
|
|
Can be used for workflow related attributes like checking or recording.
|
|
|
|
|
|
|
|
"""
|
|
|
|
id = models.UUIDField(
|
|
|
|
primary_key=True,
|
|
|
|
default=uuid.uuid4,
|
|
|
|
)
|
2022-01-12 12:56:22 +01:00
|
|
|
user = models.ForeignKey("user.User", related_name='+', on_delete=models.CASCADE, help_text="Performing user")
|
2021-07-29 15:49:19 +02:00
|
|
|
timestamp = models.DateTimeField(auto_now_add=True, help_text="Timestamp of performed action")
|
|
|
|
action = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
help_text="Short name for performed action - optional",
|
2021-08-03 17:22:41 +02:00
|
|
|
choices=UserAction.choices,
|
2021-07-29 15:49:19 +02:00
|
|
|
)
|
2021-08-05 12:54:28 +02:00
|
|
|
comment = models.CharField(max_length=255, null=True, blank=True, help_text="Additional comment on this entry")
|
2021-07-29 15:49:19 +02:00
|
|
|
|
2021-08-19 13:02:31 +02:00
|
|
|
class Meta:
|
|
|
|
ordering = (
|
|
|
|
"-timestamp",
|
|
|
|
)
|
|
|
|
|
2022-02-01 18:41:02 +01:00
|
|
|
def __str__(self):
|
|
|
|
return f"{self.timestamp.strftime(DEFAULT_DATE_TIME_FORMAT)} | {self.action} | {self.user}"
|
|
|
|
|
2021-08-05 12:54:28 +02:00
|
|
|
@property
|
|
|
|
def action_humanize(self):
|
|
|
|
""" Returns humanized version of enum
|
|
|
|
|
|
|
|
Used for template rendering
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
choices = UserAction.choices
|
|
|
|
for choice in choices:
|
|
|
|
if choice[0] == self.action:
|
|
|
|
return choice[1]
|
|
|
|
return None
|
2021-11-16 13:15:15 +01:00
|
|
|
|
|
|
|
@classmethod
|
2022-01-12 12:56:22 +01:00
|
|
|
def get_created_action(cls, user, comment: str = None):
|
2021-11-16 13:15:15 +01:00
|
|
|
action = UserActionLogEntry.objects.create(
|
|
|
|
user=user,
|
|
|
|
action=UserAction.CREATED,
|
|
|
|
comment=comment,
|
|
|
|
)
|
|
|
|
return action
|
|
|
|
|
|
|
|
@classmethod
|
2022-01-12 12:56:22 +01:00
|
|
|
def get_edited_action(cls, user, comment: str = None):
|
2021-11-16 13:15:15 +01:00
|
|
|
action = UserActionLogEntry.objects.create(
|
|
|
|
user=user,
|
|
|
|
action=UserAction.EDITED,
|
|
|
|
comment=comment,
|
|
|
|
)
|
|
|
|
return action
|
|
|
|
|
|
|
|
@classmethod
|
2022-01-12 12:56:22 +01:00
|
|
|
def get_deleted_action(cls, user, comment: str = None):
|
2021-11-16 13:15:15 +01:00
|
|
|
action = UserActionLogEntry.objects.create(
|
|
|
|
user=user,
|
|
|
|
action=UserAction.DELETED,
|
|
|
|
comment=comment,
|
|
|
|
)
|
|
|
|
return action
|
|
|
|
|
|
|
|
@classmethod
|
2022-01-12 12:56:22 +01:00
|
|
|
def get_checked_action(cls, user, comment: str = None):
|
2021-11-16 13:15:15 +01:00
|
|
|
action = UserActionLogEntry.objects.create(
|
|
|
|
user=user,
|
|
|
|
action=UserAction.CHECKED,
|
|
|
|
comment=comment,
|
|
|
|
)
|
|
|
|
return action
|
|
|
|
|
|
|
|
@classmethod
|
2022-01-12 12:56:22 +01:00
|
|
|
def get_recorded_action(cls, user, comment: str = None):
|
2021-11-16 13:15:15 +01:00
|
|
|
action = UserActionLogEntry.objects.create(
|
|
|
|
user=user,
|
|
|
|
action=UserAction.RECORDED,
|
|
|
|
comment=comment,
|
|
|
|
)
|
|
|
|
return action
|
|
|
|
|
|
|
|
@classmethod
|
2022-01-12 12:56:22 +01:00
|
|
|
def get_unrecorded_action(cls, user, comment: str = None):
|
2021-11-16 13:15:15 +01:00
|
|
|
action = UserActionLogEntry.objects.create(
|
|
|
|
user=user,
|
|
|
|
action=UserAction.UNRECORDED,
|
|
|
|
comment=comment,
|
|
|
|
)
|
|
|
|
return action
|