Skip to content

Commit

Permalink
Introducing signals.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmuellegger committed Feb 28, 2010
1 parent 66d23b8 commit 3f72213
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def split(self, *args, **kw):

setup(
name = 'django-autofixture',
version = '0.1.0',
version = '0.2.0pre1',
url = 'https://launchpad.net/django-autofixture',
license = 'BSD',
description = 'Provides tools to auto generate test data.',
Expand Down
7 changes: 6 additions & 1 deletion src/django_autofixture/autofixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.db.models import fields
from django.db.models.fields import related
from django.utils.datastructures import SortedDict
from django_autofixture import constraints, generators
from django_autofixture import constraints, generators, signals


class CreateInstanceError(Exception):
Expand Down Expand Up @@ -290,6 +290,11 @@ def create_one(self, commit=True):
instance.save()
for field in instance._meta.many_to_many:
self.process_m2m(instance, field)
signals.instance_created.send(
sender=self,
model=self.model,
instance=instance,
commited=commit)
return instance

def create(self, count=1, commit=True):
Expand Down
33 changes: 19 additions & 14 deletions src/django_autofixture/management/commands/loadtestdata.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from django.db.transaction import commit_on_success
from django.core.management.base import BaseCommand, CommandError
from django_autofixture import AutoFixture
from django_autofixture import signals, AutoFixture
from optparse import make_option


Expand Down Expand Up @@ -37,6 +37,18 @@ class Command(BaseCommand):
u'0,0 which means that no related models are created.'),
)

def print_instance(self, sender, model, instance, **kwargs):
reprstr = unicode(instance)
if len(reprstr) > 50:
reprstr = u'%s ...' % reprstr[:50]
print '%s(pk=%s): %s' % (
'%s.%s' % (
model._meta.app_label,
model._meta.object_name),
unicode(instance.pk),
reprstr,
)

@commit_on_success
def handle(self, *attrs, **options):
from django.db.models import get_model
Expand Down Expand Up @@ -86,6 +98,10 @@ def handle(self, *attrs, **options):
u'Unknown model: %s.%s' % (app_label, model_label))
models.append((model, count))

if verbosity >= 1:
signals.instance_created.connect(
self.print_instance)

for model, count in models:
fill = AutoFixture(
model,
Expand All @@ -94,16 +110,5 @@ def handle(self, *attrs, **options):
generate_fk=generate_fk,
follow_m2m=follow_m2m,
generate_m2m=generate_m2m)
for i, obj in enumerate(fill.iter(count)):
if verbosity >= 1:
reprstr = unicode(obj)
if len(reprstr) > 50:
reprstr = u'%s ...' % reprstr[:50]
print('#%d %s(pk=%s): %s' % (
i+1,
'%s.%s' % (
model._meta.app_label,
model._meta.object_name),
unicode(obj.pk),
reprstr,
))
for obj in fill.iter(count):
pass
5 changes: 5 additions & 0 deletions src/django_autofixture/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
from django.dispatch import Signal


instance_created = Signal(providing_args=['model', 'instance', 'commited'])

0 comments on commit 3f72213

Please sign in to comment.