• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

liqd / adhocracy-plus / 18908688697

29 Oct 2025 12:59PM UTC coverage: 44.622% (-44.5%) from 89.135%
18908688697

Pull #2986

github

web-flow
Merge 1dfde8ee7 into 445e1d498
Pull Request #2986: Draft: Speed up Github Ci Tests

3012 of 6750 relevant lines covered (44.62%)

0.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/apps/userdashboard/serializers.py
1
from django.urls import reverse
×
2
from django.utils.translation import gettext as _
×
3
from easy_thumbnails.files import get_thumbnailer
×
4
from rest_framework import serializers
×
5
from rest_framework.serializers import raise_errors_on_nested_writes
×
6
from rest_framework.utils import model_meta
×
7

8
from adhocracy4.comments.models import Comment
×
9
from apps.contrib.dates import get_date_display
×
10
from apps.moderatorfeedback.serializers import ModeratorCommentFeedbackSerializer
×
11

12

13
class ModerationCommentSerializer(serializers.ModelSerializer):
×
14
    comment_url = serializers.SerializerMethodField()
×
15
    is_unread = serializers.SerializerMethodField()
×
16
    is_modified = serializers.SerializerMethodField()
×
17
    last_edit = serializers.SerializerMethodField()
×
18
    moderator_feedback = ModeratorCommentFeedbackSerializer(read_only=True)
×
19
    num_reports = serializers.SerializerMethodField()
×
20
    feedback_api_url = serializers.SerializerMethodField()
×
21
    user_name = serializers.SerializerMethodField()
×
22
    user_image = serializers.SerializerMethodField()
×
23
    user_profile_url = serializers.SerializerMethodField()
×
24

25
    class Meta:
×
26
        model = Comment
×
27
        fields = [
×
28
            "comment",
29
            "comment_url",
30
            "feedback_api_url",
31
            "is_unread",
32
            "is_blocked",
33
            "is_moderator_marked",
34
            "is_modified",
35
            "last_edit",
36
            "moderator_feedback",
37
            "num_reports",
38
            "pk",
39
            "user_image",
40
            "user_name",
41
            "user_profile_url",
42
        ]
43

44
    def get_comment_url(self, instance):
×
45
        return instance.get_absolute_url()
×
46

47
    def get_is_modified(self, comment):
×
48
        return comment.modified is not None
×
49

50
    def get_last_edit(self, comment):
×
51
        if comment.modified:
×
52
            return get_date_display(comment.modified)
×
53
        else:
54
            return get_date_display(comment.created)
×
55

56
    def get_feedback_api_url(self, comment):
×
57
        return reverse("moderatorfeedback-list", kwargs={"comment_pk": comment.pk})
×
58

59
    def get_num_reports(self, comment):
×
60
        return comment.num_reports
×
61

62
    def get_user_name(self, comment):
×
63
        if comment.is_censored or comment.is_removed:
×
64
            return _("unknown user")
×
65
        return str(comment.creator.username)
×
66

67
    def get_user_image_fallback(self, comment):
×
68
        """Load small thumbnail images for default user images."""
69
        if comment.is_censored or comment.is_removed:
×
70
            return None
×
71
        try:
×
72
            if comment.creator.avatar_fallback:
×
73
                return comment.creator.avatar_fallback
×
74
        except AttributeError:
×
75
            pass
×
76
        return None
×
77

78
    def get_user_image(self, comment):
×
79
        """Load small thumbnail images for user images."""
80
        if comment.is_censored or comment.is_removed:
×
81
            return None
×
82
        try:
×
83
            if comment.creator.avatar:
×
84
                avatar = get_thumbnailer(comment.creator.avatar)["avatar"]
×
85
                return avatar.url
×
86
        except AttributeError:
×
87
            pass
×
88
        return self.get_user_image_fallback(comment)
×
89

90
    def get_user_profile_url(self, comment):
×
91
        if comment.is_censored or comment.is_removed:
×
92
            return ""
×
93
        try:
×
94
            return comment.creator.get_absolute_url()
×
95
        except AttributeError:
×
96
            return ""
×
97

98
    def get_is_unread(self, comment):
×
99
        return not comment.is_reviewed
×
100

101
    def update(self, instance, validated_data):
×
102
        """Update comment instance without changing comment.modified.
103

104
        This is essentially copied from
105
        rest_framework.serializers.ModelSerializer.update(),
106
        only difference is ignore_modified=true when saving the instance.
107
        See also here:
108
        https://github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py#L991-L1015
109
        """
110
        raise_errors_on_nested_writes("update", self, validated_data)
×
111
        info = model_meta.get_field_info(instance)
×
112

113
        # Simply set each attribute on the instance, and then save it.
114
        # Note that unlike `.create()` we don't need to treat many-to-many
115
        # relationships as being a special case. During updates we already
116
        # have an instance pk for the relationships to be associated with.
117
        m2m_fields = []
×
118
        for attr, value in validated_data.items():
×
119
            if attr in info.relations and info.relations[attr].to_many:
×
120
                m2m_fields.append((attr, value))
×
121
            else:
122
                setattr(instance, attr, value)
×
123

124
        instance.save(ignore_modified=True)
×
125

126
        # Note that many-to-many fields are set after updating instance.
127
        # Setting m2m fields triggers signals which could potentially change
128
        # updated instance and we do not want it to collide with .update()
129
        for attr, value in m2m_fields:
×
130
            field = getattr(instance, attr)
×
131
            field.set(value)
×
132

133
        return instance
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc