Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django.conf import settings
4from django.db import models
5from django.utils.timezone import now
6from django.utils.translation import gettext_lazy as _
7from superdjango.shortcuts import get_user_name
8from ..audit.utils import is_audit_model
10# Exports
12__all__ = (
13 "ResolvedByModel",
14)
16# Constants
18AUTH_USER_MODEL = settings.AUTH_USER_MODEL
20# Models
23class ResolvedByModel(models.Model):
24 """Represents a record (content of some sort) that may be resolved or unresolved."""
26 is_resolved = models.BooleanField(
27 _("resolved"),
28 default=False,
29 help_text=_("Indicates the content is resolved.")
30 )
32 resolved_by = models.ForeignKey(
33 AUTH_USER_MODEL,
34 blank=True,
35 help_text=_("The user resolving the content."),
36 null=True,
37 on_delete=models.SET_NULL,
38 related_name="%(app_label)s_%(class)s_resolved_records",
39 verbose_name=_("resolved by")
40 )
42 resolved_dt = models.DateTimeField(
43 _("resolved date/time"),
44 blank=True,
45 help_text=_("Date and time the news item was resolved."),
46 null=True
47 )
49 class Meta:
50 abstract = True
52 def mark_resolved(self, user, audit=True, commit=True):
53 """Resolve the content.
55 :param user: The user resolving the content.
56 :type user: AUTH_USER_MODEL
58 :param audit: Call the ``audit()`` method if this is also an audit model.
59 :type audit: bool
61 :param commit: Indicates the record should be saved.
62 :type commit: bool
64 """
65 # Prevent update of resolved fields.
66 if not self.is_resolved:
67 self.is_resolved = True
68 self.resolved_by = user
69 self.resolved_dt = now()
71 if audit and is_audit_model(self):
72 # noinspection PyUnresolvedReferences
73 self.audit(user, commit=False)
75 if commit:
76 self.save()
78 def mark_unresolved(self, user, audit=True, commit=True):
79 """Un-publish the content.
81 :param user: The user resolving the content.
82 :type user: AUTH_USER_MODEL
84 :param audit: Call the ``audit()`` method if this is also an audit model.
85 :type audit: bool
87 :param commit: Indicates the record should be saved.
88 :type commit: bool
90 """
91 self.is_resolved = False
92 self.resolved_by = None
93 self.resolved_dt = None
95 if audit and is_audit_model(self):
96 # noinspection PyUnresolvedReferences
97 self.audit(user, commit=False)
99 if commit:
100 self.save()
102 @property
103 def resolved_by_name(self):
104 """Get the full name or user name of the user that resolved the record.
106 :rtype: str
108 """
109 return get_user_name(self.resolved_by)