예제 #1
0
파일: patch.py 프로젝트: twidi/nplusone
 def wrapped(*args, **kwargs):
     queryset = func(*args, **kwargs)
     ctx = copy.copy(context)
     ctx['args'] = context.get('args', args)
     ctx['kwargs'] = context.get('kwargs', kwargs)
     # In Django 1.7.x, some `get_queryset` methods return a `Manager`, not a
     # `QuerySet`; in this case, patch the `get_queryset` method of the returned
     # `Manager`.
     if hasattr(queryset, 'get_queryset'):  # pragma: no cover
         queryset.get_queryset = signalify_queryset(
             queryset.get_queryset,
             parser=parser,
             **ctx
         )
     else:
         queryset._clone = signalify_queryset(queryset._clone, parser=parser, **ctx)
         queryset.iterator = signals.signalify(
             signals.lazy_load,
             queryset.iterator,
             parser=parser,
             **ctx
         )
         queryset._context = ctx
     return queryset
예제 #2
0

query.prefetch_one_level = signals.designalify(
    signals.lazy_load,
    query.prefetch_one_level,
)


def parse_get(args, kwargs, context, ret):
    return [to_key(ret)] if isinstance(ret, Model) else []


# Ignore records loaded during `get`
query.QuerySet.get = signals.signalify(
    signals.ignore_load,
    query.QuerySet.get,
    parser=parse_get,
)

ReverseOneToOneDescriptor.get_queryset = signalify_queryset(
    ReverseOneToOneDescriptor.get_queryset,
    parser=parse_reverse_one_to_one_queryset,
)
ForwardManyToOneDescriptor.get_queryset = signalify_queryset(
    ForwardManyToOneDescriptor.get_queryset,
    parser=parse_forward_many_to_one_queryset,
)


def _create_forward_many_to_many_manager(*args, **kwargs):
    context = inspect.getcallargs(create_forward_many_to_many_manager, *args,
예제 #3
0
def parse_lazy_load(args, kwargs, context):
    loader, state, _ = args
    return state.object.__class__, to_key(state.object), loader.parent_property.key


def parse_attribute_get(args, kwargs, context):
    attr, instance = args[:2]
    if instance is None:
        return None
    return attr.class_, attr.key, [to_key(instance)]


strategies.LazyLoader._load_for_state = signals.signalify(
    signals.lazy_load,
    strategies.LazyLoader._load_for_state,
    parser=parse_lazy_load,
)


def parse_populate(args, kwargs, context):
    query_context = args[0]
    state = args[2]
    instance = state.object
    return instance.__class__, context['key'], [to_key(instance)], id(query_context)


# Emit `eager_load` on populating from `joinedload` or `subqueryload`
original_populate_full = loading._populate_full
def _populate_full(*args, **kwargs):
    ret = original_populate_full(*args, **kwargs)
예제 #4
0
파일: patch.py 프로젝트: twidi/nplusone
def parse_get_prefetcher(args, kwargs, context):
    instance, field = args
    return instance.__class__, field


def parse_select_related(args, kwargs, context):
    field = args[0]
    if isinstance(field, related.OneToOneField):
        return parse_field(field)
    return parse_reverse_field(field)


query.get_prefetcher = signals.signalify(
    signals.eager_load,
    query.get_prefetcher,
    parser=parse_get_prefetcher,
)


# Emit `eager_load` on verified `select_related_descend`
original_select_related_descend = query_utils.select_related_descend
def select_related_descend(*args, **kwargs):
    ret = original_select_related_descend(*args, **kwargs)
    if ret:
        signals.eager_load.send(
            get_worker(),
            args=args,
            kwargs=kwargs,
            parser=parse_select_related,
            context=None,
예제 #5
0
    return loader.parent.class_, to_key(state.object), loader.parent_property.key


def parse_eager_load(args, kwargs, context):
    loader = args[0]
    return loader.parent.class_, loader.key


def parse_attribute_get(args, kwargs, context):
    attr = args[0]
    return attr.class_, attr.key


strategies.LazyLoader._load_for_state = signals.signalify(
    signals.lazy_load,
    strategies.LazyLoader._load_for_state,
    parser=parse_lazy_load,
)


strategies.JoinedLoader._create_eager_join = signals.signalify(
    signals.eager_load,
    strategies.JoinedLoader._create_eager_join,
    parser=parse_eager_load,
)


strategies.SubqueryLoader._apply_joins = signals.signalify(
    signals.eager_load,
    strategies.SubqueryLoader._apply_joins,
    parser=parse_eager_load,
예제 #6
0
related.create_foreign_related_manager = create_foreign_related_manager


def parse_get_prefetcher(args, kwargs, context):
    instance, field = args
    return instance.__class__, field


def parse_select_related(args, kwargs, context):
    field = args[0]
    return field.related_field.model, field.rel.related_name


query.get_prefetcher = signals.signalify(
    signals.eager_load,
    query.get_prefetcher,
    parser=parse_get_prefetcher,
)


# Emit `eager_load` on verified `select_related_descend`
original_select_related_descend = query_utils.select_related_descend
def select_related_descend(*args, **kwargs):
    ret = original_select_related_descend(*args, **kwargs)
    if ret:
        signals.eager_load.send(
            get_worker(),
            args=args,
            kwargs=kwargs,
            parser=parse_select_related,
            context=None,
예제 #7
0
파일: patch.py 프로젝트: jmcarp/nplusone

query.prefetch_one_level = signals.designalify(
    signals.lazy_load,
    query.prefetch_one_level,
)


def parse_get(args, kwargs, context, ret):
    return [to_key(ret)] if isinstance(ret, Model) else []


# Ignore records loaded during `get`
query.QuerySet.get = signals.signalify(
    signals.ignore_load,
    query.QuerySet.get,
    parser=parse_get,
)


ReverseOneToOneDescriptor.get_queryset = signalify_queryset(
    ReverseOneToOneDescriptor.get_queryset,
    parser=parse_reverse_one_to_one_queryset,
)
ForwardManyToOneDescriptor.get_queryset = signalify_queryset(
    ForwardManyToOneDescriptor.get_queryset,
    parser=parse_forward_many_to_one_queryset,
)


def _create_forward_many_to_many_manager(*args, **kwargs):
def parse_lazy_load(args, kwargs, context):
    loader, state, _ = args
    return state.object.__class__, to_key(
        state.object), loader.parent_property.key


def parse_attribute_get(args, kwargs, context):
    attr, instance = args[:2]
    if instance is None:
        return None
    return attr.class_, attr.key, [to_key(instance)]


strategies.LazyLoader._load_for_state = signals.signalify(
    signals.lazy_load,
    strategies.LazyLoader._load_for_state,
    parser=parse_lazy_load,
)


def parse_populate(args, kwargs, context):
    query_context = args[0]
    state = args[2]
    instance = state.object
    return instance.__class__, context['key'], [to_key(instance)
                                                ], id(query_context)


# Emit `eager_load` on populating from `joinedload` or `subqueryload`
original_populate_full = loading._populate_full