Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Introducing signals.

  • Loading branch information...
commit 3f722135cbd4e083196359f3c685abf11eb298db 1 parent 66d23b8
Gregor Müllegger gregmuellegger authored
2  setup.py
View
@@ -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.',
7 src/django_autofixture/autofixture.py
View
@@ -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):
@@ -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):
33 src/django_autofixture/management/commands/loadtestdata.py
View
@@ -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
@@ -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
@@ -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,
@@ -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 src/django_autofixture/signals.py
View
@@ -0,0 +1,5 @@
+# -*- coding: utf-8 -*-
+from django.dispatch import Signal
+
+
+instance_created = Signal(providing_args=['model', 'instance', 'commited'])
Please sign in to comment.
Something went wrong with that request. Please try again.