From f4edd89b1c951f4eb0aedad473f238eb32f4f1e2 Mon Sep 17 00:00:00 2001
From: Arnaud Fabre <arnaud.fabre@camobscura.fr>
Date: Tue, 2 Jun 2015 14:15:19 +0200
Subject: [PATCH] improves the way import works (smart update dossiers)

---
 .../commands/import_dossier_from_toutatis.py    |  7 +++----
 representatives_votes/serializers.py            | 17 +++++------------
 representatives_votes/utils.py                  |  6 ++++--
 3 files changed, 12 insertions(+), 18 deletions(-)

diff --git a/representatives_votes/management/commands/import_dossier_from_toutatis.py b/representatives_votes/management/commands/import_dossier_from_toutatis.py
index 91c24a6..7978754 100644
--- a/representatives_votes/management/commands/import_dossier_from_toutatis.py
+++ b/representatives_votes/management/commands/import_dossier_from_toutatis.py
@@ -18,7 +18,6 @@
 #
 # Copyright (C) 2013 Laurent Peuch <cortex@worlddomination.be>
 # Copyright (C) 2015 Arnaud Fabre <af@laquadrature.net>
-
 import json
 from urllib2 import urlopen
 
@@ -34,10 +33,10 @@ class Command(BaseCommand):
                                   'TOUTATIS_SERVER',
                                   'http://toutatis.mm.staz.be')
         search_url = toutatis_server + '/api/dossiers/?reference=%s' % reference
+        print('Import dossier from %s' % search_url)
         data = json.load(urlopen(search_url))
-        if len(data) != 1:
+        if data['count'] != 1:
             raise Exception('Search should return one and only one result')
-        detail_url = data[0]['url']
+        detail_url = data['results'][0]['url']
         data = json.load(urlopen(detail_url))
-
         import_a_dossier(data)
diff --git a/representatives_votes/serializers.py b/representatives_votes/serializers.py
index 6f23929..8f589c3 100644
--- a/representatives_votes/serializers.py
+++ b/representatives_votes/serializers.py
@@ -22,15 +22,6 @@ import representatives_votes.models as models
 from rest_framework import serializers
 
 from django.db import transaction
-from django.db import connection
-from django.db.utils import OperationalError
-
-def truncate_model(model):
-    cursor = connection.cursor()
-    try:
-        cursor.execute('TRUNCATE TABLE "{0}"'.format(model._meta.db_table))
-    except OperationalError:
-        cursor.execute('DELETE FROM "{0}"'.format(model._meta.db_table))        
 
 
 class VoteSerializer(serializers.ModelSerializer):
@@ -147,13 +138,15 @@ class DossierDetailSerializer(DossierSerializer):
     def create(self, validated_data):
         proposals_data = validated_data.pop('proposals')
         dossier, _ = models.Dossier.objects.get_or_create(**validated_data)
-
+        
+        for proposal in models.Proposal.objects.filter(dossier=dossier).all():
+            proposal.votes.all().delete()
+            proposal.delete()
+        
         self._create_proposals(proposals_data, dossier)
         return dossier
 
     def _create_proposals(self, proposals_data, dossier):
-        truncate_model(models.Proposal)
-        truncate_model(models.Vote)
         for proposal_data in proposals_data:
             votes_data = proposal_data.pop('votes')
             proposal_data['dossier'] = dossier
diff --git a/representatives_votes/utils.py b/representatives_votes/utils.py
index d45be86..d15d633 100644
--- a/representatives_votes/utils.py
+++ b/representatives_votes/utils.py
@@ -24,8 +24,10 @@ from representatives_votes.serializers import DossierDetailSerializer
 # Import a dossier
 def import_a_dossier(data):
     serializer = DossierDetailSerializer(data=data)
-    print(serializer.is_valid())
-    print(serializer.save())
+    if serializer.is_valid():
+        serializer.save()
+    else:
+        print(serializer.errors)
     
 def import_dossiers(data):
     return [import_a_dossier(d_data) for d_data in data]
-- 
GitLab