def compare_reset_view(request): messages.success(request, _('answers_resetted')) response = HttpResponseRedirect(reverse('compare')) set_cookie(response, 'answers', {}, 30) set_cookie(response, 'statistics', {}, 30) return response
def compare_view(request): questions = Question.objects.all().order_by('question_number') data = [] session_answers = get_cookie(request, 'answers', {}) session_statistics = get_cookie(request, 'statistics', {}) if request.method == "POST": for question in questions: qid = 'question_%d' % question.id session_answers[qid] = request.POST.get(qid, 0) categories = Category.objects.all() for category in categories: cq = Question.objects.filter(category=category) values = [] for question in cq: values.append( abs( question.preferred_answer - int(session_answers.get( 'question_%d' % question.id, 0)) ) ) session_statistics['category_%d' % category.id] = ( 10 - sum(values) / float(len(cq))) request.COOKIES['answers'] = session_answers request.COOKIES['statistics'] = session_statistics messages.success(request, _('answers_saved_successfully_evaluate')) for question in questions: item = { 'question' : question, 'value' : session_answers.get('question_%d' % question.id, 0) } data.append(item) if request.method == "GET": response = render( request, 'core/compare/index.html', { 'data' : data, 'meta' : default_meta } ) else: response = redirect('%s?evaluate=1' % reverse('candidates')) set_cookie(response, 'answers', session_answers, 30) set_cookie(response, 'statistics', session_statistics, 30) return response
def compare_view(request): questions = Question.objects.all().order_by('question_number') data = [] session_answers = get_cookie(request, 'answers', {}) session_statistics = get_cookie(request, 'statistics', {}) if request.POST: for question in questions: qid = 'question_%d' % question.id session_answers[qid] = request.POST.get(qid, 0) categories = Category.objects.all() for category in categories: cq = Question.objects.filter(category=category) values = [] for question in cq: values.append( abs( question.preferred_answer - int(session_answers.get( 'question_%d' % question.id, 0)) ) ) session_statistics['category_%d' % category.id] = ( 10 - sum(values) / float(len(cq))) request.COOKIES['answers'] = session_answers request.COOKIES['statistics'] = session_statistics messages.success(request, _('answers_saved_successfully_evaluate')) for question in questions: item = { 'question' : question, 'value' : session_answers.get('question_%d' % question.id, 0) } data.append(item) response = render( request, 'core/compare/index.html', { 'data' : data } ) set_cookie(response, 'answers', session_answers, 30) set_cookie(response, 'statistics', session_statistics, 30) return response
def share_view(request): response = redirect('%s?evaluate=1' % reverse('candidates')) cookies = {'statistics': {}, 'answers': {}} for k, v in request.GET.items(): cookie_name, variable_name = split('_', k, maxsplit=1) # V can be a float or int in a string and since we can not # parse a float in a string to int we did it this way cookies[cookie_name][variable_name] = int(float(v)) set_cookie(response, 'answers', cookies['answers'], 30) set_cookie(response, 'statistics', cookies['statistics'], 30) return response