Exemplo n.º 1
0
def _memoize_reset(reference, instance, **kwargs):
    "Resets the specified instance relative to ``reference``"
    # popped so it does not get included in the config for the signal
    memo = kwargs.pop('memo', None)

    # for every call, keep track of the reference and the object (fork).
    # this is used for recursive calls to related objects. this ensures
    # relationships that follow back up the tree are caught and are merely
    # referenced rather than traversed again.
    if memo is None:
        memo = utils.Memo()
    elif memo.has(reference):
        return memo.get(reference)

    if not isinstance(instance, reference.__class__):
        raise TypeError('The instance supplied must be of the same type as the reference')

    instance._commits = utils.Commits(reference)
    memo.add(reference, instance)

    # default configuration
    config = {
        'fields': None,
        'exclude': ['pk'],
        'deep': False,
        'commit': True,
    }

    # update with user-defined
    config.update(kwargs)

    # pre-signal
    signals.pre_reset.send(sender=reference.__class__, reference=reference,
        instance=instance, config=config, **kwargs)

    fields = config['fields']
    exclude = config['exclude']
    deep = config['deep']
    commit = config['commit']

    # no fields are defined, so get the default ones for shallow or deep
    if not fields:
        fields = utils._default_model_fields(reference, exclude=exclude, deep=deep)

    kwargs.update({'deep': deep})

    # iterate over each field and fork it!. nested calls will not commit,
    # until the recursion has finished
    for accessor in fields:
        _reset_field(reference, instance, accessor, **kwargs)

    # post-signal
    signals.post_reset.send(sender=reference.__class__, reference=reference,
        instance=instance, **kwargs)

    if commit:
        commit_model_object(instance)

    return instance
Exemplo n.º 2
0
def _memoize_fork(reference, **kwargs):
    "Resets the specified instance relative to ``reference``"
    # popped so it does not get included in the config for the signal
    memo = kwargs.pop('memo', None)

    # for every call, keep track of the reference and the instance being
    # acted on. this is used for recursive calls to related objects. this
    # ensures relationships that follow back up the tree are caught and are
    # merely referenced rather than traversed again.
    if memo is None:
        memo = utils.Memo()
    elif memo.has(reference):
        return memo.get(reference)

    # initialize and memoize new instance
    instance = reference.__class__()
    instance._commits = utils.Commits(reference)    
    memo.add(reference, instance)

    # default configuration
    config = {
        'fields': None,
        'exclude': ['pk'],
        'deep': False,
        'commit': True,
    }

    # pop off and set any config params for signals
    for key in config.iterkeys():
        if kwargs.has_key(key):
            config[key] = kwargs.pop(key)

    # pre-signal
    signals.pre_fork.send(sender=reference.__class__, reference=reference,
        instance=instance, config=config, **kwargs)

    fields = config['fields']
    exclude = config['exclude']
    deep = config['deep']
    commit = config['commit']

    # no fields are defined, so get the default ones for shallow or deep
    if not fields:
        fields = utils._default_model_fields(reference, exclude=exclude, deep=deep)

    # add arguments for downstream use
    kwargs.update({'deep': deep})

    # iterate over each field and fork it!. nested calls will not commit,
    # until the recursion has finished
    for accessor in fields:
        _fork_field(reference, instance, accessor, memo=memo, **kwargs)

    # post-signal
    signals.post_fork.send(sender=reference.__class__, reference=reference,
        instance=instance, **kwargs)

    # as of now, this will only every be from a top-level call
    if commit:
        commit_model_object(instance, **kwargs)

    return instance
Exemplo n.º 3
0
def _memoize_fork(reference, **kwargs):
    "Resets the specified instance relative to ``reference``"
    # popped so it does not get included in the config for the signal
    memo = kwargs.pop('memo', None)

    # for every call, keep track of the reference and the instance being
    # acted on. this is used for recursive calls to related objects. this
    # ensures relationships that follow back up the tree are caught and are
    # merely referenced rather than traversed again.
    if memo is None:
        memo = utils.Memo()
    elif memo.has(reference):
        return memo.get(reference)

    # initialize and memoize new instance
    instance = reference.__class__()
    instance._commits = utils.Commits(reference)
    memo.add(reference, instance)

    # default configuration
    config = {
        'fields': None,
        'exclude': ['pk'],
        'deep': False,
        'commit': True,
    }

    # pop off and set any config params for signals
    for key in config.iterkeys():
        if kwargs.has_key(key):
            config[key] = kwargs.pop(key)

    # pre-signal
    signals.pre_fork.send(sender=reference.__class__,
                          reference=reference,
                          instance=instance,
                          config=config,
                          **kwargs)

    fields = config['fields']
    exclude = config['exclude']
    deep = config['deep']
    commit = config['commit']

    # no fields are defined, so get the default ones for shallow or deep
    if not fields:
        fields = utils._default_model_fields(reference,
                                             exclude=exclude,
                                             deep=deep)

    # add arguments for downstream use
    kwargs.update({'deep': deep})

    # iterate over each field and fork it!. nested calls will not commit,
    # until the recursion has finished
    for accessor in fields:
        _fork_field(reference, instance, accessor, memo=memo, **kwargs)

    # post-signal
    signals.post_fork.send(sender=reference.__class__,
                           reference=reference,
                           instance=instance,
                           **kwargs)

    # as of now, this will only every be from a top-level call
    if commit:
        commit_model_object(instance, **kwargs)

    return instance