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 "PublishedByModel",
14)
16# Constants
18AUTH_USER_MODEL = settings.AUTH_USER_MODEL
20# Models
23class PublishedByModel(models.Model):
24 """Represents a record (content of some sort) that may be published or unpublished.
26 **Fields**
28 The following fields are provided:
30 - ``is_published``
31 - ``published_dt``
32 - ``published_by``
34 """
36 is_published = models.BooleanField(
37 _("published"),
38 default=False,
39 help_text=_("Indicates the content is published.")
40 )
42 published_by = models.ForeignKey(
43 AUTH_USER_MODEL,
44 blank=True,
45 help_text=_("The user publishing the content."),
46 null=True,
47 on_delete=models.SET_NULL,
48 related_name="%(app_label)s_%(class)s_published_records",
49 verbose_name=_("published by")
50 )
52 published_dt = models.DateTimeField(
53 _("published date/time"),
54 blank=True,
55 help_text=_("Date and time the news item was published."),
56 null=True
57 )
59 class Meta:
60 abstract = True
62 def mark_published(self, user, audit=True, commit=True):
63 """Publish the content.
65 :param user: The user publishing the content.
66 :type user: AUTH_USER_MODEL
68 :param audit: Call the ``audit()`` method if this is also an audit model.
69 :type audit: bool
71 :param commit: Indicates the record should be saved.
72 :type commit: bool
74 """
75 # Prevent update of published info.
76 if not self.is_published:
77 self.is_published = True
78 self.published_by = user
79 self.published_dt = now()
81 if audit and is_audit_model(self):
82 # noinspection PyUnresolvedReferences
83 self.audit(user, commit=False)
85 if commit:
86 self.save()
88 def mark_unpublished(self, user, audit=True, commit=True):
89 """Un-publish the content.
91 :param user: The user publishing the content.
92 :type user: AUTH_USER_MODEL
94 :param audit: Call the ``audit()`` method if this is also an audit model.
95 :type audit: bool
97 :param commit: Indicates the record should be saved.
98 :type commit: bool
100 """
101 self.is_published = False
102 self.published_by = None
103 self.published_dt = None
105 if audit and is_audit_model(self):
106 # noinspection PyUnresolvedReferences
107 self.audit(user, commit=False)
109 if commit:
110 self.save()
112 @property
113 def published_by_name(self):
114 """Get the full name or user name of the user that published the record.
116 :rtype: str
118 """
119 return get_user_name(self.published_by)