From 39e631d46f500576a823ee8073fee98c6d4381f1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Herv=C3=A9=20=20MENAGER?= <herve.menager@pasteur.fr>
Date: Sun, 12 Mar 2017 22:51:30 +0100
Subject: [PATCH] add a custom django command import_v1_data

this is only a draft, still with much hardcoded ugliness...
---
 ippisite/ippidb/management/__init__.py        |  0
 .../ippidb/management/commands/__init__.py    |  0
 .../management/commands/import_v1_data.py     | 24 +++++++++++++++++++
 3 files changed, 24 insertions(+)
 create mode 100644 ippisite/ippidb/management/__init__.py
 create mode 100644 ippisite/ippidb/management/commands/__init__.py
 create mode 100644 ippisite/ippidb/management/commands/import_v1_data.py

diff --git a/ippisite/ippidb/management/__init__.py b/ippisite/ippidb/management/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/ippisite/ippidb/management/commands/__init__.py b/ippisite/ippidb/management/commands/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/ippisite/ippidb/management/commands/import_v1_data.py b/ippisite/ippidb/management/commands/import_v1_data.py
new file mode 100644
index 00000000..f6b60f8e
--- /dev/null
+++ b/ippisite/ippidb/management/commands/import_v1_data.py
@@ -0,0 +1,24 @@
+from django.core.management import BaseCommand
+import mysql.connector
+from ippidb.models import Bibliography
+
+class Command(BaseCommand):
+    help = "Import data from the local v1 database"
+
+    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()
+                b.source = 'PM'
+                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])))
-- 
GitLab