-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5581d01
commit dc3fbbf
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
), | ||
] |