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):
|
||||
updated_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
|
||||
|
||||
if content['type'] == 'content_list_item':
|
||||
|
|
@ -85,18 +85,21 @@ class ChapterNode(DjangoObjectType):
|
|||
def resolve_content_blocks(self, info, **kwargs):
|
||||
user = info.context.user
|
||||
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
|
||||
publisher_content_blocks = ContentBlock.get_by_parent(self).filter(user_created=False)
|
||||
user_created_content_blocks = ContentBlock.get_by_parent(self).filter(user_created=True, owner=user)
|
||||
publisher_content_blocks = by_parent.filter(user_created=False)
|
||||
user_created_content_blocks = by_parent.filter(user_created=True, owner=user)
|
||||
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)
|
||||
|
||||
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,
|
||||
visible_for__in=school_classes).union(
|
||||
user_created_content_blocks = by_parent.filter(user_created=True,
|
||||
visible_for__in=school_classes).union(
|
||||
self_created_content_blocks)
|
||||
|
||||
return publisher_content_blocks.union(user_created_content_blocks)
|
||||
|
|
@ -180,6 +183,12 @@ class ModuleNode(DjangoObjectType):
|
|||
chapters = Chapter.objects.live().descendant_of(self)
|
||||
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):
|
||||
pk = graphene.Int()
|
||||
|
|
@ -278,6 +287,7 @@ class BookQuery(object):
|
|||
elif slug is not None:
|
||||
module = Module.objects.get(slug=slug)
|
||||
|
||||
|
||||
return module
|
||||
|
||||
def resolve_topic(self, info, **kwargs):
|
||||
|
|
|
|||
Loading…
Reference in New Issue