Hide keyboard shortcuts

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 

2 

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 

9 

10# Exports 

11 

12__all__ = ( 

13 "PublishedByModel", 

14) 

15 

16# Constants 

17 

18AUTH_USER_MODEL = settings.AUTH_USER_MODEL 

19 

20# Models 

21 

22 

23class PublishedByModel(models.Model): 

24 """Represents a record (content of some sort) that may be published or unpublished. 

25 

26 **Fields** 

27 

28 The following fields are provided: 

29 

30 - ``is_published`` 

31 - ``published_dt`` 

32 - ``published_by`` 

33 

34 """ 

35 

36 is_published = models.BooleanField( 

37 _("published"), 

38 default=False, 

39 help_text=_("Indicates the content is published.") 

40 ) 

41 

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 ) 

51 

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 ) 

58 

59 class Meta: 

60 abstract = True 

61 

62 def mark_published(self, user, audit=True, commit=True): 

63 """Publish the content. 

64 

65 :param user: The user publishing the content. 

66 :type user: AUTH_USER_MODEL 

67 

68 :param audit: Call the ``audit()`` method if this is also an audit model. 

69 :type audit: bool 

70 

71 :param commit: Indicates the record should be saved. 

72 :type commit: bool 

73 

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() 

80 

81 if audit and is_audit_model(self): 

82 # noinspection PyUnresolvedReferences 

83 self.audit(user, commit=False) 

84 

85 if commit: 

86 self.save() 

87 

88 def mark_unpublished(self, user, audit=True, commit=True): 

89 """Un-publish the content. 

90 

91 :param user: The user publishing the content. 

92 :type user: AUTH_USER_MODEL 

93 

94 :param audit: Call the ``audit()`` method if this is also an audit model. 

95 :type audit: bool 

96 

97 :param commit: Indicates the record should be saved. 

98 :type commit: bool 

99 

100 """ 

101 self.is_published = False 

102 self.published_by = None 

103 self.published_dt = None 

104 

105 if audit and is_audit_model(self): 

106 # noinspection PyUnresolvedReferences 

107 self.audit(user, commit=False) 

108 

109 if commit: 

110 self.save() 

111 

112 @property 

113 def published_by_name(self): 

114 """Get the full name or user name of the user that published the record. 

115 

116 :rtype: str 

117 

118 """ 

119 return get_user_name(self.published_by)