def quiz_results(request, quiz_result_id): result = get_object_or_404(QuizResults, id=quiz_result_id) quiz = get_object_or_404(Quiz, id=result.quiz_id) party_params = get_parties_params() parties = Party.objects.filter(**party_params) created = result.created qa = {} if request.session.get('quiz_results_id', False): quiz_results_id = request.session['quiz_results_id'] party_params = get_parties_params() parties_data = Party.objects.filter(**party_params) \ .order_by('-last_result_parliment') quiz_answers_data = QuizAnswer.objects.filter( quiz_results_info__id=quiz_results_id) questions_data = get_questions_for_answers(quiz_answers_data) answers_data = get_answers_for_questions(questions_data) qa = get_qpa_table_data_with_quiz_results(questions_data, answers_data, parties_data, quiz_answers_data) parties_json = {} main_parties = [x for x in parties if x.last_result_parliment > 4] other_parties = [x for x in parties if x.last_result_parliment < 4] for party in parties: parties_json[party.id] = { 'name': party.name, 'color': party.color, 'main':bool(party.last_result_parliment > 4) } return render(request, "results.html", { 'result': json.loads(result.data), 'data': result.data, 'parties_json': json.dumps(parties_json), 'parties': parties, 'main_parties':main_parties, 'other_parties':other_parties, 'quiz': quiz, 'quiz_result_id': quiz_result_id, 'created': created, 'description': 'Resultat för ' + quiz.name, 'title': 'Resultat för ' + quiz.name, 'image': request.build_absolute_uri() + 'img.jpg', 'qa': qa})
def tag(request, tag_name): tag_data = get_object_or_404(Tag, name=from_slug(tag_name)) questions_data = get_questions_for_tag(tag_data.id) answers_data = get_answers_for_questions(questions_data) party_params = get_parties_params() parties_data = Party.objects.filter(**party_params) \ .order_by('-last_result_parliment') data = { 'questions': get_questions_json(questions_data), 'answers': get_answers_json(answers_data), 'parties': get_parties_json(parties_data) } json_data = json.dumps(data, separators=(',', ':')) form = TagModelForm(instance=tag_data) if settings.ADMIN_ENABLED else None context = { 'tag': tag_data, 'qpa': get_qpa_table_data(questions_data, answers_data, parties_data), 'form': form, 'data': json_data, 'user': get_user(request) } return render(request, 'tag.html', context)
def party(request, party_name): party_params = get_parties_params({'name': from_slug(party_name)}) party_data = get_object_or_404(Party, **party_params) answer_params = get_answers_params({ 'party_id': party_data.id, 'answer_type_id__isnull': False }) answers_data = Answer.objects.filter(**answer_params) questions_data = get_questions_for_answers(answers_data) data = { 'questions': get_questions_json(questions_data), 'answers': get_answers_json(answers_data), 'parties': get_parties_json([party_data]) } json_data = json.dumps(data, separators=(',', ':')) form = PartyModelForm( instance=party_data) if settings.ADMIN_ENABLED else None context = { 'party': party_data, 'qpa': get_qpa_table_data(questions_data, answers_data, [party_data]), 'data': json_data, 'form': form, 'user': get_user(request), } return render(request, 'party.html', context)
def search(request): if request.method == 'GET': query = request.GET["query"] # Min character length for performing a search if len(query) > 2: party_params = get_parties_params({'name__icontains': query}) parties = Party.objects.filter(**party_params).order_by('name') question_params = get_questions_params({'title__icontains': query}) questions = Question.objects.filter( **question_params).order_by('title') tag_params = get_tags_params({'name__icontains': query}) tags = Tag.objects.filter(**tag_params).order_by('name') quizzes_params = get_quizzes_params({'name__icontains': query}) quizzes = Quiz.objects.filter(**quizzes_params).order_by('name') parties_data = [{ 'data': { 'id': p.id, 'cat': 'Parties', 'type': 'p' }, 'value': p.name } for p in parties] questions_data = [{ 'data': { 'id': q.id, 'cat': 'Questions', 'type': 'q' }, 'value': q.title } for q in questions] tags_data = [{ 'data': { 'id': t.id, 'cat': 'Tags', 'type': 't' }, 'value': t.name } for t in tags] quizzes_data = [{ 'data': { 'id': q.id, 'cat': 'Quizzes', 'type': 'z' }, 'value': q.name } for q in quizzes] suggestions = parties_data + questions_data + tags_data + quizzes_data data = {'query': query, 'suggestions': suggestions} return HttpResponse(json.dumps(data), content_type='application/json')
def question(request, question_title): question_params = get_questions_params({ 'title': from_slug(question_title) }) question_data = get_object_or_404(Question, **question_params) answer_params = get_answers_params({ 'question_id': question_data.id, 'answer_type_id__isnull': False }) answers_data = Answer.objects.filter(**answer_params) party_params = get_parties_params() parties_data = Party.objects.filter(**party_params) \ .order_by('-last_result_parliment') tags_data = get_tags_for_question(question_data.id) data = { 'questions': get_questions_json([question_data]), 'answers': get_answers_json(answers_data), 'parties': get_parties_json(parties_data) } tags_string = ', '.join([tag.name for tag in tags_data]) json_data = json.dumps(data, separators=(',', ':')) if settings.ADMIN_ENABLED: question_form = QuestionModelForm(instance=question_data) answer_form = AnswerModelForm() else: question_form = None answer_form = None context = { 'question': question_data, 'qpa': get_qpa_table_data([question_data], answers_data, parties_data), 'tags': tags_data, 'data': json_data, 'question_form': question_form, 'answer_form': answer_form, 'user': get_user(request), 'tags_string': tags_string } return render(request, 'question.html', context)
def calculate_quiz_results(questions, a_data, city): party_params = get_parties_params() parties = Party.objects.filter(**party_params) \ .order_by('-last_result_parliment') question_ids = [q.id for q in questions] answer_params = get_answers_params({ 'question_id__in': question_ids, 'answer_type_id__isnull': False }) answers = Answer.objects.filter(**answer_params) qa = get_qpa_table_data(questions, answers, parties) parties_map = {} for p in parties: parties_map[p.id] = { 'answers': 0, 'correct': 0, 'incorrect': 0, 'points': 0 } for _, qd in qa['questions'].items(): q = qd['question'] for ad in qd['answers']: ua = a_data.get(str(q.id)) if ua and ad and ad.get('answer'): a = ad['answer'] party_entry = parties_map[a.party_id] if a.answer_type_id == ua: party_entry['correct'] += 1 elif ua != "0": party_entry['incorrect'] += 1 if a.answer_type_id is not None: party_entry['answers'] += 1 return parties_map
def questions(request): question_params = get_questions_params() questions_data = Question.objects.filter(**question_params) answer_params = get_answers_params({ 'answer_type_id__isnull': False }) answers_data = Answer.objects.filter(**answer_params) party_params = get_parties_params() parties_data = Party.objects.filter(**party_params) \ .order_by('-last_result_parliment') data = { 'questions': get_questions_json(questions_data), 'answers': get_answers_json(answers_data), 'parties': get_parties_json(parties_data), } json_data = json.dumps(data, separators=(',', ':')) form = QuestionModelForm() if settings.ADMIN_ENABLED else None context = {'data': json_data, 'user': get_user(request), 'form': form} return render(request, 'questions.html', context)
def parties(request): party_params = get_parties_params() parties_data = Party.objects.filter(**party_params) \ .order_by('-last_result_parliment') parties_representants = [] parties_other = [] for party in parties_data: if party_has_reps(party): parties_representants.append(party) else: parties_other.append(party) parties_1 = parties_representants[:len(parties_representants) // 2] parties_2 = parties_representants[len(parties_representants) // 2:] parties_other_1 = parties_other[:len(parties_other) // 2] parties_other_2 = parties_other[len(parties_other) // 2:] form = PartyModelForm() if settings.ADMIN_ENABLED else None context = { 'official': { 'left': parties_1, 'right': parties_2 }, 'other': { 'left': parties_other_1, 'right': parties_other_2 }, 'user': get_user(request), 'form': form } return render(request, 'parties.html', context)
def generate_quiz_result_image(results, id): party_params = get_parties_params() parties = Party.objects.filter(**party_params) no_parties = len(parties) padd_x = 25 padd_y = 100 height = 600 width = 600 logo_padding_top = 10 logo_dim = padd_x bar_between = 25 bar_width = (width - bar_between * (no_parties - 1) - padd_x * 2) \ / no_parties bar_max_height = height - 2 * padd_y - logo_dim - logo_padding_top text_padding_bottom = 5 text_character_width = 4 font_size = 12 blocks = "" texts = "" r = json.loads(results.data) for i, p in enumerate(parties): result = r[str(p.id)] total = result['correct'] + result['incorrect'] if total != 0: score = result['correct']/total else: score = 0 left = padd_x + i * (bar_width + bar_between) scoreStr = str(round(score * 100.0)) + "%" text_padding = (3 - len(scoreStr) + 1) * text_character_width top = padd_y + bar_max_height - bar_max_height * score block_args = { 'score': scoreStr, 'top': top, 'left': left, 'bottom': height - padd_y - logo_dim - logo_padding_top, 'logo_bottom': height - padd_y - logo_dim, 'right': left + bar_width, 'party_id': p.id, 'img_dim': bar_width, 'text_left': left + text_padding, 'text_top': top - text_padding_bottom, 'color': p.color } blocks = blocks + ''' -fill "{color}" -draw '''.format(**block_args) if score > 0.0: blocks = blocks + '''"rectangle {left},{top} {right},{bottom} ''' \ .format(**block_args) else: blocks = blocks + '''"''' print(score) blocks = blocks + '''image over {left},{logo_bottom} {img_dim},{img_dim} 'generate/images/large/{party_id}.png'"'''.format(**block_args) texts = texts + ''' -fill "{color}" -draw "translate -0,-0 text {text_left},{text_top} '{score}'" '''.format(**block_args) command_args = { 'width': width, 'height': height, 'blocks': blocks, 'id': id, 'texts': texts, 'font_size': font_size, 'crop_width': width - 10, 'crop_height': height - 10, 'outfile': settings.GENERATED_IMG_PATH + id + '.jpg' } command = '''convert -font Ubuntu -pointsize {font_size} -fill black -size {width}x{height} xc:transparent \ -crop {crop_width}x{crop_height}+0+0 +repage {blocks} \( +clone -background "#666" -shadow 30x3+4+3 \) \ \( -repage +20+20 generate/images/logo.png \) \ \( {texts} \) \ \( generate/images/bg.jpg \) \ -reverse -layers merge -crop {width}x{height} -flatten {outfile}''' \ .format(**command_args) process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) process.wait()