Optimize some queries to be easier on the database
This commit is contained in:
parent
35cdd5ff5f
commit
ef6711965f
|
|
@ -48,7 +48,7 @@ class ContentBlockNode(DjangoObjectType):
|
||||||
def resolve_contents(self, info, **kwargs):
|
def resolve_contents(self, info, **kwargs):
|
||||||
updated_stream_data = []
|
updated_stream_data = []
|
||||||
for content in self.contents.stream_data:
|
for content in self.contents.stream_data:
|
||||||
if not are_solutions_enabled_for(info.context.user, self.module) and content['type'] == 'solution':
|
if content['type'] == 'solution' and not are_solutions_enabled_for(info.context.user, self.module):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if content['type'] == 'content_list_item':
|
if content['type'] == 'content_list_item':
|
||||||
|
|
@ -85,17 +85,20 @@ class ChapterNode(DjangoObjectType):
|
||||||
def resolve_content_blocks(self, info, **kwargs):
|
def resolve_content_blocks(self, info, **kwargs):
|
||||||
user = info.context.user
|
user = info.context.user
|
||||||
school_classes = user.school_classes.values_list('pk')
|
school_classes = user.school_classes.values_list('pk')
|
||||||
|
by_parent = ContentBlock.get_by_parent(self).prefetch_related(
|
||||||
|
'visible_for__schoolclass').prefetch_related(
|
||||||
|
'hidden_for__schoolclass')
|
||||||
|
|
||||||
if user.has_perm('users.can_manage_school_class_content'): # teacher
|
if user.has_perm('users.can_manage_school_class_content'): # teacher
|
||||||
publisher_content_blocks = ContentBlock.get_by_parent(self).filter(user_created=False)
|
publisher_content_blocks = by_parent.filter(user_created=False)
|
||||||
user_created_content_blocks = ContentBlock.get_by_parent(self).filter(user_created=True, owner=user)
|
user_created_content_blocks = by_parent.filter(user_created=True, owner=user)
|
||||||
else: # student
|
else: # student
|
||||||
publisher_content_blocks = ContentBlock.get_by_parent(self).filter(user_created=False).exclude(
|
publisher_content_blocks = by_parent.filter(user_created=False).exclude(
|
||||||
hidden_for__in=school_classes)
|
hidden_for__in=school_classes)
|
||||||
|
|
||||||
self_created_content_blocks = ContentBlock.get_by_parent(self).filter(user_created=True, owner=user)
|
self_created_content_blocks = by_parent.filter(user_created=True, owner=user)
|
||||||
|
|
||||||
user_created_content_blocks = ContentBlock.get_by_parent(self).filter(user_created=True,
|
user_created_content_blocks = by_parent.filter(user_created=True,
|
||||||
visible_for__in=school_classes).union(
|
visible_for__in=school_classes).union(
|
||||||
self_created_content_blocks)
|
self_created_content_blocks)
|
||||||
|
|
||||||
|
|
@ -180,6 +183,12 @@ class ModuleNode(DjangoObjectType):
|
||||||
chapters = Chapter.objects.live().descendant_of(self)
|
chapters = Chapter.objects.live().descendant_of(self)
|
||||||
return ChapterBookmark.objects.filter(chapter__in=chapters, user=user)
|
return ChapterBookmark.objects.filter(chapter__in=chapters, user=user)
|
||||||
|
|
||||||
|
def resolve_objective_groups(self, root, **kwargs):
|
||||||
|
return self.objective_groups.all() \
|
||||||
|
.prefetch_related('hidden_for__schoolclass') \
|
||||||
|
.prefetch_related('visible_for__schoolclass') \
|
||||||
|
.prefetch_related('objective_progress')
|
||||||
|
|
||||||
|
|
||||||
class TopicNode(DjangoObjectType):
|
class TopicNode(DjangoObjectType):
|
||||||
pk = graphene.Int()
|
pk = graphene.Int()
|
||||||
|
|
@ -278,6 +287,7 @@ class BookQuery(object):
|
||||||
elif slug is not None:
|
elif slug is not None:
|
||||||
module = Module.objects.get(slug=slug)
|
module = Module.objects.get(slug=slug)
|
||||||
|
|
||||||
|
|
||||||
return module
|
return module
|
||||||
|
|
||||||
def resolve_topic(self, info, **kwargs):
|
def resolve_topic(self, info, **kwargs):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue