views.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from core.models import Politician, Question, Statistic, Answer, Category
  2. from django.http import JsonResponse
  3. from django.utils.encoding import force_text
  4. from django.core.serializers.json import DjangoJSONEncoder
  5. def v1(request):
  6. questions = [
  7. {
  8. 'id': x.id,
  9. 'category_id': x.category.id,
  10. 'text': {
  11. 'de': x.text_de if x.text_de else '',
  12. 'fr': x.text_fr if x.text_fr else '',
  13. 'it': x.text_it if x.text_it else ''
  14. },
  15. 'description': {
  16. 'de': x.description_de if x.description_de else '',
  17. 'fr': x.description_fr if x.description_fr else '',
  18. 'it': x.description_it if x.description_it else ''
  19. },
  20. 'preferred_answer': x.preferred_answer
  21. }
  22. for x
  23. in Question.objects.all().order_by('id')
  24. ]
  25. categories = [
  26. {
  27. 'id': x.id,
  28. 'name': {
  29. 'de': x.name_de if x.name_de else '',
  30. 'fr': x.name_fr if x.name_fr else '',
  31. 'it': x.name_it if x.name_it else ''
  32. }
  33. }
  34. for x
  35. in Category.objects.all().order_by('id')
  36. ]
  37. politicians = []
  38. for x in Politician.objects.all().order_by('id'):
  39. if Statistic.objects.filter(politician=x).exists():
  40. p = {
  41. 'id': x.id,
  42. 'first_name': x.first_name if x.image else None,
  43. 'last_name': x.last_name if x.image else None,
  44. 'is_member_of_parliament': x.is_member_of_parliament,
  45. 'image': x.image.url if x.image else None,
  46. 'state': x.state.name if x.state else None,
  47. 'past_contributions': x.past_contributions,
  48. 'future_plans': x.future_plans,
  49. 'answers': []
  50. }
  51. if x.party:
  52. p['party'] = {
  53. 'name': x.party.name,
  54. 'shortname': x.party.shortname
  55. }
  56. elif x.party_other:
  57. p['party']= {
  58. 'name': x.party_other,
  59. 'shortname': None
  60. }
  61. else:
  62. p['party'] = None
  63. for a in Answer.objects.filter(politician=x).order_by('id'):
  64. p['answers'].append({
  65. 'question_id': a.question.id,
  66. 'answer': a.agreement_level,
  67. 'note': a.note
  68. })
  69. politicians.append(p)
  70. return JsonResponse({ 'politicians': politicians, 'questions': questions, 'categories': categories })