def create_context_review(request): data = {} if "id" in request.GET: user = RequestContext(request)["user"] challenge = Challenge.objects.get(pk=request.GET.get("id")) if not challenge.is_enabled_for_user(user): raise Http404 if challenge.has_enough_user_reviews(user): raise Http404 if not challenge.submitted_by_user(user): raise Http404 review = Review.get_open_review(challenge, user) if not review: # number of hours needed to pass until elaboration is applicable as candidate offset = randint(ReviewConfig.get_candidate_offset_min(), ReviewConfig.get_candidate_offset_max()) review_candidate = Elaboration.get_review_candidate(challenge, user, offset) if review_candidate: review = Review(elaboration=review_candidate, reviewer=user) review.save() else: return data data["review"] = review data["stack_id"] = challenge.get_stack().id review_questions = ReviewQuestion.objects.filter(challenge=challenge).order_by("order") data["questions"] = review_questions return data
def create_context_review(request): data = {} if 'id' in request.GET: user = RequestContext(request)['user'] challenge = Challenge.objects.get(pk=request.GET.get('id')) if not challenge.is_enabled_for_user(user): raise Http404 if challenge.has_enough_user_reviews(user): raise Http404 if not challenge.submitted_by_user(user): raise Http404 review = Review.get_open_review(challenge, user) if not review: # number of hours needed to pass until elaboration is applicable as candidate offset = randint(ReviewConfig.get_candidate_offset_min(), ReviewConfig.get_candidate_offset_max()) review_candidate = Elaboration.get_review_candidate( challenge, user, offset) if review_candidate: review = Review(elaboration=review_candidate, reviewer=user) review.save() else: return data data['review'] = review data['stack_id'] = challenge.get_stack().id review_questions = ReviewQuestion.objects.filter( challenge=challenge).order_by("order") data['questions'] = review_questions return data
def get_random_review_candidate(challenge, user): # Wait x hours before making elaborations available for review offset = randint(ReviewConfig.get_candidate_offset_min(), ReviewConfig.get_candidate_offset_max()) threshold = datetime.now() - timedelta(hours=offset) # Exclude elaborations the user has already submitted a review for already_submitted_reviews_ids = (Review.objects.filter( reviewer=user, elaboration__challenge=challenge).values_list('elaboration__id', flat=True)) # Get all possible candidates, ignore treshold for now because we need # to fall back to a random elaboration if no elaboration is old enough candidates = (Elaboration.objects.filter( challenge=challenge, submission_time__isnull=False, user__is_staff=False).annotate( num_reviews=Count('review')).exclude(user=user).exclude( id__in=already_submitted_reviews_ids) ).order_by('num_reviews') # Separate candidates old_enough_candidates, newer_candidates = [], [] for candidate in candidates: old_enough_candidates.append( candidate ) if candidate.submission_time < threshold else newer_candidates.append( candidate) if len(old_enough_candidates) > 0: chosen_candidate = old_enough_candidates[0] elif len(newer_candidates) > 0: chosen_candidate = newer_candidates[0] else: # Fall back to dummy elaborations candidates = (Elaboration.objects.filter( challenge=challenge, submission_time__isnull=False).annotate( num_reviews=Count('review')).exclude(user=user).exclude( id__in=already_submitted_reviews_ids) ).order_by('num_reviews') chosen_candidate = candidates[0] return {'chosen_by': 'random', 'candidate': chosen_candidate}
def get_random_review_candidate(challenge, user): # Wait x hours before making elaborations available for review offset = randint(ReviewConfig.get_candidate_offset_min(), ReviewConfig.get_candidate_offset_max()) threshold = datetime.now() - timedelta(hours=offset) # Exclude elaborations the user has already submitted a review for already_submitted_reviews_ids = ( Review.objects .filter(reviewer=user, elaboration__challenge=challenge) .values_list('elaboration__id', flat=True) ) # Get all possible candidates, ignore treshold for now because we need # to fall back to a random elaboration if no elaboration is old enough candidates = ( Elaboration.objects .filter(challenge=challenge, submission_time__isnull=False, user__is_staff=False) .annotate(num_reviews=Count('review')) .exclude(user=user) .exclude(id__in=already_submitted_reviews_ids) ).order_by('num_reviews') # Separate candidates old_enough_candidates, newer_candidates = [], [] for candidate in candidates: old_enough_candidates.append(candidate) if candidate.submission_time < threshold else newer_candidates.append(candidate) if len(old_enough_candidates) > 0: chosen_candidate = old_enough_candidates[0] elif len(newer_candidates) > 0: chosen_candidate = newer_candidates[0] else: # Fall back to dummy elaborations candidates = ( Elaboration.objects .filter(challenge=challenge, submission_time__isnull=False) .annotate(num_reviews=Count('review')) .exclude(user=user) .exclude(id__in=already_submitted_reviews_ids) ).order_by('num_reviews') chosen_candidate = candidates[0] return { 'chosen_by': 'random', 'candidate': chosen_candidate }
def test_review_config_offset(self): assert ReviewConfig.get_candidate_offset_min() == 0 assert ReviewConfig.get_candidate_offset_max() == 0 ReviewConfig(candidate_offset_min=1, candidate_offset_max=2).save() assert ReviewConfig.get_candidate_offset_min() == 1 assert ReviewConfig.get_candidate_offset_max() == 2
def test_review_config_offset(self): assert ReviewConfig.get_candidate_offset_min() == 0 assert ReviewConfig.get_candidate_offset_max() == 0 ReviewConfig(candidate_offset_min=1, candidate_offset_max=2).save() assert ReviewConfig.get_candidate_offset_min() == 1 assert ReviewConfig.get_candidate_offset_max() == 2