Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ htmlcov/
wepublic_backend/staticfiles/
media/
activate
settings_local.py
19 changes: 18 additions & 1 deletion users/management/commands/create_users.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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'):
Expand Down Expand Up @@ -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',

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have database entries for those.

'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()
Expand Down
1 change: 1 addition & 0 deletions wepublic_backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
'wp_news',
'wp_newsletter',
'wp_party',
'wp_match',
]

MIDDLEWARE = [
Expand Down
35 changes: 35 additions & 0 deletions wepublic_backend/settings_local.py
Original file line number Diff line number Diff line change
@@ -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 = ''
1 change: 1 addition & 0 deletions wepublic_backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
]

Expand Down
Empty file added wp_match/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions wp_match/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions wp_match/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class WpMatchConfig(AppConfig):
name = 'wp_match'
Empty file added wp_match/migrations/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions wp_match/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions wp_match/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
9 changes: 9 additions & 0 deletions wp_match/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

from django.conf.urls import url

from . import views

urlpatterns = [
url(r'^$', views.index, name="index")

]
35 changes: 35 additions & 0 deletions wp_match/views.py
Original file line number Diff line number Diff line change
@@ -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)