Ejemplo n.º 1
0
def get_records(request, question_id):
    """
    Get records of users and comparison targets
    """
    try:
        question = Question.objects.get(id=question_id)
        survey = question.survey
    except:
        return Response(status=status.HTTP_400_BAD_REQUEST)

    comparison_targets = cache.get("survey:" + str(survey.id) + ":comparison_targets:records")
    if comparison_targets is None:
        comparison_targets = redis.set_records_of_comparison_targets_cache(survey)

    data = []

    for comparison_target in comparison_targets:
        data.append(
            {
                "name": comparison_target["name"],
                "color": comparison_target["color"],
                "choice_id": comparison_target["records"][question_id],
            }
        )

    if request.user.is_authenticated():
        answer = request.user.user_chosen_answers.select_related("choice").filter(choice__question=question)
        try:
            data.append({"name": "나", "color": "#9b59b6", "choice_id": answer[0].choice.id})
        # When user does not answered this question
        except:
            pass

    return Response(data)
def update_cache_when_comparison_target_updated(sender, instance, created, **kwargs):
    """
    Update cache when comparison target updated
    """
    redis.set_records_of_comparison_targets_cache(instance.survey)