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 quizzes(request): quiz_params = get_quizzes_params() quizzes_data = Quiz.objects.filter(**quiz_params) form = QuizModelForm() if settings.ADMIN_ENABLED else None context = {'quizzes': quizzes_data, 'form': form, 'user': get_user(request)} return render(request, 'quizzes.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 quiz(request, quiz_name): quiz_params = get_quizzes_params({ 'name': from_slug(quiz_name) }) quiz_data = get_object_or_404(Quiz, **quiz_params) questions_data = get_questions_for_quiz(quiz_data.id) kommuner_data = get_kommuner_json(Kommun.objects.all()) lan_data = get_lans_json(Lan.objects.all()) form = QuizModelForm(instance=quiz_data) if settings.ADMIN_ENABLED else None context = {'quiz': quiz_data, 'questions': questions_data, 'kommuner': json.dumps(kommuner_data, separators=(',', ':')), 'lan': json.dumps(lan_data, separators=(',', ':')), 'form': form, 'user': get_user(request)} return render(request, 'quiz.html', context)
def answer(request, question_title, party_name): question_params = get_questions_params( {'title': from_slug(question_title)}) question_data = get_object_or_404(Question, **question_params) party_data = get_object_or_404(Party, name=from_slug(party_name)) answer_params = get_answers_params({ 'question_id': question_data.id, 'party_id': party_data.id }) answer_data = Answer.objects.filter( **answer_params).order_by('-date').first() answer_text = answer_data.party.name.capitalize() if answer_data.answer_type.id == "1": answer_text += " tycker att " elif answer_data.answer_type.id == "2": answer_text += " tycker inte att " else: answer_text += " har ingen åsikt om " + answer_data.answer_type.id answer_text += question_data.title form = AnswerModelForm( instance=answer_data) if settings.ADMIN_ENABLED else None context = { 'answer': answer_data, 'party': party_data, 'question': question_data, 'description': answer_text, 'form': form, 'user': get_user(request), 'title': answer_data.party.name.capitalize() + 's svar på om ' + question_data.title } return render(request, 'answer.html', context)
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 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)