Coverage for sites/comments_site/comments_database/serializers.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-05-19 19:20 +0000

1from rest_framework import serializers 

2 

3from comments_api.constants import COMMENT_STATUS_CHOICES 

4 

5from .models import Comment 

6from .models import ModerationRights 

7 

8 

9class ModerationRightsSerializer(serializers.ModelSerializer): 

10 class Meta: 

11 model = ModerationRights 

12 fields = "__all__" 

13 

14 

15class ModerationRightsReadSerializer(serializers.ModelSerializer): 

16 comment__status = serializers.ChoiceField(COMMENT_STATUS_CHOICES, read_only=True) 

17 

18 class Meta: 

19 model = ModerationRights 

20 fields = ["moderator_id", "comment_id", "comment__status"] 

21 

22 

23class ParentCommentSerializer(serializers.ModelSerializer): 

24 """ 

25 Serializer for a comment's parent data. 

26 

27 We only extract part of the data directly attached to the comment model, no 

28 relations. 

29 This is used to format the comment's author name 

30 """ 

31 

32 author_name = serializers.CharField(source="author_full_name", read_only=True) 

33 

34 class Meta: 

35 model = Comment 

36 fields = [ 

37 "id", 

38 "article_author_comment", 

39 "editorial_team_comment", 

40 "hide_author_name", 

41 "author_id", 

42 "author_email", 

43 "author_name", 

44 "author_provider", 

45 "author_provider_uid", 

46 ] 

47 

48 

49class CommentSerializerCreate(serializers.ModelSerializer): 

50 """ 

51 Same as the base CommentSerializer without `raw_html` field and a non-modified 

52 `parent` field 

53 """ 

54 

55 author_name = serializers.CharField(source="author_full_name", read_only=True) 

56 site_name = serializers.CharField(source="site.username", read_only=True) 

57 base_url = serializers.URLField(source="get_base_url", read_only=True) 

58 moderation_data = ModerationRightsSerializer(source="get_moderation_data", read_only=True) 

59 moderators = serializers.ListField(source="get_moderators", read_only=True) 

60 

61 class Meta: 

62 model = Comment 

63 fields = [ 

64 "id", 

65 "doi", 

66 "sanitized_html", 

67 "raw_html", 

68 "status", 

69 "date_submitted", 

70 "date_last_modified", 

71 "article_author_comment", 

72 "editorial_team_comment", 

73 "hide_author_name", 

74 "author_id", 

75 "author_email", 

76 "author_name", 

77 "author_first_name", 

78 "author_last_name", 

79 "author_provider", 

80 "author_provider_uid", 

81 "is_new", 

82 "validation_email_sent", 

83 "site", 

84 "site_name", 

85 "parent", 

86 "base_url", 

87 "moderation_data", 

88 "moderators", 

89 ] 

90 

91 

92class CommentSerializer(CommentSerializerCreate): 

93 parent = ParentCommentSerializer(read_only=True) 

94 

95 class Meta: 

96 model = Comment 

97 fields = [ 

98 "id", 

99 "doi", 

100 "sanitized_html", 

101 "status", 

102 "date_submitted", 

103 "date_last_modified", 

104 "article_author_comment", 

105 "editorial_team_comment", 

106 "hide_author_name", 

107 "author_id", 

108 "author_email", 

109 "author_name", 

110 "author_provider", 

111 "author_provider_uid", 

112 "is_new", 

113 "validation_email_sent", 

114 "site", 

115 "site_name", 

116 "parent", 

117 "base_url", 

118 "moderation_data", 

119 "moderators", 

120 ]