From 0b22e1263aabb83a2881836d2769f08897f9f256 Mon Sep 17 00:00:00 2001
From: Arnaud Fabre <arnaud.fabre@camobscura.fr>
Date: Mon, 18 May 2015 11:01:42 +0200
Subject: [PATCH] updates representatives_votes format

---
 representatives_votes/format.json             |  7 ++--
 .../migrations/0003_auto_20150513_0936.py     | 26 ++++++++++++++
 .../migrations/0004_auto_20150513_1216.py     | 19 ++++++++++
 representatives_votes/models.py               |  7 ++--
 representatives_votes/utils.py                | 35 +++++++++++++++++++
 5 files changed, 88 insertions(+), 6 deletions(-)
 create mode 100644 representatives_votes/migrations/0003_auto_20150513_0936.py
 create mode 100644 representatives_votes/migrations/0004_auto_20150513_1216.py
 create mode 100644 representatives_votes/utils.py

diff --git a/representatives_votes/format.json b/representatives_votes/format.json
index 2537fbe..34aec27 100644
--- a/representatives_votes/format.json
+++ b/representatives_votes/format.json
@@ -1,11 +1,12 @@
 {
-    "Dossier": {
+    "dossier": {
         "title": "", // Dossier title, eg "2011/0167(NLE) EU/Australia, Canada, Japan, Korea, Mexico, Morocco, New Zealand, Singapore, Switzerland and United States Anti-Counterfeiting Trade Agreement (ACTA)"
         "reference": "", // Dossier reference, eg "2011/0167(NLE)"        
         "text": "", // Descriptipn
-        "Proposals": [{
+        "proposals": [{
             "title": "", // eg A7-0204/2012 - David Martin - Request for referral back to committee
-            "reference", "", // eg A7-0204/2012
+            "reference": "", // eg A7-0204/2012
+            "description": "",
             "kind": "", 
             "date": "", // timestamp
             "votes": [{
diff --git a/representatives_votes/migrations/0003_auto_20150513_0936.py b/representatives_votes/migrations/0003_auto_20150513_0936.py
new file mode 100644
index 0000000..d4d6036
--- /dev/null
+++ b/representatives_votes/migrations/0003_auto_20150513_0936.py
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('representatives_votes', '0002_auto_20150511_1209'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='proposal',
+            name='kind',
+            field=models.CharField(max_length=200, null=True),
+            preserve_default=True,
+        ),
+        migrations.AlterField(
+            model_name='proposal',
+            name='reference',
+            field=models.CharField(max_length=200, null=True),
+            preserve_default=True,
+        ),
+    ]
diff --git a/representatives_votes/migrations/0004_auto_20150513_1216.py b/representatives_votes/migrations/0004_auto_20150513_1216.py
new file mode 100644
index 0000000..ba4e5b3
--- /dev/null
+++ b/representatives_votes/migrations/0004_auto_20150513_1216.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('representatives_votes', '0003_auto_20150513_0936'),
+    ]
+
+    operations = [
+        migrations.RenameField(
+            model_name='vote',
+            old_name='representative_slug',
+            new_name='representative_name',
+        ),
+    ]
diff --git a/representatives_votes/models.py b/representatives_votes/models.py
index 45db9e9..60d2062 100644
--- a/representatives_votes/models.py
+++ b/representatives_votes/models.py
@@ -14,9 +14,9 @@ class Proposal(models.Model):
     dossier = models.ForeignKey(Dossier)
     title = models.CharField(max_length=500)
     description = models.TextField()
-    reference = models.CharField(max_length=200)
+    reference = models.CharField(max_length=200, null=True)
     datetime = models.DateTimeField()
-    
+    kind = models.CharField(max_length=200, null=True)
     total_abstain = models.IntegerField()
     total_against = models.IntegerField()
     total_for = models.IntegerField()
@@ -31,7 +31,8 @@ class Vote(models.Model):
 
     proposal = models.ForeignKey(Proposal)
 
-    representative_slug = models.CharField(max_length=200, blank=True, null=True)
+    # There are two representative fields for flexibility,
+    representative_name = models.CharField(max_length=200, blank=True, null=True)
     representative_remote_id = models.CharField(max_length=200, blank=True, null=True)
 
     position = models.CharField(max_length=10, choices=VOTECHOICES)
diff --git a/representatives_votes/utils.py b/representatives_votes/utils.py
new file mode 100644
index 0000000..bb8492c
--- /dev/null
+++ b/representatives_votes/utils.py
@@ -0,0 +1,35 @@
+from .models import Dossier
+
+def export_all_dossiers():
+    return [export_a_dossier(dossier) for dossier in Dossier.objects.all()]
+
+def export_a_dossier(dossier):
+    ret = {'dossier': {
+        'title': dossier.title,
+        'reference': dossier.reference,
+        'text': dossier.text
+    }}
+
+    ret['proposals'] = [export_a_proposal(proposal) for proposal in dossier.proposal_set.all()]
+           
+    return ret
+
+def export_a_proposal(proposal):
+    ret = {
+        'title': proposal.title,
+        'reference': proposal.reference,
+        'description': proposal.description,
+        'date': proposal.datetime.isoformat(),
+        'total_abstain': proposal.total_abstain,
+        'total_against': proposal.total_against,
+        'total_for': proposal.total_for
+    }
+
+    ret['votes'] = [export_a_vote(vote) for vote in proposal.vote_set.all()]
+    return ret
+
+def export_a_vote(vote):
+    return {
+        'representative': vote.representative_remote_id,
+        'postion': vote.position
+    }
-- 
GitLab