def user_factory(skip_initial_collection=True, **kwargs): # note: collections.signals creates a collection on post-save user, this factory skips that signal by default if 'username' not in kwargs: kwargs['username'] = nextname('User') if skip_initial_collection: post_save.disconnect(create_initial_collection, sender=UserProfile) user = UserProfile.objects.create(**kwargs) if skip_initial_collection: post_save.connect(create_initial_collection, sender=UserProfile) return user
def collection_factory(initial_items=3, **kwargs): if 'owner' not in kwargs: if UserProfile.objects.count() == 0: kwargs['owner'] = user_factory() else: kwargs['owner'] = UserProfile.objects.order_by('?')[0] if 'title' not in kwargs: kwargs['title'] = nextname('Collection') if 'description' not in kwargs: kwargs['description'] = lorem collection = G(Collection, **kwargs) if 'items' not in kwargs: if Item.objects.count() < initial_items: for _ in range(initial_items - Item.objects.count()): item_args = subargs(kwargs, 'item') item_factory(**item_args) if initial_items: for item in Item.objects.order_by('?')[:initial_items]: CollectedItem.objects.create(item=item, collection=collection) return collection