Commit f4edd89b authored by Arnaud Fabre's avatar Arnaud Fabre

improves the way import works (smart update dossiers)

parent 5280efb4
......@@ -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)
......@@ -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
......
......@@ -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]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment