Skip to content

Instantly share code, notes, and snippets.

@jochenklar
Created April 9, 2023 07:14
Show Gist options
  • Save jochenklar/0bea84345741a976570e23eed1a33559 to your computer and use it in GitHub Desktop.
Save jochenklar/0bea84345741a976570e23eed1a33559 to your computer and use it in GitHub Desktop.
This script uses faker to create a set of catalogs with a lot of questions.
'''
# 10000 questions for RDMO
This script uses faker to create a set of catalogs with a lot of questions.
## Setup
```
cd rdmo-app
mkdir scripts
touch scripts/__init__.py
pip install faker tqdm
```
place this script in scripts/1000_questions.py
## Usage
```
./manage.py runscript 10000_questions
```
'''
from faker import Faker
from tqdm import tqdm
from rdmo.domain.models import Attribute
from rdmo.questions.models import Catalog, Section, Page, Question
def run():
fake = Faker()
Attribute.objects.all().delete()
for i in tqdm(range(1, 10001), desc='attributes'):
Attribute.objects.create(
key=f'attribute{i:05}'
)
Question.objects.all().delete()
for i in tqdm(range(1, 10001), desc='questions'):
Question.objects.create(
uri_path=f'questions/{i:05}',
text_lang1=fake.sentence().replace('.', '?'),
help_lang1=fake.paragraph(nb_sentences=3),
widget_type='text',
value_type='text',
attribute=Attribute.objects.get(key=f'attribute{i:05}')
)
Page.objects.all().delete()
for i in tqdm(range(1, 1001), desc='pages'):
page = Page.objects.create(
uri_path=f'pages/{i:05}',
title_lang1=fake.text(max_nb_chars=32).replace('.', ''),
help_lang1=fake.paragraph(nb_sentences=3)
)
page.questions.set(Question.objects.filter(uri_path__in=[
f'questions/{j:05}' for j in range(i * 10, i * 10 + 11)
]))
Section.objects.all().delete()
for i in tqdm(range(1, 101), desc='sections'):
section = Section.objects.create(
uri_path=f'sections/{i:05}',
title_lang1=fake.text(max_nb_chars=32).replace('.', '')
)
section.pages.set(Page.objects.filter(uri_path__in=[
f'pages/{j:05}' for j in range(i * 10, i * 10 + 11)
]))
Catalog.objects.all().delete()
for i in tqdm(range(1, 11), desc='catalogs'):
catalog = Catalog.objects.create(
uri_path=f'catalogs/{i:05}',
title_lang1=fake.text(max_nb_chars=32).replace('.', '')
)
catalog.sections.set(Section.objects.filter(uri_path__in=[
f'sections/{j:05}' for j in range(i * 10, i * 10 + 11)
]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment