Commit e1211920 authored by njoyard's avatar njoyard

Merge branch 'add-json-chart-views' into 'master'

Add chart data json views



See merge request !167
parents 5f9c3ed1 d7bf73fe
Pipeline #175 passed with stages
in 18 minutes and 7 seconds
import json
from .base import BaseTest
class ChartViewsTest(BaseTest):
def test_theme_score(self):
url = '/charts/data/theme-scores/1/'
data = json.loads(self.client.get(url).content)
expected = {
"theme": "Etat d'urgence",
"scores": [-305.0, -305.0, -265.0, -235.0, -220.0, -215.0, -205.0,
-205.0, -195.0, -195.0, -190.0, -185.0, -135.0, -115.0,
-110.0, -105.0, -100.0, -100.0, -100.0, -90.0, -90.0,
-90.0, -90.0, -55.0, -25.0, -5.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 35.0]
}
assert data == expected
def test_chamber_score(self):
url = '/charts/data/chamber-scores/1/'
data = json.loads(self.client.get(url).content)
expected = {
"chamber": "European Parliament",
"scores": [-1312.0, -1216.0, -1216.0, -1216.0, -1216.0, -1216.0,
-1216.0, -1208.0, -1200.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1208.0,
1224.0, 1468.0, 1468.0, 1480.0]
}
assert data == expected
def test_theme_score_queries(self):
url = '/charts/data/theme-scores/1/'
# First query to set session variables
self.client.get(url)
with self.assertNumQueries(2):
# 1 query for the theme
# 1 query for theme scores
self.client.get(url)
def test_chamber_score_queries(self):
url = '/charts/data/chamber-scores/1/'
# First query to set session variables
self.client.get(url)
with self.assertNumQueries(2):
# 1 query for the chamber
# 1 query for chamber scores
self.client.get(url)
......@@ -9,6 +9,9 @@ from views.autocomplete import (
ProposalAutocomplete,
RepresentativeAutocomplete,
)
from views.charts import ThemeScoresJSONView, ChamberScoresJSONView
from views.dossier_detail_base import DossierDetailBase
from views.dossier_detail_recommendations import DossierDetailRecommendations
from views.dossier_detail_proposals import DossierDetailProposals
......@@ -184,6 +187,20 @@ urlpatterns = [
name='redirect-group-representative-list'
),
# Chart data URLs
url(
r'^charts/data/theme-scores/(?P<theme>\d+)/$',
ThemeScoresJSONView.as_view(),
name='charts-data-theme-scores'
),
url(
r'^charts/data/chamber-scores/(?P<chamber>\d+)/$',
ChamberScoresJSONView.as_view(),
name='charts-data-chamber-scores'
),
# Testing URLs
url(
......
import datetime
import json
from django.db.models import Q
from django.http import HttpResponse, HttpResponseNotFound
from django.views import generic
from representatives.models import Chamber, Representative
from memopol_themes.models import Theme
from memopol_scores.models import ThemeScore
class ThemeScoresJSONView(generic.View):
"""
Returns JSON with all score points for a given theme as follows:
{
"theme": "<theme_name>",
"scores": [-99, -98, -98, ... 100, 102, 102, 102]
}
"""
def get(self, request, *args, **kwargs):
try:
theme = Theme.objects.get(id=kwargs['theme'])
except Theme.DoesNotExist:
return HttpResponseNotFound()
data = {
"theme": theme.name,
"scores": [ts['score'] for ts in
ThemeScore.objects.filter(theme=theme).order_by('score')
.values('score')]
}
return HttpResponse(json.dumps(data), content_type='application/json')
class ChamberScoresJSONView(generic.View):
"""
Returns JSON with all score points for a given chamber as follows:
{
"chamber": "<chamber_name>",
"scores": [-99, -98, -98, ... 100, 102, 102, 102]
}
"""
def get(self, request, *args, **kwargs):
try:
chamber = Chamber.objects.get(id=kwargs['chamber'])
except Chamber.DoesNotExist:
return HttpResponseNotFound()
today = datetime.date.today()
meps = Representative.objects.filter(
Q(mandates__end_date__gte=today) |
Q(mandates__end_date__isnull=True),
mandates__group__chamber=chamber
).select_related('representative_score') \
.order_by('representative_score__score') \
.distinct()
data = {
"chamber": chamber.name,
"scores": [r.representative_score.score for r in meps]
}
return HttpResponse(json.dumps(data), content_type='application/json')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment