refine random problems features

This commit is contained in:
lixh 2019-07-04 20:12:34 -07:00
parent 6c22457143
commit d8f21bccd8

View File

@ -107,29 +107,27 @@ class ContestProblemAPI(APIView):
problem_data = ProblemSafeSerializer(problem).data problem_data = ProblemSafeSerializer(problem).data
return self.success(problem_data) return self.success(problem_data)
random.seed(request.user.id + request.contest.id)
# Random problems for every students in contest # Random problems for every students in contest
def random_contest_problems(self, difficulty='Low', count=3): random.seed(request.user.id + self.contest.id)
contest_problem_set = Problem.objects.select_related("created_by").filter(contest=self.contest, visible=True, difficulty=difficulty).all()
contest_problem_id = []
for i in contest_problem_set:
contest_problem_id.append(i._id)
contest_problem_id = random.sample(contest_problem_id, count)
contest_problems = Problem.objects.select_related("created_by").filter(contest=self.contest, visible=True, _id__in=contest_problem_id)
return contest_problems
contest_low_problems = random_contest_problems(difficulty='Low', count=3) query_set = Problem.objects.select_related("created_by").filter(contest=self.contest, visible=True).all()
contest_mid_problems = random_contest_problems(difficulty='Mid', count=2) low_problems_query_set = query_set.filter(difficulty='Low')
contest_high_problems = random_contest_problems(difficulty='High', count=0) low_problems_query_set_id = list(low_problems_query_set.values_list('_id', flat=True))
low_selected_id = random.sample(low_problems_query_set_id, 3)
contest_low_problems = low_problems_query_set.filter(_id__in=low_selected_id)
contest_problems = (contest_low_problems | contest_mid_problems | contest_high_problems).order_by("_id") mid_problems_query_set = query_set.filter(difficulty='Mid')
mid_problems_query_set_id = list(mid_problems_query_set.values_list('_id', flat=True))
mid_selected_id = random.sample(mid_problems_query_set_id, 2)
contest_mid_problems = mid_problems_query_set.filter(_id__in=mid_selected_id)
# self.contest_problem_list = random.sample(contest_low_problem_set, 3) high_problems_query_set = query_set.filter(difficulty='High')
# self.contest_problem_list.extend(random.sample(contest_mid_problem_set, 2)) high_problems_query_set_id = list(high_problems_query_set.values_list('_id', flat=True))
# self.contest_problem_list.extend(random.sample(contest_high_problem_set, 0)) high_selected_id = random.sample(high_problems_query_set_id, 0)
contest_high_problems = high_problems_query_set.filter(_id__in=high_selected_id)
contest_problems = contest_low_problems | contest_mid_problems | contest_high_problems
# # contest_problems = Problem.objects.select_related("created_by").filter(contest=self.contest, visible=True, _id__in=self.probelm_list).order_by('_id')
if self.contest.problem_details_permission(request.user): if self.contest.problem_details_permission(request.user):
data = ProblemSerializer(contest_problems, many=True).data data = ProblemSerializer(contest_problems, many=True).data
self._add_problem_status(request, data) self._add_problem_status(request, data)