performance optimization to get full learningpath content
This commit is contained in:
parent
2daf659d71
commit
96020bf83d
|
|
@ -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()
|
||||||
|
|
@ -55,8 +55,9 @@ class Topic(Page):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_serializer_class(cls):
|
def get_serializer_class(cls):
|
||||||
return get_it_serializer_class(cls,
|
return get_it_serializer_class(
|
||||||
field_names=['id', 'title', 'slug', 'type', 'translation_key', 'is_visible', ])
|
cls, field_names=['id', 'title', 'slug', 'type', 'translation_key', 'is_visible', ]
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = "Topic"
|
verbose_name = "Topic"
|
||||||
|
|
|
||||||
|
|
@ -20,5 +20,21 @@ class ItBaseSerializer(wagtail_serializers.BaseSerializer):
|
||||||
|
|
||||||
meta_fields = []
|
meta_fields = []
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
self.descendants = kwargs.pop('descendants', None)
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
def get_children(self, obj):
|
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]
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from vbv_lernwelt.learnpath.models import Circle, LearningPath
|
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, [])):
|
class LearningPathSerializer(get_it_serializer_class(LearningPath, [])):
|
||||||
|
|
@ -10,7 +10,8 @@ class LearningPathSerializer(get_it_serializer_class(LearningPath, [])):
|
||||||
meta_fields = []
|
meta_fields = []
|
||||||
|
|
||||||
def get_children(self, obj):
|
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):
|
def get_meta_label(self, obj):
|
||||||
return obj._meta.label
|
return obj._meta.label
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue