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 ..audit.utils import is_audit_model 

8 

9# Exports 

10from superdjango.shortcuts import get_user_name 

11 

12__all__ = ( 

13 "ReviewedByModel", 

14) 

15 

16# Constants 

17 

18AUTH_USER_MODEL = settings.AUTH_USER_MODEL 

19 

20# Models 

21 

22 

23class ReviewedByModel(models.Model): 

24 """Represents a record (content of some sort) that may be reviewed. 

25 

26 **Fields** 

27 

28 The following fields are provided: 

29 

30 - ``is_reviewed`` 

31 - ``reviewed_dt`` 

32 - ``reviewed_by`` 

33 

34 """ 

35 

36 is_reviewed = models.BooleanField( 

37 _("reviewed"), 

38 default=False, 

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

40 ) 

41 

42 reviewed_by = models.ForeignKey( 

43 AUTH_USER_MODEL, 

44 blank=True, 

45 help_text=_("The user reviewing the content."), 

46 null=True, 

47 on_delete=models.SET_NULL, 

48 related_name="%(app_label)s_%(class)s_reviewed_records", 

49 verbose_name=_("reviewed by") 

50 ) 

51 

52 reviewed_dt = models.DateTimeField( 

53 _("reviewed date/time"), 

54 blank=True, 

55 help_text=_("Date and time the news item was reviewed."), 

56 null=True 

57 ) 

58 

59 class Meta: 

60 abstract = True 

61 

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

63 """Review the content. 

64 

65 :param user: The user reviewing 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 review fields. 

76 if not self.is_reviewed: 

77 self.is_reviewed = True 

78 self.reviewed_by = user 

79 self.reviewed_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_unreviewed(self, user, audit=True, commit=True): 

89 """Un-review 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_reviewed = False 

102 self.reviewed_by = None 

103 self.reviewed_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 reviewed_by_name(self): 

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

115 

116 :rtype: str 

117 

118 """ 

119 return get_user_name(self.reviewed_by)