From e7f3e38c5fe96907bd8a8f74f9f061d69ed8c545 Mon Sep 17 00:00:00 2001 From: Jonas Braun Date: Fri, 25 Aug 2017 18:14:33 +0200 Subject: [PATCH 1/7] local settings --- .gitignore | 1 - wepublic_backend/settings_local.py | 35 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 wepublic_backend/settings_local.py diff --git a/.gitignore b/.gitignore index 5adfa5c..9406407 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,3 @@ htmlcov/ wepublic_backend/staticfiles/ media/ activate -settings_local.py diff --git a/wepublic_backend/settings_local.py b/wepublic_backend/settings_local.py new file mode 100644 index 0000000..8457773 --- /dev/null +++ b/wepublic_backend/settings_local.py @@ -0,0 +1,35 @@ +import os + + +SECRET_KEY = 'secretsecretsecret' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ['*'] + +PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(PROJECT_ROOT, 'database.sqlite') + } +} + +MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media') +MEDIA_URL = '/media/' + +STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles') +STATIC_URL = '/static/' + +EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend" +EMAIL_FILE_PATH = '/tmp/mails' +EMAIL_HOST = '' +EMAIL_PORT = '' +EMAIL_HOST_USER = '' +EMAIL_HOST_PASSWORD = '' +REPORT_MAILS = [] +REPORT_MAILS_ACTIVE = True + +SLACK_NOTIFICATIONS_ACTIVE = False +SLACK_NOTIFICATIONS_URL = '' From eb07b92b623bab99012e007a469b42e70cf76863 Mon Sep 17 00:00:00 2001 From: Jonas Braun Date: Fri, 25 Aug 2017 23:03:33 +0200 Subject: [PATCH 2/7] wp_match app basics --- wepublic_backend/settings.py | 1 + wepublic_backend/urls.py | 1 + wp_match/__init__.py | 0 wp_match/admin.py | 3 +++ wp_match/apps.py | 5 +++++ wp_match/migrations/__init__.py | 0 wp_match/models.py | 3 +++ wp_match/tests.py | 3 +++ wp_match/urls.py | 9 +++++++++ wp_match/views.py | 10 ++++++++++ 10 files changed, 35 insertions(+) create mode 100644 wp_match/__init__.py create mode 100644 wp_match/admin.py create mode 100644 wp_match/apps.py create mode 100644 wp_match/migrations/__init__.py create mode 100644 wp_match/models.py create mode 100644 wp_match/tests.py create mode 100644 wp_match/urls.py create mode 100644 wp_match/views.py diff --git a/wepublic_backend/settings.py b/wepublic_backend/settings.py index 8079aea..f13c72c 100644 --- a/wepublic_backend/settings.py +++ b/wepublic_backend/settings.py @@ -48,6 +48,7 @@ 'wp_news', 'wp_newsletter', 'wp_party', + 'wp_match', ] MIDDLEWARE = [ diff --git a/wepublic_backend/urls.py b/wepublic_backend/urls.py index 804563b..c3b4ff7 100644 --- a/wepublic_backend/urls.py +++ b/wepublic_backend/urls.py @@ -35,6 +35,7 @@ router.register(r'Parties', PartyViewSet, 'parties') urlpatterns = [ url(r'^admin/', admin.site.urls), + url(r'^v1/Match/', include('wp_match.urls')), url(r'^v1/', include(router.urls)), ] diff --git a/wp_match/__init__.py b/wp_match/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wp_match/admin.py b/wp_match/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/wp_match/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/wp_match/apps.py b/wp_match/apps.py new file mode 100644 index 0000000..6e64ba2 --- /dev/null +++ b/wp_match/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class WpMatchConfig(AppConfig): + name = 'wp_match' diff --git a/wp_match/migrations/__init__.py b/wp_match/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wp_match/models.py b/wp_match/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/wp_match/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/wp_match/tests.py b/wp_match/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/wp_match/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/wp_match/urls.py b/wp_match/urls.py new file mode 100644 index 0000000..bcf6e73 --- /dev/null +++ b/wp_match/urls.py @@ -0,0 +1,9 @@ + +from django.conf.urls import url + +from . import views + +urlpatterns = [ + url(r'^$', views.index, name="index") + +] diff --git a/wp_match/views.py b/wp_match/views.py new file mode 100644 index 0000000..0d6a4c0 --- /dev/null +++ b/wp_match/views.py @@ -0,0 +1,10 @@ +from rest_framework import viewsets + +from django.http import JsonResponse + + +def index(request): + + + + return JsonResponse([ { 'content': 'asdf' } ], safe=False) From 23fb53da02fb89b54a4fc3143560978c035e0653 Mon Sep 17 00:00:00 2001 From: Jonas Braun Date: Fri, 25 Aug 2017 23:37:14 +0200 Subject: [PATCH 3/7] create_users script: add parties to answers --- users/management/commands/create_users.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/users/management/commands/create_users.py b/users/management/commands/create_users.py index 2eb162a..5f5f4af 100644 --- a/users/management/commands/create_users.py +++ b/users/management/commands/create_users.py @@ -1,5 +1,6 @@ from django.core.management.base import BaseCommand from users.models import User +from wp_party.models import Party from wp_core.models import ( Question, Answer, @@ -28,6 +29,8 @@ def handle(self, *args, **options): self.add_tags() if(model == 'question' or model == 'all'): self.add_questions() + if(model == 'party' or model == 'all'): + self.add_parties() if(model == 'answer' or model == 'all'): self.add_answers() if(model == 'vote-question' or model == 'all'): @@ -85,16 +88,30 @@ def add_questions(self, number=50): q.tags.add(Tag.objects.all()[randint(0, tag_count-1)]) q.save() + def add_parties(self, number=5): + + parties = { 'SPD': 'Sozialdemokratische Partei Deutschlands', + 'CDU': 'Christlich Demokratische Union', + 'FDP': 'Freie Demokratische Partei', + 'AfD': 'Alternative für Deutschland', + 'DIE GRÜNEN': 'Bündnis 90 / Die Grünen' } + + for short_name, name in parties.items(): + p = Party(short_name=short_name, name=name) + p.save() + def add_answers(self, number=70): fake = Factory.create('de_DE') user_count = User.objects.count() question_count = Question.objects.count() + party_count = Party.objects.count() for _ in range(0, number): user = User.objects.all()[randint(0, user_count-1)] text = fake.text(max_nb_chars=300) question = Question.objects.all()[randint(0, question_count-1)] - Answer.objects.create(text=text, user=user, question=question) + party = Party.objects.all()[randint(0, party_count-1)] + Answer.objects.create(text=text, user=user, question=question, party=party) def add_votes_question(self, number_max=30): users = User.objects.all() From ad9eb5c4219171db179bd9007ced6eb354cca4eb Mon Sep 17 00:00:00 2001 From: Jonas Braun Date: Sat, 26 Aug 2017 01:26:26 +0200 Subject: [PATCH 4/7] fix user to admin@wepublic.me --- wp_match/views.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/wp_match/views.py b/wp_match/views.py index 0d6a4c0..85bc7e4 100644 --- a/wp_match/views.py +++ b/wp_match/views.py @@ -1,10 +1,15 @@ from rest_framework import viewsets +from rest_framework.permissions import IsAuthenticated +from rest_framework.decorators import detail_route from django.http import JsonResponse +from users.models import User + +@detail_route(methods=['get'], permission_classes=[IsAuthenticated]) def index(request): - + user = User.objects.get(email='admin@wepublic.me') - return JsonResponse([ { 'content': 'asdf' } ], safe=False) + return JsonResponse([ { 'content': str(user) } ], safe=False) From 08237ccbb9b7012c27141c8ab861aca95a82e004 Mon Sep 17 00:00:00 2001 From: Celsa Diaz Date: Sat, 26 Aug 2017 01:44:55 +0200 Subject: [PATCH 5/7] add percentage calulation to views --- wp_match/views.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/wp_match/views.py b/wp_match/views.py index 85bc7e4..844868e 100644 --- a/wp_match/views.py +++ b/wp_match/views.py @@ -1,6 +1,8 @@ from rest_framework import viewsets from rest_framework.permissions import IsAuthenticated from rest_framework.decorators import detail_route +from wp_core.models import Answer +from collections import Counter from django.http import JsonResponse @@ -9,7 +11,13 @@ @detail_route(methods=['get'], permission_classes=[IsAuthenticated]) def index(request): - user = User.objects.get(email='admin@wepublic.me') - return JsonResponse([ { 'content': str(user) } ], safe=False) + answers = Answer.objects.filter(voteanswer__user=user, voteanswer__up=True) + c = Counter() + c.update([a.party for a in answers]) + keys = [k.short_name for k in list(c.keys())] + a = dict(zip(keys, c.values())) + total = {(k, (v / sum(a.values())) * 100) for k, v in a.items()} + + return JsonResponse([{'content': str(total)}], safe=False) From 83e8c25f9bf716eb7fcec535c410f451d2a3e1f8 Mon Sep 17 00:00:00 2001 From: Celsa Diaz Date: Sat, 26 Aug 2017 11:32:52 +0200 Subject: [PATCH 6/7] parties percentage response to nice list of dicts --- wp_match/views.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/wp_match/views.py b/wp_match/views.py index 844868e..4adddee 100644 --- a/wp_match/views.py +++ b/wp_match/views.py @@ -7,17 +7,29 @@ from django.http import JsonResponse from users.models import User +import json @detail_route(methods=['get'], permission_classes=[IsAuthenticated]) def index(request): user = User.objects.get(email='admin@wepublic.me') + # get the amount of positive voted answers per user answers = Answer.objects.filter(voteanswer__user=user, voteanswer__up=True) c = Counter() c.update([a.party for a in answers]) keys = [k.short_name for k in list(c.keys())] a = dict(zip(keys, c.values())) - total = {(k, (v / sum(a.values())) * 100) for k, v in a.items()} + total = [(k, (v / sum(a.values())) * 100) for k, v in a.items()] + parties = [x[0] for x in total] + percentage = [x[1] for x in total] - return JsonResponse([{'content': str(total)}], safe=False) + response = [] + for i, party in enumerate(total): + d_party = { + 'name': parties[i], + 'percentage': percentage[i] + } + response.append(d_party) + + return JsonResponse([{'content': json.dumps(response)}], safe=False) From be7a949134610711930431192e93904c77b881e9 Mon Sep 17 00:00:00 2001 From: Jonas Braun Date: Sat, 26 Aug 2017 13:31:36 +0200 Subject: [PATCH 7/7] reponse object --- wp_match/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp_match/views.py b/wp_match/views.py index 4adddee..24b98ad 100644 --- a/wp_match/views.py +++ b/wp_match/views.py @@ -32,4 +32,4 @@ def index(request): } response.append(d_party) - return JsonResponse([{'content': json.dumps(response)}], safe=False) + return JsonResponse(response, safe=False)