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/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() 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/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 = '' 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..24b98ad --- /dev/null +++ b/wp_match/views.py @@ -0,0 +1,35 @@ +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 + +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()] + parties = [x[0] for x in total] + percentage = [x[1] for x in total] + + response = [] + for i, party in enumerate(total): + d_party = { + 'name': parties[i], + 'percentage': percentage[i] + } + response.append(d_party) + + return JsonResponse(response, safe=False)