Commit ddb7aef1 by Daniel Friedman

Merge pull request #25 from edx/dan-f/add-bulk-create-command

Add management command to create notes in bulk
parents 3f831178 f048f5ed
import json
from optparse import make_option
import os
import random
import uuid
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
from notesapi.v1.models import Note
def extract_comma_separated_list(option, opt_str, value, parser):
"""Parse an option string as a comma separated list"""
setattr(parser.values, option.dest, [course_id.strip() for course_id in value.split(',')])
class Command(BaseCommand):
args = '<total_notes>'
option_list = BaseCommand.option_list + (
make_option(
'--per_user',
action='store',
type='int',
default=50,
help='number of notes that should be attributed to each user (default 50)'
),
make_option(
'--course_ids',
action='callback',
callback=extract_comma_separated_list,
type='string',
default=['edX/DemoX/Demo_Course'],
help='comma-separated list of course_ids for which notes should be randomly attributed'
),
)
help = 'Add N random notes to the database'
def handle(self, *args, **options):
if len(args) != 1:
raise CommandError("generate_random_notes takes the following arguments: " + self.args)
total_notes = int(args[0])
notes_per_user = options['per_user']
course_ids = options['course_ids']
Note.objects.bulk_create(note_iter(total_notes, notes_per_user, course_ids))
def note_iter(total_notes, notes_per_user, course_ids):
"""
Return an iterable of random notes data of length `total_notes`.
Arguments:
total_notes (int): total number of notes models to yield
notes_per_user (int): number of notes to attribute to any one user
course_ids (list): list of course_id strings to which notes will be
randomly attributed
Returns:
generator: An iterable of note models.
"""
DATA_DIRECTORY = os.path.join(os.path.dirname(__file__), "data/")
with open(os.path.join(DATA_DIRECTORY, 'basic_words.txt')) as f:
notes_text = [word for line in f for word in line.split()]
def weighted_get_words(weighted_num_words):
"""
Return random words of of a length of weighted probability.
`weighted_num_words` should look like [(word_count, weight), (word_count, weight) ...]
"""
return random.sample(
notes_text,
random.choice([word_count for word_count, weight in weighted_num_words for i in range(weight)])
)
get_new_user_id = lambda: uuid.uuid4().hex
user_id = get_new_user_id()
for note_count in range(total_notes):
if note_count % notes_per_user == 0:
user_id = get_new_user_id()
# Notice that quote and ranges are arbitrary
yield Note(
user_id=user_id,
course_id=random.choice(course_ids),
usage_id=uuid.uuid4().hex,
quote='foo bar baz',
text=' '.join(weighted_get_words([(10, 5), (25, 3), (100, 2)])),
ranges=json.dumps([{"start": "/div[1]/p[1]", "end": "/div[1]/p[1]", "startOffset": 0, "endOffset": 6}]),
tags=json.dumps(weighted_get_words([(1, 40), (2, 30), (5, 15), (10, 10), (15, 5)]))
)
a about above across act active activity add afraid after again age ago agree air all alone along already always am amount an and angry another answer any anyone anything anytime appear apple are area arm army around arrive art as ask at attack aunt autumn away
baby back bad bag ball bank base basket bath be bean bear beautiful bed bedroom beer behave before begin behind bell below besides best better between big bird birth birthday bit bite black bleed block blood blow blue board boat body boil bone book border born borrow both bottle bottom bowl box boy branch brave bread break breakfast breathe bridge bright bring brother brown brush build burn business bus busy but buy by
cake call can candle cap car card care careful careless carry case cat catch central century certain chair chance change chase cheap cheese chicken child children chocolate choice choose circle city class clever clean clear climb clock cloth clothes cloud cloudy close coffee coat coin cold collect colour comb comfortable common compare come complete computer condition continue control cook cool copper corn corner correct cost contain count country course cover crash cross cry cup cupboard cut
dance dangerous dark daughter day dead decide decrease deep deer depend desk destroy develop die different difficult dinner direction dirty discover dish do dog door double down draw dream dress drink drive drop dry duck dust duty
each ear early earn earth east easy eat education effect egg eight either electric elephant else empty end enemy enjoy enough enter equal entrance escape even evening event ever every everyone exact everybody examination example except excited exercise expect expensive explain extremely eye
face fact fail fall false family famous far farm father fast fat fault fear feed feel female fever few fight fill film find fine finger finish fire first fish fit five fix flag flat float floor flour flower fly fold food fool foot football for force foreign forest forget forgive fork form fox four free freedom freeze fresh friend friendly from front fruit full fun funny furniture further future
game garden gate general gentleman get gift give glad glass go goat god gold good goodbye grandfather grandmother grass grave great green grey ground group grow gun
hair half hall hammer hand happen happy hard hat hate have he head healthy hear heavy heart heaven height hello help hen her here hers hide high hill him his hit hobby hold hole holiday home hope horse hospital hot hotel house how hundred hungry hour hurry husband hurt
I ice idea if important in increase inside into introduce invent iron invite is island it its
jelly job join juice jump just
keep key kill kind king kitchen knee knife knock know
ladder lady lamp land large last late lately laugh lazy lead leaf learn leave leg left lend length less lesson let letter library lie life light like lion lip list listen little live lock lonely long look lose lot love low lower luck
machine main make male man many map mark market marry matter may me meal mean measure meat medicine meet member mention method middle milk million mind minute miss mistake mix model modern moment money monkey month moon more morning most mother mountain mouth move much music must my
name narrow nation nature near nearly neck need needle neighbour neither net never new news newspaper next nice night nine no noble noise none nor north nose not nothing notice now number
obey object ocean of off offer office often oil old on one only open opposite or orange order other our out outside over own
page pain paint pair pan paper parent park part partner party pass past path pay peace pen pencil people pepper per perfect period person petrol photograph piano pick picture piece pig pin pink place plane plant plastic plate play please pleased plenty pocket point poison police polite pool poor popular position possible potato pour power present press pretty prevent price prince prison private prize probably problem produce promise proper protect provide public pull punish pupil push put
queen question quick quiet quite
radio rain rainy raise reach read ready real really receive record red remember remind remove rent repair repeat reply report rest restaurant result return rice rich ride right ring rise road rob rock room round rubber rude rule ruler run rush
sad safe sail salt same sand save say school science scissors search seat second see seem sell send sentence serve seven several shade shadow shake shape share sharp she sheep sheet shelf shine ship shirt shoe shoot shop short should shoulder shout show sick side signal silence silly silver similar simple single since sing sink sister sit six size skill skin skirt sky sleep slip slow small smell smile smoke snow so soap sock soft some someone something sometimes son soon sorry sound soup south space speak special speed spell spend spoon sport spread spring square stamp stand star start station stay steal steam step still stomach stone stop store storm story strange street strong structure student study stupid subject substance successful such sudden sugar suitable summer sun sunny support sure surprise sweet swim sword
table take talk tall taste taxi tea teach team tear telephone television tell ten tennis terrible test than that the their then there therefore these thick thin thing think third this though threat three tidy tie title to today toe together tomorrow tonight too tool tooth top total touch town train tram travel tree trouble true trust twice try turn type
ugly uncle under understand unit until up use useful usual usually
vegetable very village voice visit
wait wake walk want warm was wash waste watch water way we weak wear weather wedding week weight welcome were well west wet what wheel when where which while white who why wide wife wild will win wind window wine winter wire wise wish with without woman wonder word work world worry
yard yell yesterday yet you young your
zero zoo
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment