From 8476b9d76dfbde0106530bfb3bf34a35d09257d3 Mon Sep 17 00:00:00 2001
From: maciej <maciej@mandarynki.eu>
Date: Mon, 7 Apr 2025 09:19:47 +0200
Subject: [PATCH] Add db/main.py

---
 db/main.py | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)
 create mode 100644 db/main.py

diff --git a/db/main.py b/db/main.py
new file mode 100644
index 0000000..e3e08cb
--- /dev/null
+++ b/db/main.py
@@ -0,0 +1,115 @@
+import csv
+from tqdm import tqdm
+import psycopg2
+
+conn = psycopg2.connect('host=10.16.32.107 user=maciej password=maciej dbname=egzamin')
+cur = conn.cursor()
+
+cur.execute('TRUNCATE TABLE tasks;')
+cur.execute('TRUNCATE TABLE questions;')
+cur.execute('TRUNCATE TABLE categories;')
+cur.execute('TRUNCATE TABLE tasks_advanced;')
+cur.execute('TRUNCATE TABLE questions_advanced;')
+
+with open('pytania.csv') as file:
+    reader = csv.reader(file, delimiter=';')
+    for row in tqdm(reader):
+        numer = row[0]
+        pytanie_pl = row[1]
+        odp_a_pl = row[2]
+        odp_b_pl = row[3]
+        odp_c_pl = row[4]
+        poprawna_odp = row[5]
+        media = row[6]
+        kategorie = row[7]
+        pjm_pytanie = row[8]
+        pjm_odp_a = row[9]
+        pjm_odp_b = row[10]
+        pjm_odp_c = row[11]
+        pytanie_en = row[12]
+        odp_a_en = row[13]
+        odp_b_en = row[14]
+        odp_c_en = row[15]
+        pytanie_de = row[16]
+        odp_a_de = row[17]
+        odp_b_de = row[18]
+        odp_c_de = row[19]
+        pytanie_ua = row[20]
+        odp_a_ua = row[21]
+        odp_b_ua = row[22]
+        odp_c_ua = row[23]
+
+        categories = kategorie.split(',')
+        task_id = int(numer)
+
+        if numer and pytanie_pl and poprawna_odp and kategorie and not odp_a_pl and not odp_b_pl and not odp_c_pl:
+            if poprawna_odp == 'Tak':
+                correct_answer = True
+            elif poprawna_odp == 'Nie':
+                correct_answer = False
+            else:
+                raise Exception('zjebało się')
+
+            cur.execute('INSERT INTO tasks (id, correct_answer, media_url) VALUES (%s, %s, %s)',
+                        (task_id, correct_answer, media))
+
+            cur.execute('INSERT INTO questions(task_id, lang, text) VALUES (%s, %s, %s)',
+                        (task_id, 'PL', pytanie_pl))
+
+            if pytanie_en:
+                cur.execute('INSERT INTO questions(task_id, lang, text) VALUES (%s, %s, %s)',
+                            (task_id, 'EN', pytanie_en))
+            if pytanie_de:
+                cur.execute('INSERT INTO questions(task_id, lang, text) VALUES (%s, %s, %s)',
+                            (task_id, 'DE', pytanie_de))
+            if pytanie_ua:
+                cur.execute('INSERT INTO questions(task_id, lang, text) VALUES (%s, %s, %s)',
+                            (task_id, 'UA', pytanie_ua))
+
+        if numer and pytanie_pl and poprawna_odp and kategorie and odp_a_pl and odp_b_pl and odp_c_pl:
+            cur.execute('INSERT INTO tasks_advanced(id, correct_answer, media_url) VALUES (%s, %s, %s)',
+                        (task_id, poprawna_odp, media))
+
+            cur.execute('INSERT INTO questions_advanced(task_id, lang, text, answer_a, answer_b, answer_c) VALUES (%s, %s, %s, %s, %s, %s)',
+                        (task_id, 'PL', pytanie_pl, odp_a_pl, odp_b_pl, odp_c_pl))
+
+            if odp_a_en and odp_b_en and odp_c_en:
+                cur.execute('INSERT INTO questions_advanced(task_id, lang, text, answer_a, answer_b, answer_c) VALUES (%s, %s, %s, %s, %s, %s)',
+                    (task_id, 'EN', pytanie_en, odp_a_en, odp_b_en, odp_c_en))
+
+            if odp_a_de and odp_b_de and odp_c_de:
+                cur.execute('INSERT INTO questions_advanced(task_id, lang, text, answer_a, answer_b, answer_c) VALUES (%s, %s, %s, %s, %s, %s)',
+                    (task_id, 'DE', pytanie_de, odp_a_de, odp_b_de, odp_c_de))
+
+            if odp_a_ua and odp_b_ua and odp_c_ua:
+                cur.execute('INSERT INTO questions_advanced(task_id, lang, text, answer_a, answer_b, answer_c) VALUES (%s, %s, %s, %s, %s, %s)',
+                    (task_id, 'UA', pytanie_ua, odp_a_ua, odp_b_ua, odp_c_ua))
+
+        for category in categories:
+            cur.execute('INSERT INTO categories(name, task_id) VALUES (%s, %s)',
+                        (category, task_id))
+
+
+with open('punkty.csv') as file:
+    reader = csv.reader(file, delimiter=';')
+    for row in tqdm(reader):
+        numer = int(row[0])
+        punkty = int(row[1])
+        cur.execute('UPDATE tasks SET weight=%s WHERE id=%s', (punkty, numer))
+        cur.execute('UPDATE tasks_advanced SET weight=%s WHERE id=%s', (punkty, numer))
+
+
+cur.execute('SELECT COUNT(id) FROM tasks')
+print(f'Imported {cur.fetchone()[0]} tasks')
+cur.execute('SELECT COUNT(*) FROM questions')
+print(f'Imported {cur.fetchone()[0]} questions')
+cur.execute('SELECT COUNT(id) FROM tasks_advanced')
+print(f'Imported {cur.fetchone()[0]} tasks_advanced')
+cur.execute('SELECT COUNT(*) FROM questions_advanced')
+print(f'Imported {cur.fetchone()[0]} questions_advanced')
+cur.execute('SELECT COUNT(*) FROM categories')
+print(f'Imported {cur.fetchone()[0]} categories')
+
+conn.commit()
+cur.close()
+conn.close()