From d31a21c68b1d08c100862ff4f91a114771bfd06b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Herv=C3=A9=20=20MENAGER?= <herve.menager@pasteur.fr>
Date: Thu, 16 Mar 2017 22:34:31 +0100
Subject: [PATCH] migrate proteins and choose migrated models with arguments 
 # Please enter the commit message for your changes. Lines starting

---
 .../management/commands/import_v1_data.py     | 67 ++++++++++++++-----
 1 file changed, 50 insertions(+), 17 deletions(-)

diff --git a/ippisite/ippidb/management/commands/import_v1_data.py b/ippisite/ippidb/management/commands/import_v1_data.py
index c49cf75e..22a073ef 100644
--- a/ippisite/ippidb/management/commands/import_v1_data.py
+++ b/ippisite/ippidb/management/commands/import_v1_data.py
@@ -1,27 +1,60 @@
 from django.core.management import BaseCommand
 import mysql.connector
-from ippidb.models import Bibliography
+from ippidb.models import Bibliography, Protein
 
 class Command(BaseCommand):
+
     help = "Import data from the local v1 database"
 
+    def add_arguments(self, parser):
+        parser.add_argument(
+            '--bibliographies',
+            action='store_true',
+            dest='bibliographies',
+            default=False,
+            help='Flush and migrate bibliographies',
+        )
+        parser.add_argument(
+            '--proteins',
+            action='store_true',
+            dest='proteins',
+            default=False,
+            help='Flush and migrate proteins',
+        )
+
     def handle(self, *args, **options):
         conn = mysql.connector.connect(host="localhost",user="root",password="ippidb", database="ippidb")
         cursor = conn.cursor()
-        cursor.execute("""SELECT * FROM biblio""")
-        rows = cursor.fetchall()
-        Bibliography.objects.all().delete()
-        self.stdout.write(self.style.SUCCESS('Successfully flushed bibliography table'))
-        for row in rows:
-            try:
-                b = Bibliography()
-                if row[1]=='article':
-                    b.source = 'PM'
+        if options['bibliographies']:
+            cursor.execute("""SELECT * FROM biblio""")
+            rows = cursor.fetchall()
+            Bibliography.objects.all().delete()
+            self.stdout.write(self.style.SUCCESS('Successfully flushed bibliography table'))
+            for row in rows:
+                try:
+                    b = Bibliography()
+                    if row[1]=='article':
+                        b.source = 'PM'
+                    else:
+                        b.source = 'PT'
+                    b.id_source = row[2]
+                    b.save()
+                except Exception as e:
+                    self.stdout.write(self.style.ERROR('Failed inserting {}'.format(row[2])))
+                else:
+                    self.stdout.write(self.style.SUCCESS('Successfully inserted {}'.format(row[2])))
+        if options['proteins']:
+            cursor.execute("""SELECT * FROM protein""")
+            rows = cursor.fetchall()
+            Protein.objects.all().delete()
+            self.stdout.write(self.style.SUCCESS('Successfully flushed protein table'))
+            for row in rows:
+                try:
+                    p = Protein()
+                    p.uniprot_id = row[1]
+                    p.save()
+                except Exception as e:
+                    self.stdout.write(self.style.ERROR('Failed inserting {} {}'.format(row[1], row[2])))
                 else:
-                    b.source = 'PT'
-                b.id_source = row[2]
-                b.save()
-            except Exception as e:
-                self.stdout.write(self.style.ERROR('Failed inserting {}'.format(row[2])))
-            else:
-                self.stdout.write(self.style.SUCCESS('Successfully inserted {}'.format(row[2])))
+                    self.stdout.write(self.style.SUCCESS('Successfully inserted {} {}'.format(row[1], row[2])))
+            
-- 
GitLab