# 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 ..audit.utils import is_audit_model
# Exports
from superdjango.shortcuts import get_user_name
__all__ = (
"ReviewedByModel",
)
# Constants
AUTH_USER_MODEL = settings.AUTH_USER_MODEL
# Models
[docs]class ReviewedByModel(models.Model):
"""Represents a record (content of some sort) that may be reviewed.
**Fields**
The following fields are provided:
- ``is_reviewed``
- ``reviewed_dt``
- ``reviewed_by``
"""
is_reviewed = models.BooleanField(
_("reviewed"),
default=False,
help_text=_("Indicates the content is reviewed.")
)
reviewed_by = models.ForeignKey(
AUTH_USER_MODEL,
blank=True,
help_text=_("The user reviewing the content."),
null=True,
on_delete=models.SET_NULL,
related_name="%(app_label)s_%(class)s_reviewed_records",
verbose_name=_("reviewed by")
)
reviewed_dt = models.DateTimeField(
_("reviewed date/time"),
blank=True,
help_text=_("Date and time the news item was reviewed."),
null=True
)
class Meta:
abstract = True
[docs] def mark_reviewed(self, user, audit=True, commit=True):
"""Review the content.
:param user: The user reviewing 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 review fields.
if not self.is_reviewed:
self.is_reviewed = True
self.reviewed_by = user
self.reviewed_dt = now()
if audit and is_audit_model(self):
# noinspection PyUnresolvedReferences
self.audit(user, commit=False)
if commit:
self.save()
[docs] def mark_unreviewed(self, user, audit=True, commit=True):
"""Un-review the content.
:param user: The user publishing 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_reviewed = False
self.reviewed_by = None
self.reviewed_dt = None
if audit and is_audit_model(self):
# noinspection PyUnresolvedReferences
self.audit(user, commit=False)
if commit:
self.save()
@property
def reviewed_by_name(self):
"""Get the full name or user name of the user that reviewed the record.
:rtype: str
"""
return get_user_name(self.reviewed_by)