Skip to content

Commit

Permalink
Adding some extra documentation and the SAMPLEDATAHELPER_SEED setting…
Browse files Browse the repository at this point in the history
…s variable
  • Loading branch information
Jesús Espino committed Jul 28, 2013
1 parent 1750ba2 commit 5e0ea74
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 17 deletions.
35 changes: 18 additions & 17 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,34 @@ Example

Sample data command for generate instances of MyModel::

from django.core.management.base import BaseCommand, CommandError
from django.core.management.base import BaseCommand
from myapp.models import MyModel
from sampledatahelper.helper import SampleDataHelper
class Command(BaseCommand):
args = ''
help = 'Example data generator'
sd = SampleDataHelper(seed=12345678901)
def handle(self, *args, **options):
sd = SampleDataHelper(seed=12345678901)
INSTANCES = 5
for x in range(INSTANCES):
def generate_mymodel_data(self, instances):
for x in range(instances):
instance = MyModel.objects.create(
slug=sd.slug(2, 3),
name=sd.name(2, 3)
claim=sd.sentence(),
description=sd.paragraph(),
email=sd.email(),
photo=sd.image(64, 64),
is_active=sd.boolean(),
birth_date=sd.past_date(),
expected_death_date=sd.future_date(),
my_related_object=sd.db_object(MyRelatedModel)
slug=self.sd.slug(2, 3),
name=self.sd.name(2, 3)
claim=self.sd.sentence(),
description=self.sd.paragraph(),
email=self.sd.email(),
photo=self.sd.image(64, 64),
is_active=self.sd.boolean(),
birth_date=self.sd.past_date(),
expected_death_date=self.sd.future_date(),
my_related_object=self.sd.db_object(MyRelatedModel)
)

def handle(self, *args, **options):
print "Generating MyModel data"
self.generate_mymodel_data(5)

Documentation
-------------

Expand Down
11 changes: 11 additions & 0 deletions doc/source/install_and_configure.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
Install and configure
=====================

Install using pip, including any pillow if you want image genetion...::

pip install django-sampledatahelper
pip install pillow # For image generation


You can configure, if you want a SAMPLEDATAHELPER_SEED variable in your
settings, to generate alwais the same data. Example::

SAMPLEDATAHELPER_SEED = 123456789
43 changes: 43 additions & 0 deletions doc/source/quick_start.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,45 @@
Quick start
===========

Follow the install and configure instructions.

If you have some aplications to populate, you can split your sample data
generation on one command per app, or add only one command in one app thats
generate everything. Anyway you have to build a command files.

The file must be in `<app-module>/management/commands/<command-name>.py` can be
something like `myapp/management/commands/mysampledata.py`.

And finally the command file content can be like this::

from django.core.management.base import BaseCommand
from myapp.models import MyModel
from sampledatahelper.helper import SampleDataHelper
class Command(BaseCommand):
args = ''
help = 'Example data generator'
sd = SampleDataHelper(seed=12345678901)
def generate_mymodel_data(self, instances):
for x in range(instances):
instance = MyModel.objects.create(
slug=self.sd.slug(2, 3),
name=self.sd.name(2, 3)
claim=self.sd.sentence(),
description=self.sd.paragraph(),
email=self.sd.email(),
photo=self.sd.image(64, 64),
is_active=self.sd.boolean(),
birth_date=self.sd.past_date(),
expected_death_date=self.sd.future_date(),
my_related_object=self.sd.db_object(MyRelatedModel)
)

def handle(self, *args, **options):
print "Generating MyModel data"
self.generate_mymodel_data(5)

To generate your sampledata, simply run the created command, for example::

python manage.py mysampledata
6 changes: 6 additions & 0 deletions sampledatahelper/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
# -*- coding: utf-8 -*-

import random

from django.conf import settings

from .mixins import NumberMixin, TextMixin, TimeMixin, LocalizedMixin, ImageMixin, OtherMixin


class SampleDataHelper(NumberMixin, TextMixin, TimeMixin, LocalizedMixin, ImageMixin, OtherMixin):
def __init__(self, seed=None):
if seed is None:
seed = getattr(settings, 'SAMPLEDATAHELPER_SEED', None)

if seed is not None:
random.seed(seed)

0 comments on commit 5e0ea74

Please sign in to comment.