You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
konova/user/models/user_action.py

136 lines
3.8 KiB
Python

"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 15.11.21
"""
import uuid
from django.db import models
from django.utils.timezone import localtime
from django.utils.translation import gettext_lazy as _
from konova.sub_settings.django_settings import DEFAULT_DATE_TIME_FORMAT
class UserAction(models.TextChoices):
"""
Defines different possible user actions for UserActionLogEntry
"""
CHECKED = "checked", _("Checked")
RECORDED = "recorded", _("Recorded")
UNRECORDED = "unrecorded", _("Unrecorded")
CREATED = "created", _("Created")
EDITED = "edited", _("Edited")
DELETED = "deleted", _("Deleted")
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,
)
user = models.ForeignKey("user.User", related_name='+', on_delete=models.CASCADE, help_text="Performing user")
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",
choices=UserAction.choices,
)
comment = models.CharField(max_length=255, null=True, blank=True, help_text="Additional comment on this entry")
class Meta:
ordering = (
"-timestamp",
)
def __str__(self):
return f"{self.timestamp.strftime(DEFAULT_DATE_TIME_FORMAT)} | {self.action} | {self.user}"
@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
@classmethod
def get_created_action(cls, user, comment: str = None):
action = UserActionLogEntry.objects.create(
user=user,
action=UserAction.CREATED,
comment=comment,
)
return action
@classmethod
def get_edited_action(cls, user, comment: str = None):
action = UserActionLogEntry.objects.create(
user=user,
action=UserAction.EDITED,
comment=comment,
)
return action
@classmethod
def get_deleted_action(cls, user, comment: str = None):
action = UserActionLogEntry.objects.create(
user=user,
action=UserAction.DELETED,
comment=comment,
)
return action
@classmethod
def get_checked_action(cls, user, comment: str = None):
action = UserActionLogEntry.objects.create(
user=user,
action=UserAction.CHECKED,
comment=comment,
)
return action
@classmethod
def get_recorded_action(cls, user, comment: str = None):
action = UserActionLogEntry.objects.create(
user=user,
action=UserAction.RECORDED,
comment=comment,
)
return action
@classmethod
def get_unrecorded_action(cls, user, comment: str = None):
action = UserActionLogEntry.objects.create(
user=user,
action=UserAction.UNRECORDED,
comment=comment,
)
return action
def get_timestamp_str_formatted(self):
""" Getter for formatted timestamp string of the entry
Returns:
val (str): The formatted timestamp as string
"""
val = self.timestamp
val = localtime(val)
return val.strftime(DEFAULT_DATE_TIME_FORMAT)