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

1import uuid 

2 

3import reversion 

4from actstream.models import Action 

5from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation 

6from django.contrib.contenttypes.models import ContentType 

7from django.db import models 

8from django.utils import timezone 

9from django.utils.translation import gettext as _ 

10from django_bleach.models import BleachField 

11from mptt.models import MPTTModel, TreeForeignKey 

12 

13 

14class Notification(MPTTModel): 

15 """ 

16 Notification with tree-traversal support for threads 

17 """ 

18 

19 PUBLIC = "PUB" 

20 PRIVATE = "PRI" 

21 CURATOR = "CUR" 

22 ACCESS_REQUEST = "AR" 

23 PUB_REQUEST = "PR" 

24 

25 NOTIFICATION_TYPE_CHOICES = [ 

26 (PUBLIC, _("public")), 

27 (PRIVATE, _("private")), 

28 (CURATOR, _("curator")), 

29 (ACCESS_REQUEST, _("access request")), 

30 (PUB_REQUEST, _("publication request")), 

31 ] 

32 

33 uuid = models.UUIDField(default=uuid.uuid4, editable=False) 

34 notification_type = models.CharField( 

35 max_length=4, choices=NOTIFICATION_TYPE_CHOICES, default=PUBLIC, 

36 ) 

37 text = BleachField(max_length=12000) 

38 date_added = models.DateTimeField(auto_now_add=True) 

39 date_edited = models.DateTimeField(auto_now=True) 

40 owner = models.ForeignKey( 

41 "ddusers.User", related_name="notification_owner", on_delete=models.PROTECT 

42 ) 

43 recipient = models.ForeignKey( 

44 "ddusers.User", 

45 related_name="notification_recipient", 

46 on_delete=models.PROTECT, 

47 null=True, 

48 blank=True, 

49 ) 

50 permanent = models.BooleanField(default=False) 

51 

52 parent = TreeForeignKey( 

53 "self", 

54 on_delete=models.CASCADE, 

55 null=True, 

56 blank=True, 

57 related_name="notification_children", 

58 ) 

59 

60 # mandatory fields for generic relation 

61 content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT) 

62 object_id = models.IntegerField() 

63 content_object = GenericForeignKey() 

64 

65 def __str__(self): 

66 return "[%s] %s %s (%s), %s" % ( 

67 self.get_notification_type_display(), 

68 self.content_type, 

69 self.content_object, 

70 self.owner, 

71 self.date_added, 

72 ) 

73 

74 def class_name(self): 

75 return self.__class__.__name__ 

76 

77 

78@reversion.register() 

79class Comment(MPTTModel): 

80 """ 

81 Comment with tree-traversal support for threads 

82 """ 

83 

84 PUBLIC = "PUB" 

85 PRIVATE = "PRI" 

86 CURATOR = "CUR" 

87 PERMANENT = "PERM" 

88 

89 COMMENT_TYPE_CHOICES = [ 

90 (PUBLIC, _("public")), 

91 (PRIVATE, _("private")), 

92 (CURATOR, _("curator")), 

93 (PERMANENT, _("permanent")), 

94 ] 

95 

96 uuid = models.UUIDField(default=uuid.uuid4, editable=False) 

97 doi = models.CharField(max_length=200, blank=True) 

98 comment_type = models.CharField( 

99 max_length=4, choices=COMMENT_TYPE_CHOICES, default=PUBLIC, 

100 ) 

101 text = BleachField(max_length=12000) 

102 date_added = models.DateTimeField(auto_now_add=True) 

103 date_edited = models.DateTimeField(auto_now=True) 

104 owner = models.ForeignKey( 

105 "ddusers.User", related_name="comment_owner", on_delete=models.PROTECT 

106 ) 

107 permanent = models.BooleanField(default=False) 

108 deleted = models.BooleanField(default=False) 

109 

110 parent = TreeForeignKey( 

111 "self", 

112 on_delete=models.CASCADE, 

113 null=True, 

114 blank=True, 

115 related_name="comment_children", 

116 ) 

117 

118 # mandatory fields for generic relation 

119 content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT) 

120 object_id = models.IntegerField() 

121 content_object = GenericForeignKey() 

122 

123 def __str__(self): 

124 return "%s" % ( 

125 # self.get_comment_type_display(), 

126 # self.content_type, 

127 self.content_object, 

128 # self.owner, 

129 # self.date_added.strftime("%d.%m.%Y, %H:%M:%S"), 

130 ) 

131 

132 def is_public(self): 

133 if ( 

134 self.comment_type == Comment.PUBLIC 

135 or self.comment_type == Comment.PERMANENT 

136 ): 

137 return True 

138 

139 def class_name(self): 

140 return self.__class__.__name__ 

141 

142 def set_delete(self): 

143 delete_date = timezone.now().strftime("%d.%m.%Y, %H:%M:%S") 

144 delete_text = _("This comment has been deleted at") 

145 self.text = "{} {}".format(delete_text, delete_date) 

146 self.deleted = True 

147 self.save() 

148 

149 def get_absolute_url(self): 

150 return "{}discuss/#comment-{}".format( 

151 self.content_object.get_absolute_url(), self.uuid 

152 )