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 "ResolvedByModel", 

14) 

15 

16# Constants 

17 

18AUTH_USER_MODEL = settings.AUTH_USER_MODEL 

19 

20# Models 

21 

22 

23class ResolvedByModel(models.Model): 

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

25 

26 is_resolved = models.BooleanField( 

27 _("resolved"), 

28 default=False, 

29 help_text=_("Indicates the content is resolved.") 

30 ) 

31 

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 ) 

41 

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 ) 

48 

49 class Meta: 

50 abstract = True 

51 

52 def mark_resolved(self, user, audit=True, commit=True): 

53 """Resolve the content. 

54 

55 :param user: The user resolving the content. 

56 :type user: AUTH_USER_MODEL 

57 

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

59 :type audit: bool 

60 

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

62 :type commit: bool 

63 

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

70 

71 if audit and is_audit_model(self): 

72 # noinspection PyUnresolvedReferences 

73 self.audit(user, commit=False) 

74 

75 if commit: 

76 self.save() 

77 

78 def mark_unresolved(self, user, audit=True, commit=True): 

79 """Un-publish the content. 

80 

81 :param user: The user resolving the content. 

82 :type user: AUTH_USER_MODEL 

83 

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

85 :type audit: bool 

86 

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

88 :type commit: bool 

89 

90 """ 

91 self.is_resolved = False 

92 self.resolved_by = None 

93 self.resolved_dt = None 

94 

95 if audit and is_audit_model(self): 

96 # noinspection PyUnresolvedReferences 

97 self.audit(user, commit=False) 

98 

99 if commit: 

100 self.save() 

101 

102 @property 

103 def resolved_by_name(self): 

104 """Get the full name or user name of the user that resolved the record. 

105 

106 :rtype: str 

107 

108 """ 

109 return get_user_name(self.resolved_by)