• 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/documents/serializers.py
1
from rest_framework import serializers
×
2

3
from adhocracy4.dashboard import components
×
4
from adhocracy4.dashboard import signals as a4dashboard_signals
×
5
from adhocracy4.modules.models import Module
×
6

7
from .models import Chapter
×
8
from .models import Paragraph
×
9

10

11
class ParagraphSerializer(serializers.ModelSerializer):
×
12
    id = serializers.IntegerField(required=False)
×
13

14
    name = serializers.CharField(
×
15
        required=False,
16
        allow_blank=True,
17
        max_length=Paragraph._meta.get_field("name").max_length,
18
    )
19

20
    class Meta:
×
21
        model = Paragraph
×
22
        fields = ("id", "weight", "name", "text")
×
23
        extra_kwargs = {"weight": {"required": False, "default": 0, "write_only": True}}
×
24

25

26
class ChapterSerializer(serializers.ModelSerializer):
×
27
    id = serializers.IntegerField(required=False)
×
28
    paragraphs = ParagraphSerializer(many=True)
×
29

30
    class Meta:
×
31
        model = Chapter
×
32
        fields = ("id", "name", "weight", "paragraphs")
×
33
        extra_kwargs = {"weight": {"required": False, "default": 0, "write_only": True}}
×
34

35
    def create(self, validated_data):
×
36
        paragraphs = validated_data.pop("paragraphs")
×
37
        user = self.context["request"].user
×
38
        module = Module.objects.get(pk=self.context["module_pk"])
×
39
        chapter = Chapter.objects.create(creator=user, module=module, **validated_data)
×
40

41
        for weight, paragraph_data in enumerate(paragraphs):
×
42
            paragraph_data["weight"] = weight
×
43
            Paragraph.objects.create(chapter=chapter, **paragraph_data)
×
44

45
        return chapter
×
46

47
    def update(self, instance, validated_data):
×
48
        # Update the chapter
49
        paragraphs_data = validated_data.pop("paragraphs")
×
50
        chapter = super(ChapterSerializer, self).update(instance, validated_data)
×
51

52
        # Delete removed paragraphs from the database
53
        paragraph_ids = [item["id"] for item in paragraphs_data if "id" in item]
×
54
        chapter.paragraphs.exclude(id__in=paragraph_ids).delete()
×
55

56
        # Update or create the chapters
57
        for weight, paragraph_data in enumerate(paragraphs_data):
×
58
            paragraph_data["weight"] = weight
×
59
            if "id" in paragraph_data:
×
60
                paragraph = Paragraph.objects.get(id=paragraph_data["id"])
×
61
                assert paragraph.chapter == chapter
×
62
                Paragraph.objects.filter(id=paragraph.id).update(**paragraph_data)
×
63

64
            else:
65
                Paragraph.objects.create(chapter=chapter, **paragraph_data)
×
66

67
        return chapter
×
68

69

70
class DocumentSerializer(serializers.Serializer):
×
71
    chapters = ChapterSerializer(many=True)
×
72

73
    def create(self, validated_data):
×
74
        chapters_data = validated_data["chapters"]
×
75
        chapters = list(self._create_or_update(chapters_data))
×
76

77
        # Send the component updated signal
78
        # (the serializer is only used from within the dashboard)
79
        self._send_component_updated_signal()
×
80

81
        return {"chapters": chapters}
×
82

83
    def _create_or_update(self, validated_data):
×
84
        chapter_list_serializer = self.fields["chapters"]
×
85
        chapter_serializer = chapter_list_serializer.child
×
86

87
        chapter_ids = [chapter["id"] for chapter in validated_data if "id" in chapter]
×
88
        Chapter.objects.filter(module_id=self.context["module_pk"]).exclude(
×
89
            id__in=chapter_ids
90
        ).delete()
91

92
        for weight, chapter_data in enumerate(validated_data):
×
93
            chapter_data["weight"] = weight
×
94
            if "id" in chapter_data:
×
95
                instance = Chapter.objects.get(id=chapter_data["id"])
×
96
                assert instance.module_id == self.context["module_pk"]
×
97
                yield chapter_serializer.update(instance, chapter_data)
×
98
            else:
99
                yield chapter_serializer.create(chapter_data)
×
100

101
    def _send_component_updated_signal(self):
×
102
        component = components.modules["document_settings"]
×
103
        a4dashboard_signals.module_component_updated.send(
×
104
            sender=component.__class__,
105
            module=Module.objects.get(id=self.context["module_pk"]),
106
            component=component,
107
            user=self.context["request"].user,
108
        )
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