import os, csv
from django.core.management.base import NoArgsCommand
from django.conf import settings

from knesset.mks.models import Member,Party
from knesset.laws.models import Vote,VoteAction
from knesset.agendas.models import Agenda, AgendaVote

class Command(NoArgsCommand):

    def handle_noargs(self, **options):
        mks = Member.objects.order_by('current_party').values('id','name','current_party')
        for agenda in Agenda.objects.all():
            f = open(os.path.join(settings.DATA_ROOT, 'agenda_%d.csv' %
                                  agenda.id), 'wt')
            csv_writer = csv.writer(f)
            header = []
            header.append('Vote id')
            header.append('Score')
            for mk in mks:
                header.append(mk['id'])
            csv_writer.writerow(header)

            for agenda_vote in AgendaVote.objects.filter(agenda=agenda):
                row = []
                row.append(agenda_vote.vote.id)
                row.append(agenda_vote.score)
                mks_for = agenda_vote.vote.get_voters_id('for')
                mks_against = agenda_vote.vote.get_voters_id('against')
                for mk in mks:
                    if mk['id'] in mks_for:
                        row.append(1)
                    elif mk['id'] in mks_against:
                        row.append(-1)
                    else:
                        row.append(0)
                csv_writer.writerow(row)
            f.close()








@@ -673,16 +673,56 @@
 te id')%0A
+            header.append('Vote title')%0A
         
@@ -805,16 +805,54 @@
 .append(
+'%25s %25d' %25 (mk%5B'name'%5D.encode('utf8'), 
 mk%5B'id'%5D
@@ -852,16 +852,17 @@
 k%5B'id'%5D)
+)
 %0A       
@@ -1041,16 +1041,82 @@
 ote.id)%0A
+                row.append(agenda_vote.vote.title.encode('utf8'))%0A
         

update agenda export to csv to contain mks names and vote titles