Skip to content

Instantly share code, notes, and snippets.

@kalda341
Created December 20, 2018 02:40
Show Gist options
  • Save kalda341/8401a423894d996b0ecde55df28ece4c to your computer and use it in GitHub Desktop.
Save kalda341/8401a423894d996b0ecde55df28ece4c to your computer and use it in GitHub Desktop.
def get_queryset(self):
def filter_question(question_type=None, prefix='user__questions'):
kwargs = {
'{}__course'.format(prefix): models.F('course'),
}
if question_type:
kwargs['{}__{}__isnull'.format(prefix, question_type)] = False
return models.Q(**kwargs)
def filter_answer(question_type=None):
return models.Q()
return (filter_question(question_type, prefix='user__answers__question') &
models.Q(user__answers__hidden_to_others=False))
def votes(vote_type, field, filter):
return models.Count('user__{}__votes'.format(field), filter=(
filter & models.Q(user__questions__votes__type=vote_type)
), distinct=True)
return super().get_queryset().annotate(
num_slide_questions=models.Count('user__questions', filter=filter_question('slidequestion'), distinct=True),
num_meta_questions=models.Count('user__questions', filter=filter_question('metaquestion'), distinct=True),
num_slide_question_answers=models.Count('user__answers', filter=filter_answer('slidequestion'), distinct=True),
num_test_question_answers=models.Count('user__answers', filter=filter_answer('testquestion'), distinct=True),
num_meta_question_answers=models.Count('user__answers', filter=filter_answer('metaquestion'), distinct=True),
question_upvotes=votes('+1', 'questions', filter_question()),
question_downvotes=votes('-1', 'questions', filter_question()),
answer_upvotes=votes('+1', 'answers', filter_answer()),
answer_downvotes=votes('-1', 'answers', filter_answer()),
num_helpful_slides=models.Count('user__slide_question_links', filter=models.Q(
user__slide_question_links__value=1,
user__slide_question_links__question__course=models.F('course'),
), distinct=True),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment