From 66cbcf4cc453461103f291d275f4cff65d9769cf Mon Sep 17 00:00:00 2001
From: luxcem <af@laquadrature.net>
Date: Tue, 2 Jun 2015 16:31:10 +0200
Subject: [PATCH] progress bar

---
 .../import_representatives_from_compotista.py | 30 ++++++++++++++-----
 1 file changed, 23 insertions(+), 7 deletions(-)

diff --git a/representatives/management/commands/import_representatives_from_compotista.py b/representatives/management/commands/import_representatives_from_compotista.py
index 6de1eada..ca71ef4a 100644
--- a/representatives/management/commands/import_representatives_from_compotista.py
+++ b/representatives/management/commands/import_representatives_from_compotista.py
@@ -19,24 +19,40 @@
 # Copyright (C) 2013 Laurent Peuch <cortex@worlddomination.be>
 # Copyright (C) 2015 Arnaud Fabre <af@laquadrature.net>
 
+import json
+import ijson
+import pyprind
+
 from django.core.management.base import BaseCommand
 from django.conf import settings
 
 from urllib2 import urlopen
-import ijson
 
 from representatives.models import Representative
 from representatives.utils import import_a_representative
 
 class Command(BaseCommand):
     def handle(self, *args, **options):
-        compotista_server = getattr(settings,
+
+        Representative.objects.all().delete()
+
+        self.compotista_server = getattr(settings,
                                     'COMPOTISTA_SERVER',
                                     'http://compotista.mm.staz.be')
-
-        url = compotista_server + '/export/latest/'
+        
+        url = self.compotista_server + '/export/latest/'
         print('Import representatives from %s' % url)
+        
+        bar = pyprind.ProgBar(self.get_number_of_meps())
         resource = urlopen(url)
-        Representative.objects.all().delete()
-        for representative in ijson.items(resource, 'item'):
-            import_a_representative(representative)
+        for i, representative in enumerate(ijson.items(resource, 'item')):
+            representative = import_a_representative(representative)
+            representative_id = '{} - {}'.format(i, representative.full_name.encode('utf-8'))
+            bar.update(item_id = representative_id)
+
+        print(bar)            
+
+
+    def get_number_of_meps(self):
+        response = urlopen(self.compotista_server + '/api/representatives/')
+        return int(json.load(response).get('count'))
-- 
GitLab