Skip to content

Commit

Permalink
working on #140: stats comparison based on current knesset data. also…
Browse files Browse the repository at this point in the history
… some minor PEP8 and performance improvements
  • Loading branch information
ofri committed Sep 2, 2013
1 parent 59295e5 commit 89f04d5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 75 deletions.
5 changes: 4 additions & 1 deletion laws/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ def votes_against_party_count(self):
return VoteAction.objects.filter(member__current_party=self.party, against_party=True).count()

def votes_count(self):
return VoteAction.objects.filter(member__current_party=self.party).exclude(type='no-vote').count()
d = Knesset.objects.current_knesset().start_date
return VoteAction.objects.filter(
member__current_party=self.party,
vote__time__gt=d).exclude(type='no-vote').count()

def votes_per_seat(self):
return round(float(self.votes_count()) / self.party.number_of_seats,1)
Expand Down
18 changes: 12 additions & 6 deletions mks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,18 @@ def coalition_status(self):
return self.current_party.is_coalition

def recalc_bill_statistics(self):
self.bills_stats_proposed = self.bills.count()
self.bills_stats_pre = self.bills.filter(
stage__in=['2', '3', '4', '5', '6']).count()
self.bills_stats_first = self.bills.filter(
stage__in=['4', '5', '6']).count()
self.bills_stats_approved = self.bills.filter(stage='6').count()
d = Knesset.objects.current_knesset().start_date
self.bills_stats_proposed = self.proposals_proposed.filter(
date__gte=d).count()
self.bills_stats_pre = self.proposals_proposed.filter(
date__gte=d,
bill__stage__in=['2', '3', '4', '5', '6']).count()
self.bills_stats_first = self.proposals_proposed.filter(
date__gte=d,
bill__stage__in=['4', '5', '6']).count()
self.bills_stats_approved = self.proposals_proposed.filter(
date__gte=d,
bill__stage='6').count()
self.save()

def recalc_average_weekly_presence_hours(self):
Expand Down
130 changes: 62 additions & 68 deletions mks/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import urllib
from operator import attrgetter
from itertools import chain

from django.conf import settings
from django.db.models import Sum, Q
Expand Down Expand Up @@ -76,7 +77,7 @@ def get_context_data(self, **kwargs):
context['friend_pages'] = self.pages
context['stat_type'] = info
context['title'] = dict(self.pages)[info]
context['csv_path'] = 'api/v2/member' + '?' + self.request.GET.urlencode() + '&format=csv&limit=0'
context['csv_path'] = 'api/v2/member' + '?' + self.request.GET.urlencode() + '&format=csv&limit=0'
context['past_mks'] = Member.current_knesset.filter(is_current=False)

# We make sure qs are lists so that the template can get min/max
Expand Down Expand Up @@ -409,25 +410,22 @@ def get_context_data(self, **kwargs):
context['friend_pages'] = self.pages
context['stat_type'] = info

if info=='seats':
if info == 'seats':
context['coalition'] = context['coalition'].annotate(extra=Sum('number_of_seats')).order_by('-extra')
context['opposition'] = context['opposition'].annotate(extra=Sum('number_of_seats')).order_by('-extra')
context['norm_factor'] = 1
context['baseline'] = 0
if info=='votes-per-seat':

if info == 'votes-per-seat':
m = 0
for p in context['coalition']:
for p in chain(context['coalition'], context['opposition']):
p.extra = p.voting_statistics.votes_per_seat()
if p.extra > m:
m = p.extra
for p in context['opposition']:
p.extra = p.voting_statistics.votes_per_seat()
if p.extra > m:
m = p.extra
context['norm_factor'] = m/20
context['norm_factor'] = m / 20
context['baseline'] = 0

if info=='discipline':
if info == 'discipline':
m = 100
for p in context['coalition']:
p.extra = p.voting_statistics.discipline()
Expand Down Expand Up @@ -495,98 +493,94 @@ def get_context_data(self, **kwargs):
context['norm_factor'] = (10.0-m)/15
context['baseline'] = m-1

if info=='bills-proposed':
if info == 'bills-proposed':
m = 9999
for p in context['coalition']:
p.extra = len(set(Bill.objects.filter(proposers__current_party=p).values_list('id',flat=True)))/p.number_of_seats
if p.extra < m:
m = p.extra
for p in context['opposition']:
p.extra = len(set(Bill.objects.filter(proposers__current_party=p).values_list('id',flat=True)))/p.number_of_seats
d = Knesset.objects.current_knesset().start_date
for p in chain(context['coalition'], context['opposition']):
p.extra = round(float(
len(set(Bill.objects.filter(
proposers__current_party=p,
proposals__date__gt=d).values_list('id', flat=True))
)) / p.number_of_seats, 1)
if p.extra < m:
m = p.extra
context['norm_factor'] = m/2
context['norm_factor'] = m / 2
context['baseline'] = 0

if info=='bills-pre':
if info == 'bills-pre':
m = 9999
for p in context['coalition']:
p.extra = round(float(len(set(Bill.objects.filter(Q(proposers__current_party=p),Q(stage='2')|Q(stage='3')|Q(stage='4')|Q(stage='5')|Q(stage='6')).values_list('id',flat=True))))/p.number_of_seats,1)
if p.extra < m:
m = p.extra
for p in context['opposition']:
p.extra = round(float(len(set(Bill.objects.filter(Q(proposers__current_party=p),Q(stage='2')|Q(stage='3')|Q(stage='4')|Q(stage='5')|Q(stage='6')).values_list('id',flat=True))))/p.number_of_seats,1)
d = Knesset.objects.current_knesset().start_date
for p in chain(context['coalition'], context['opposition']):
p.extra = round(float(
len(set(Bill.objects.filter(
Q(stage='2') | Q(stage='3') | Q(stage='4') |
Q(stage='5') | Q(stage='6'),
proposers__current_party=p,
proposals__date__gt=d).values_list('id', flat=True))
)) / p.number_of_seats, 1)
if p.extra < m:
m = p.extra
context['norm_factor'] = m/2
context['norm_factor'] = m / 2
context['baseline'] = 0

if info=='bills-first':
if info == 'bills-first':
m = 9999
for p in context['coalition']:
p.extra = round(float(len(set(Bill.objects.filter(Q(proposers__current_party=p),Q(stage='4')|Q(stage='5')|Q(stage='6')).values_list('id',flat=True))))/p.number_of_seats,1)
if p.extra < m:
m = p.extra
for p in context['opposition']:
p.extra = round(float(len(set(Bill.objects.filter(Q(proposers__current_party=p),Q(stage='4')|Q(stage='5')|Q(stage='6')).values_list('id',flat=True))))/p.number_of_seats,1)
d = Knesset.objects.current_knesset().start_date
for p in chain(context['coalition'], context['opposition']):
p.extra = round(float(
len(set(Bill.objects.filter(
Q(stage='4') | Q(stage='5') | Q(stage='6'),
proposers__current_party=p,
proposals__date__gt=d).values_list('id', flat=True))
)) / p.number_of_seats, 1)
if p.extra < m:
m = p.extra
context['norm_factor'] = m/2
context['norm_factor'] = m / 2
context['baseline'] = 0

if info=='bills-approved':
if info == 'bills-approved':
m = 9999
for p in context['coalition']:
p.extra = round(float(len(set(Bill.objects.filter(proposers__current_party=p,stage='6').values_list('id',flat=True))))/p.number_of_seats,1)
d = Knesset.objects.current_knesset().start_date
for p in chain(context['coalition'], context['opposition']):
p.extra = round(float(
len(set(Bill.objects.filter(
proposers__current_party=p,
proposals__date__gt=d,
stage='6').values_list('id', flat=True))
)) / p.number_of_seats, 1)
if p.extra < m:
m = p.extra
for p in context['opposition']:
p.extra = round(float(len(set(Bill.objects.filter(proposers__current_party=p,stage='6').values_list('id',flat=True))))/p.number_of_seats,1)
if p.extra < m:
m = p.extra
context['norm_factor'] = m/2
context['norm_factor'] = m / 2
context['baseline'] = 0

if info=='presence':
if info == 'presence':
m = 9999
for p in context['coalition']:
awp = [member.average_weekly_presence() for member in p.members.all() if member.average_weekly_presence()]
if awp:
p.extra = round(float(sum(awp))/len(awp),1)
else:
p.extra = 0
if p.extra < m:
m = p.extra
for p in context['opposition']:
awp = [member.average_weekly_presence() for member in p.members.all() if member.average_weekly_presence()]
for p in chain(context['coalition'], context['opposition']):
awp = [member.average_weekly_presence() for member in
p.members.all()]
awp = [a for a in awp if a]
if awp:
p.extra = round(float(sum(awp))/len(awp),1)
p.extra = round(float(sum(awp)) / len(awp), 1)
else:
p.extra = 0
if p.extra < m:
m = p.extra
context['norm_factor'] = m/2
context['norm_factor'] = m / 2
context['baseline'] = 0

if info=='committees':
if info == 'committees':
m = 9999
for p in context['coalition']:
cmpm = [member.committee_meetings_per_month() for member in p.members.all() if member.committee_meetings_per_month()]
if cmpm:
p.extra = round(float(sum(cmpm))/len(cmpm),1)
else:
p.extra = 0
if p.extra < m:
m = p.extra
for p in context['opposition']:
cmpm = [member.committee_meetings_per_month() for member in p.members.all() if member.committee_meetings_per_month()]
for p in chain(context['coalition'], context['opposition']):
cmpm = [member.committee_meetings_per_month() for member in
p.members.all()]
cmpm = [c for c in cmpm if c]
if cmpm:
p.extra = round(float(sum(cmpm))/len(cmpm),1)
p.extra = round(float(sum(cmpm)) / len(cmpm), 1)
else:
p.extra = 0
if p.extra < m:
m = p.extra
context['norm_factor'] = m/2
context['norm_factor'] = m / 2
context['baseline'] = 0

context['title'] = _('Parties by %s') % dict(self.pages)[info]
Expand Down

0 comments on commit 89f04d5

Please sign in to comment.