# Imports
from django.conf import settings
from django.db import models
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _
from superdjango.shortcuts import get_user_name
from ..audit.utils import is_audit_model
# Exports
__all__ = (
"ResolvedByModel",
)
# Constants
AUTH_USER_MODEL = settings.AUTH_USER_MODEL
# Models
[docs]class ResolvedByModel(models.Model):
"""Represents a record (content of some sort) that may be resolved or unresolved."""
is_resolved = models.BooleanField(
_("resolved"),
default=False,
help_text=_("Indicates the content is resolved.")
)
resolved_by = models.ForeignKey(
AUTH_USER_MODEL,
blank=True,
help_text=_("The user resolving the content."),
null=True,
on_delete=models.SET_NULL,
related_name="%(app_label)s_%(class)s_resolved_records",
verbose_name=_("resolved by")
)
resolved_dt = models.DateTimeField(
_("resolved date/time"),
blank=True,
help_text=_("Date and time the news item was resolved."),
null=True
)
class Meta:
abstract = True
[docs] def mark_resolved(self, user, audit=True, commit=True):
"""Resolve the content.
:param user: The user resolving the content.
:type user: AUTH_USER_MODEL
:param audit: Call the ``audit()`` method if this is also an audit model.
:type audit: bool
:param commit: Indicates the record should be saved.
:type commit: bool
"""
# Prevent update of resolved fields.
if not self.is_resolved:
self.is_resolved = True
self.resolved_by = user
self.resolved_dt = now()
if audit and is_audit_model(self):
# noinspection PyUnresolvedReferences
self.audit(user, commit=False)
if commit:
self.save()
[docs] def mark_unresolved(self, user, audit=True, commit=True):
"""Un-publish the content.
:param user: The user resolving the content.
:type user: AUTH_USER_MODEL
:param audit: Call the ``audit()`` method if this is also an audit model.
:type audit: bool
:param commit: Indicates the record should be saved.
:type commit: bool
"""
self.is_resolved = False
self.resolved_by = None
self.resolved_dt = None
if audit and is_audit_model(self):
# noinspection PyUnresolvedReferences
self.audit(user, commit=False)
if commit:
self.save()
@property
def resolved_by_name(self):
"""Get the full name or user name of the user that resolved the record.
:rtype: str
"""
return get_user_name(self.resolved_by)