Skip to content

Commit

Permalink
add initial data migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
maelys-buhler committed Mar 6, 2024
1 parent 5581d01 commit dc3fbbf
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/masteriq/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand Down Expand Up @@ -123,3 +124,6 @@
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


INIT_DATA_FOLDER = os.path.join('data', 'csv')
45 changes: 45 additions & 0 deletions api/masteriqapp/migrations/0002_auto_20240303_1147.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Generated by Django 5.0.2 on 2024-03-03 10:47

from django.db import migrations
from django.conf import settings
import pandas as pd
import os
import logging


def load_initial_data(apps, schema_editor):
print("\nStart of initial data migration, this may take some time...")
question_model = apps.get_model('masteriqapp', 'Question')
option_model = apps.get_model('masteriqapp', 'Option')
category_model = apps.get_model('masteriqapp', 'Category')

directory = settings.INIT_DATA_FOLDER
logging.info(directory)
for filename in os.listdir(directory):
f = os.path.join(directory, filename)
if os.path.isfile(f) and filename.endswith(".csv"):
df = pd.read_csv(f)
category_name = filename[:-3].replace('-', ' ').title()
category = category_model.objects.create(name=category_name)
for row in df.index:
if pd.isnull(df['A'][row]) or pd.isnull(df['B'][row]):
continue
question = question_model.objects.create(text=df['Questions'][row], category=category)
right_option_text = df['Correct'][row]
option_model.objects.create(text=right_option_text, is_correct=True, question=question)

for letter in ['A', 'B', 'C', 'D']:
if pd.isnull(df[letter][row]) or df[letter][row] == right_option_text:
continue
option_model.objects.create(text=df[letter][row], is_correct=False, question=question)
print("Initial data loaded!")


class Migration(migrations.Migration):
dependencies = [
('masteriqapp', '0001_initial'),
]

operations = [
migrations.RunPython(load_initial_data)
]
20 changes: 20 additions & 0 deletions api/masteriqapp/migrations/0003_alter_category_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 5.0.2 on 2024-03-05 17:52

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('masteriqapp', '0002_auto_20240303_1147'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AlterField(
model_name='category',
name='users',
field=models.ManyToManyField(through='masteriqapp.IQ', to=settings.AUTH_USER_MODEL),
),
]

0 comments on commit dc3fbbf

Please sign in to comment.