Permalink
Browse files

Merge branch 'master' of https://github.com/ofri/Open-Knesset into ha…

…sadna
  • Loading branch information...
2 parents 231bdaf + d2aa45a commit a16e299ba46f82b611c3f84cbcc2bd18846cdd8a @daonb committed Mar 31, 2012
@@ -4,12 +4,12 @@
def getAllAgendaPartyVotes():
cursor = connection.cursor()
- cursor.execute(QUERY)
+ cursor.execute(PARTY_QUERY)
results = dict(map(lambda (key,group):(key,map(lambda g:(g[1],float(g[2])),list(group))),
groupby(cursor.fetchall(),key=itemgetter(0))))
return results
-QUERY = """
+PARTY_QUERY = """
SELECT a.agendaid,
a.partyid,
round(cast(coalesce(v.totalvotevalue,0.0)/a.totalscore*100.0 as numeric),2) score
@@ -57,3 +57,57 @@ def getAllAgendaPartyVotes():
ON a.agendaid = v.agenda_id
AND a.partyid = v.partyid
ORDER BY agendaid,score desc"""
+
+def getAllAgendaMkVotes():
+ cursor = connection.cursor()
+ cursor.execute(MK_QUERY)
+ results = dict(map(lambda (key,group):(key,map(lambda g:(g[1],float(g[2])),list(group))),
+ groupby(cursor.fetchall(),key=itemgetter(0))))
+ return results
+
+MK_QUERY = """
+SELECT a.agendaid,
+ v.memberid,
+ Round(CAST(Coalesce(v.totalvotevalue, 0.0) / a.totalscore * 100.0 AS
+ NUMERIC), 2
+ ) score
+FROM (SELECT agenda_id agendaid,
+ SUM(Abs(score * importance)) totalscore
+ FROM agendas_agendavote
+ GROUP BY agenda_id) a
+ LEFT OUTER JOIN (SELECT agenda_id,
+ memberid,
+ SUM(forvotes) - SUM(againstvotes) totalvotevalue
+ FROM (SELECT a.agenda_id,
+ p.memberid,
+ CASE p.vtype
+ WHEN 'for' THEN p.numvotes * a.VALUE
+ ELSE 0
+ END forvotes,
+ CASE p.vtype
+ WHEN 'against' THEN
+ p.numvotes * a.VALUE
+ ELSE 0
+ END againstvotes
+ FROM (SELECT m.id memberid,
+ v.vote_id voteid,
+ v.TYPE vtype,
+ COUNT(*) numvotes
+ FROM laws_voteaction v
+ INNER JOIN mks_member m
+ ON v.member_id = m.id
+ WHERE v.TYPE IN ( 'for', 'against' )
+ GROUP BY m.id,
+ v.vote_id,
+ v.TYPE) p
+ INNER JOIN (SELECT vote_id,
+ agenda_id,
+ score * importance AS
+ VALUE
+ FROM agendas_agendavote) a
+ ON p.voteid = a.vote_id) b
+ GROUP BY agenda_id,
+ memberid) v
+ ON a.agendaid = v.agenda_id
+ORDER BY agendaid,
+ score DESC"""
@@ -1,4 +1,5 @@
import logging
+from operator import itemgetter
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.decorators import login_required
@@ -18,7 +19,7 @@
MeetingLinkingFormSet)
from models import Agenda, AgendaVote, AgendaMeeting
-from queries import getAllAgendaPartyVotes
+from queries import getAllAgendaPartyVotes,getAllAgendaMkVotes
from django.test import Client
from django.core.handlers.wsgi import WSGIRequest
@@ -93,30 +94,24 @@ def get_context_data(self, *args, **kwargs):
context['watched_members'] = watched_members
all_mks = 'all_mks' in self.request.GET.keys()
+ allAgendaMkVotes = cache.get('AllAgendaMkVotes')
+ if not allAgendaMkVotes:
+ allAgendaMkVotes = getAllAgendaMkVotes()
+ cache.set('AllAgendaMkVotes',allAgendaMkVotes,1800)
+ context['agenda_mk_values']=dict(allAgendaMkVotes[agenda.id])
if all_mks:
- cached_context = cache.get('agenda_mks_%d_all_mks' % agenda.id)
- if not cached_context:
- mks = agenda.selected_instances(Member, top=200, bottom=0)
- cached_context = {'selected_mks':mks['top'],'all_mks':True}
- cache.set('agenda_mks_%d_all_mks' % agenda.id,
- cached_context, 900)
- context.update(cached_context)
+ context['all_mks_ids']=map(itemgetter(0),sorted(allAgendaMkVotes[agenda.id],key=itemgetter(1),reverse=True)[:200])
+ context['all_mks']=True
else:
- cached_context = cache.get('agenda_mks_%d' % agenda.id)
- if not cached_context:
- mks = agenda.selected_instances(Member, top=5,bottom=5)
- cached_context = {'selected_mks_top': mks['top'],
- 'selected_mks_bottom': mks['bottom'],
- 'all_mks':False}
- cache.set('agenda_mks_%d' % agenda.id, cached_context, 900)
- context.update(cached_context)
-
- cached_context = cache.get('agenda_parties_%d' % agenda.id)
- if not cached_context:
- selected_parties = agenda.selected_instances(Party, top=20,bottom=0)['top']
- cached_context = {'selected_parties': selected_parties }
- cache.set('agenda_parties_%d' % agenda.id, cached_context, 900)
- context.update(cached_context)
+ context['mks_top']=map(itemgetter(0),sorted(allAgendaMkVotes[agenda.id],key=itemgetter(1),reverse=True)[:5])
+ context['mks_bottom']=map(itemgetter(0),sorted(allAgendaMkVotes[agenda.id],key=itemgetter(1),reverse=False)[:5])
+
+ allAgendaPartyVotes = cache.get('AllAgendaPartyVotes')
+ if not allAgendaPartyVotes:
+ allAgendaPartyVotes = getAllAgendaPartyVotes()
+ cache.set('AllAgendaPartyVotes',allAgendaPartyVotes,1800)
+ context['agenda_party_values']=dict(allAgendaPartyVotes[agenda.id])
+ context['agendaTopParties']=map(itemgetter(0),sorted(allAgendaPartyVotes[agenda.id],key=itemgetter(1),reverse=True)[:20])
cached_context = cache.get('agenda_votes_%d' % agenda.id)
if not cached_context:
@@ -126,6 +121,15 @@ def get_context_data(self, *args, **kwargs):
cache.set('agenda_votes_%d' % agenda.id, cached_context, 900)
context.update(cached_context)
+ # Optimization: get all parties and members before rendering
+ # Further possible optimization: only bring parties/members needed for rendering
+ parties_objects = Party.objects.all()
+ partiesDict = dict(map(lambda party:(party.id,party),parties_objects))
+ context['parties']=partiesDict
+
+ member_objects = Member.objects.all()
+ membersDict = dict(map(lambda mk:(mk.id,mk),member_objects))
+ context['members']=membersDict
return context
class AgendaMkDetailView (DetailView):
@@ -94,4 +94,6 @@ def pagination(page_obj, paginator, request):
@register.inclusion_tag('laws/vote_list_item.html')
def vote_list_item(vote, watched_members=[]):
- return {'vote': vote, 'watched_members': watched_members}
+ return {'vote': vote,
+ 'tags': vote.tags,
+ 'watched_members': watched_members}
@@ -1,4 +1,5 @@
{% extends "site_base.html" %}
+{% load lookup %}
{% load i18n %}
{% load tagging_tags %}
{% load laws_extra %}
@@ -59,42 +60,60 @@
<tr>
<td colspan="3" style="text-align:center">{% trans 'Agenda MKs' %}</td>
</tr>
- {% for selected_mk in selected_mks %}
+ {% for selected_mk_id in all_mks_ids %}
+ {% with members|lookup:selected_mk_id as selected_mk %}
<tr>
- <td><a class="hashnav item dontwrap party-{{selected_mk.current_party.id}}" title="{{selected_mk.current_party.name}}" id="detail-{{ selected_mk.id }}" href="{% url mk-agenda-detail object.id selected_mk.id %}">{{ selected_mk }}</a></td>
+ {% with parties|lookup:selected_mk.current_party_id as selected_mk_party %}
+ {% with agenda_mk_values|lookup:selected_mk.id as selected_mk_score %}
+ <td><a class="hashnav item dontwrap party-{{selected_mk_party.id}}" title="{{selected_mk_party.name}}" id="detail-{{ selected_mk.id }}" href="{% url mk-agenda-detail object.id selected_mk.id %}">{{ selected_mk }}</a></td>
<td class="numeric">{{selected_mk.score|floatformat:"0"}}%</td>
<td>
- <div id="selected_mk_{{ selected_mk.id }}_percentile" class="percentile {% if selected_mk.score < 0 %}percentile_against{% endif %}"></div>
- <script>set_percentile('selected_mk_{{ selected_mk.id }}',({{ selected_mk.score }}+100)/2);</script>
+ <div id="selected_mk_{{ selected_mk.id }}_percentile" class="percentile {% if selected_mk_score < 0 %}percentile_against{% endif %}"></div>
+ <script>set_percentile('selected_mk_{{ selected_mk.id }}',({{ selected_mk_score }}+100)/2);</script>
</td>
+ {% endwith %}
+ {% endwith %}
</tr>
+ {% endwith %}
{% endfor %}
{% else %}
<tr>
<td colspan="3" style="text-align:center">{% trans 'Agenda Top MKs' %}</td>
</tr>
- {% for selected_mk in selected_mks_top %}
+ {% for selected_mk_id in mks_top %}
+ {% with members|lookup:selected_mk_id as selected_mk %}
<tr>
- <td><a class="hashnav item dontwrap party-{{selected_mk.current_party.id}}" title="{{selected_mk.current_party.name}}" id="detail-{{ selected_mk.id }}" href="{% url mk-agenda-detail object.id selected_mk.id %}">{{ selected_mk }}</a></td>
+ {% with parties|lookup:selected_mk.current_party_id as selected_mk_party %}
+ {% with agenda_mk_values|lookup:selected_mk.id as selected_mk_score %}
+ <td><a class="hashnav item dontwrap party-{{selected_mk_party.id}}" title="{{selected_mk_party.name}}" id="detail-{{ selected_mk.id }}" href="{% url mk-agenda-detail object.id selected_mk.id %}">{{ selected_mk }}</a></td>
<td class="numeric">{{selected_mk.score|floatformat:"0"}}%</td>
<td>
- <div id="selected_mk_{{ selected_mk.id }}_percentile" class="percentile {% if selected_mk.score < 0 %}percentile_against{% endif %}"></div>
- <script>set_percentile('selected_mk_{{ selected_mk.id }}',({{ selected_mk.score }}+100)/2);</script>
+ <div id="selected_mk_{{ selected_mk.id }}_percentile" class="percentile {% if selected_mk_score < 0 %}percentile_against{% endif %}"></div>
+ <script>set_percentile('selected_mk_{{ selected_mk.id }}',({{ selected_mk_score }}+100)/2);</script>
</td>
+ {% endwith %}
+ {% endwith %}
</tr>
+ {% endwith %}
{% endfor %}
<tr>
<td colspan="3" style="text-align:center">{% trans 'Agenda Bottom MKs' %}</td>
</tr>
- {% for selected_mk in selected_mks_bottom %}
+ {% for selected_mk_id in mks_bottom %}
+ {% with members|lookup:selected_mk_id as selected_mk %}
<tr>
- <td><a class="hashnav item dontwrap party-{{selected_mk.current_party.id}}" title="{{selected_mk.current_party.name}}" id="detail-{{ selected_mk.id }}" href="{% url mk-agenda-detail object.id selected_mk.id %}">{{ selected_mk }}</a></td>
+ {% with parties|lookup:selected_mk.current_party_id as selected_mk_party %}
+ {% with agenda_mk_values|lookup:selected_mk.id as selected_mk_score %}
+ <td><a class="hashnav item dontwrap party-{{selected_mk_party.id}}" title="{{selected_mk_party.name}}" id="detail-{{ selected_mk.id }}" href="{% url mk-agenda-detail object.id selected_mk.id %}">{{ selected_mk }}</a></td>
<td class="numeric">{{selected_mk.score|floatformat:"0"}}%</td>
<td>
- <div id="selected_mk_{{ selected_mk.id }}_percentile" class="percentile {% if selected_mk.score < 0 %}percentile_against{% endif %}"></div>
- <script>set_percentile('selected_mk_{{ selected_mk.id }}',({{ selected_mk.score }}+100)/2);</script>
+ <div id="selected_mk_{{ selected_mk.id }}_percentile" class="percentile {% if selected_mk_score < 0 %}percentile_against{% endif %}"></div>
+ <script>set_percentile('selected_mk_{{ selected_mk.id }}',({{ selected_mk_score }}+100)/2);</script>
</td>
- </tr>
+ {% endwith %}
+ {% endwith %}
+ </tr>
+ {% endwith %}
{% endfor %}
{% endif %}
</table>
@@ -105,22 +124,26 @@
<p>{% trans 'Click the # to highlight MKs from this party' %}</p>
<div class="list-wrap">
<table>
- {% for selected_party in selected_parties %}
+ {% for selected_party_id in agendaTopParties %}
+ {% with parties|lookup:selected_party_id as selected_party %}
<tr>
<td><a id="party-highlight-{{selected_party.id}}" href="#">#</a><a class="hashnav item dontwrap" id="party-link-{{ selected_party.id }}" href="{% url party-detail selected_party.id %}">{{ selected_party }}</a></td>
- <td class="numeric">{{selected_party.score|floatformat:"0"}}%</td>
+ {% with agenda_party_values|lookup:selected_party.id as selected_party_score %}
+ <td class="numeric">{{selected_party_score|floatformat:"0"}}%</td>
<td>
- <div id="selected_party_{{ selected_party.id }}_percentile" class="percentile {% if selected_party.score < 0 %}percentile_against{% endif %}"></div>
+ <div id="selected_party_{{ selected_party.id }}_percentile" class="percentile {% if selected_party_score < 0 %}percentile_against{% endif %}"></div>
<script>
- set_percentile('selected_party_{{ selected_party.id }}',({{ selected_party.score }}+100)/2);
+ set_percentile('selected_party_{{ selected_party.id }}',({{ selected_party_score }}+100)/2);
$('#party-highlight-{{selected_party.id}}').click(function(){
$('.party-{{selected_party.id}}').toggleClass('highlight');
$('#party-highlight-{{selected_party.id}}').toggleClass('highlight');
return false;
});
</script>
</td>
+ {% endwith %}
</tr>
+ {% endwith %}
{% endfor %}
</table>
</div>
@@ -52,8 +52,8 @@
</div>
</a>
<div class="item-metadata">
- {% if vote.tags %}
- {% item_tags vote.tags %}
+ {% if tags %}
+ {% item_tags tags %}
{% else %}
<!--{% trans 'No tags' %}-->
{% endif %}

0 comments on commit a16e299

Please sign in to comment.