def getRoundsFor(self, name): a_room = self.get(name=name) categories = a_room.categories round_amount = a_room.rounds_amount questions_of_category = Question.objects.filter( categories__in=categories) if len(questions_of_category) >= round_amount: questions = [ val['id'] for val in random.sample(list(questions_of_category), k=round_amount) ] else: pending_questions_amount = round_amount - len( questions_of_category) extra_questions = Question.objects( id__nin=questions_of_category.values_list('id')) if len(extra_questions) >= pending_questions_amount: extra_questions_random = random.sample( list(extra_questions), k=pending_questions_amount) questions = list( chain(questions_of_category, extra_questions_random)) else: questions = list(chain(questions_of_category, extra_questions)) rounds = [] shuffled_questions = random.sample(list(questions), k=len(questions)) for question in shuffled_questions: round = Round(question=question) rounds.append(round) return rounds
def get() -> Response: output = Question.objects() return jsonify({'result': output})