diff --git a/scripts/count_queries.py b/scripts/count_queries.py new file mode 100644 index 00000000..dc65881c --- /dev/null +++ b/scripts/count_queries.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +import json +import os +import sys + +import django + +sys.path.append("../server") + +os.environ.setdefault("IT_APP_ENVIRONMENT", "development") +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.base") +django.setup() + +from vbv_lernwelt.learnpath.models import LearningPath +from vbv_lernwelt.learnpath.serializers import LearningPathSerializer + + +def main(): + from django.conf import settings + settings.DEBUG = True + from django.db import connection + from django.db import reset_queries + reset_queries() + + learning_path = LearningPath.objects.filter(slug='versicherungsvermittlerin', locale__language_code='de-CH').first() + + serializer = LearningPathSerializer(learning_path) + print(serializer.data) + print(len(json.dumps(serializer.data))) + print(len(connection.queries)) + + +if __name__ == '__main__': + main() diff --git a/server/vbv_lernwelt/learnpath/models.py b/server/vbv_lernwelt/learnpath/models.py index 67534a3c..0e3d1c57 100644 --- a/server/vbv_lernwelt/learnpath/models.py +++ b/server/vbv_lernwelt/learnpath/models.py @@ -55,8 +55,9 @@ class Topic(Page): @classmethod def get_serializer_class(cls): - return get_it_serializer_class(cls, - field_names=['id', 'title', 'slug', 'type', 'translation_key', 'is_visible', ]) + return get_it_serializer_class( + cls, field_names=['id', 'title', 'slug', 'type', 'translation_key', 'is_visible', ] + ) class Meta: verbose_name = "Topic" diff --git a/server/vbv_lernwelt/learnpath/serializer_helpers.py b/server/vbv_lernwelt/learnpath/serializer_helpers.py index bfaeb48b..5ebfda78 100644 --- a/server/vbv_lernwelt/learnpath/serializer_helpers.py +++ b/server/vbv_lernwelt/learnpath/serializer_helpers.py @@ -20,5 +20,21 @@ class ItBaseSerializer(wagtail_serializers.BaseSerializer): meta_fields = [] + def __init__(self, *args, **kwargs): + self.descendants = kwargs.pop('descendants', None) + super().__init__(*args, **kwargs) + def get_children(self, obj): - return [c.specific.get_serializer_class()(c.specific).data for c in obj.get_children()] + if self.descendants: + children = _get_children(self.descendants, obj) + return [c.specific.get_serializer_class()(c.specific, descendants=self.descendants).data for c in children] + else: + return [c.specific.get_serializer_class()(c.specific).data for c in obj.get_children().specific()] + + +def _get_descendants(pages, obj): + return [c for c in pages if c.path.startswith(obj.path) and c.depth >= obj.depth] + + +def _get_children(pages, obj): + return [c for c in pages if c.path.startswith(obj.path) and obj.depth + 1 == c.depth] diff --git a/server/vbv_lernwelt/learnpath/serializers.py b/server/vbv_lernwelt/learnpath/serializers.py index 3aa8d494..ae4dadd9 100644 --- a/server/vbv_lernwelt/learnpath/serializers.py +++ b/server/vbv_lernwelt/learnpath/serializers.py @@ -1,7 +1,7 @@ from rest_framework import serializers from vbv_lernwelt.learnpath.models import Circle, LearningPath -from vbv_lernwelt.learnpath.serializer_helpers import get_it_serializer_class +from vbv_lernwelt.learnpath.serializer_helpers import get_it_serializer_class, _get_children class LearningPathSerializer(get_it_serializer_class(LearningPath, [])): @@ -10,7 +10,8 @@ class LearningPathSerializer(get_it_serializer_class(LearningPath, [])): meta_fields = [] def get_children(self, obj): - return [c.specific.get_serializer_class()(c.specific).data for c in obj.get_children()] + descendants = [p for p in obj.get_descendants().specific()] + return [c.specific.get_serializer_class()(c.specific, descendants=descendants).data for c in _get_children(descendants, obj)] def get_meta_label(self, obj): return obj._meta.label