def filterchain(
    request,
    app,
    model,
    field,
    foreign_key_app_name,
    foreign_key_model_name,
    foreign_key_field_name,
    value,
    child_app=None,
    child_model=None,
    child_field=None,
    manager=None,
):

    model_class = get_model(app, model)
    m2m = is_m2m(model_class, field)
    keywords = get_keywords(field, value, m2m=m2m)

    # SECURITY: Make sure all smart selects requests are opt-in
    foreign_model_class = get_model(foreign_key_app_name,
                                    foreign_key_model_name)
    if not any([(isinstance(f, ChainedManyToManyField)
                 or isinstance(f, ChainedForeignKey))
                for f in foreign_model_class._meta.get_fields()]):
        raise PermissionDenied("Smart select disallowed")

    # filter queryset using limit_choices_to
    limit_choices_to = get_limit_choices_to(foreign_key_app_name,
                                            foreign_key_model_name,
                                            foreign_key_field_name)

    queryset = get_queryset(model_class, manager, limit_choices_to)

    # filter by a foreign key within the relationship (only ForeignKey not M2M)
    if child_app and child_model and child_field:
        child_model_class = get_model(child_app, child_model)
        child_queryset = get_queryset(child_model_class, None, None)
        child_keywords = get_keywords("pk", value, m2m=False)

        get_child = do_filter(child_queryset, child_keywords)
        get_child_model = getattr(get_child[0], child_field, None)

        keywords = get_keywords(child_field, get_child_model.id, m2m=False)

    results = do_filter(queryset, keywords)

    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, "ordering", False):
        results = list(results)
        sort_results(results)

    serialized_results = serialize_results(results)
    return JsonResponse(serialized_results, safe=False)
Exemple #2
0
def filterchain_all(request, app, model, field, foreign_key_app_name,
                    foreign_key_model_name, foreign_key_field_name, value):
    """Returns filtered results followed by excluded results below."""
    model_class = get_model(app, model)
    keywords = get_keywords(field, value)
    # filter queryset using limit_choices_to
    limit_choices_to = get_limit_choices_to(foreign_key_app_name, foreign_key_model_name, foreign_key_field_name)
    queryset = get_queryset(model_class, limit_choices_to=limit_choices_to)

    filtered = list(do_filter(queryset, keywords))
    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, 'ordering', False):
        sort_results(list(filtered))

    excluded = list(do_filter(queryset, keywords, exclude=True))
    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, 'ordering', False):
        sort_results(list(excluded))

    # Empty choice to separate filtered and excluded results.
    empty_choice = {'value': "", 'display': "---------"}

    serialized_results = (
        serialize_results(filtered) +
        [empty_choice] +
        serialize_results(excluded)
    )

    return JsonResponse(serialized_results, safe=False)
Exemple #3
0
def filterchain_all(request, app, model, field, foreign_key_app_name,
                    foreign_key_model_name, foreign_key_field_name, value):
    """Returns filtered results followed by excluded results below."""

    model_class = get_model(app, model)
    keywords = get_keywords(field, value)
    # filter queryset using limit_choices_to
    limit_choices_to = get_limit_choices_to(foreign_key_app_name,
                                            foreign_key_model_name,
                                            foreign_key_field_name)
    queryset = get_queryset(model_class, limit_choices_to=limit_choices_to)

    filtered = list(queryset.filter(**keywords))
    sort_results(filtered)

    excluded = list(queryset.exclude(**keywords))
    sort_results(excluded)

    # Empty choice to separate filtered and excluded results.
    empty_choice = {'value': "", 'display': "---------"}

    serialized_results = (serialize_results(filtered) + [empty_choice] +
                          serialize_results(excluded))

    results_json = json.dumps(serialized_results)
    return HttpResponse(results_json, content_type='application/json')
Exemple #4
0
def filterchain(request,
                app,
                model,
                field,
                foreign_key_app_name,
                foreign_key_model_name,
                foreign_key_field_name,
                value,
                manager=None):

    model_class = get_model(app, model)
    keywords = get_keywords(field, value)
    # filter queryset using limit_choices_to
    limit_choices_to = get_limit_choices_to(foreign_key_app_name,
                                            foreign_key_model_name,
                                            foreign_key_field_name)
    queryset = get_queryset(model_class, manager, limit_choices_to)

    results = queryset.filter(**keywords)

    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, 'ordering', False):
        results = list(results)
        sort_results(results)

    serialized_results = serialize_results(results)
    results_json = json.dumps(serialized_results)
    return HttpResponse(results_json, content_type='application/json')
Exemple #5
0
def filterchain(request,
                app,
                model,
                field,
                foreign_key_app_name,
                foreign_key_model_name,
                foreign_key_field_name,
                value,
                manager=None):
    model_class = get_model(app, model)
    m2m = is_m2m(model_class, field)
    keywords = get_keywords(field, value, m2m=m2m)
    # filter queryset using limit_choices_to
    limit_choices_to = get_limit_choices_to(foreign_key_app_name,
                                            foreign_key_model_name,
                                            foreign_key_field_name)
    queryset = get_queryset(model_class, manager, limit_choices_to)

    results = queryset.filter(**keywords)

    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, 'ordering', False):
        results = list(results)
        sort_results(results)

    serialized_results = serialize_results(results)
    return JsonResponse(serialized_results, safe=False)
Exemple #6
0
def filterchain(request, app, model, field, foreign_key_app_name, foreign_key_model_name,
                foreign_key_field_name, value, manager=None):
    model_class = get_model(app, model)
    m2m = is_m2m(model_class, field)
    keywords = get_keywords(field, value, m2m=m2m)

    # SECURITY: Make sure all smart selects requests are opt-in
    foreign_model_class = get_model(foreign_key_app_name, foreign_key_model_name)
    if not any([(isinstance(f, ChainedManyToManyField) or
                 isinstance(f, ChainedForeignKey))
                for f in foreign_model_class._meta.get_fields()]):
        raise PermissionDenied("Smart select disallowed")

    # filter queryset using limit_choices_to
    limit_choices_to = get_limit_choices_to(foreign_key_app_name, foreign_key_model_name, foreign_key_field_name)
    queryset = get_queryset(model_class, manager, limit_choices_to)

    results = do_filter(queryset, keywords)

    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, 'ordering', False):
        results = list(results)
        sort_results(results)

    serialized_results = serialize_results(results)
    return JsonResponse(serialized_results, safe=False)
def filterchain_all(request, app, model, field, foreign_key_app_name,
                    foreign_key_model_name, foreign_key_field_name, value):
    """Returns filtered results followed by excluded results below."""

    model_class = get_model(app, model)
    keywords = get_keywords(field, value)
    # filter queryset using limit_choices_to
    limit_choices_to = get_limit_choices_to(foreign_key_app_name, foreign_key_model_name, foreign_key_field_name)
    queryset = get_queryset(model_class, limit_choices_to=limit_choices_to)

    filtered = list(queryset.filter(**keywords))
    sort_results(filtered)

    excluded = list(queryset.exclude(**keywords))
    sort_results(excluded)

    # Empty choice to separate filtered and excluded results.
    empty_choice = {'value': "", 'display': "---------"}

    serialized_results = (
        serialize_results(filtered) +
        [empty_choice] +
        serialize_results(excluded)
    )

    results_json = json.dumps(serialized_results)
    return HttpResponse(results_json, content_type='application/json')
Exemple #8
0
def filterchain_all(request, app, model, field, foreign_key_app_name,
                    foreign_key_model_name, foreign_key_field_name, value):
    """Returns filtered results followed by excluded results below."""
    model_class = get_model(app, model)
    keywords = get_keywords(field, value)
    # filter queryset using limit_choices_to
    limit_choices_to = get_limit_choices_to(foreign_key_app_name,
                                            foreign_key_model_name,
                                            foreign_key_field_name)
    queryset = get_queryset(model_class, limit_choices_to=limit_choices_to)

    filtered = list(do_filter(queryset, keywords))
    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, 'ordering', False):
        sort_results(list(filtered))

    excluded = list(do_filter(queryset, keywords, exclude=True))
    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, 'ordering', False):
        sort_results(list(excluded))

    # Empty choice to separate filtered and excluded results.
    empty_choice = {'value': "", 'display': "---------"}

    serialized_results = (serialize_results(filtered) + [empty_choice] +
                          serialize_results(excluded))

    return JsonResponse(serialized_results, safe=False)
Exemple #9
0
def filterchain(request, app, model, field, foreign_key_app_name, foreign_key_model_name,
                foreign_key_field_name, value, manager=None):
    model_class = get_model(app, model)
    m2m = is_m2m(model_class, field)
    keywords = get_keywords(field, value, m2m=m2m)

    # SECURITY: Make sure all smart selects requests are opt-in
    foreign_model_class = get_model(foreign_key_app_name, foreign_key_model_name)
    if not any([(isinstance(f, ChainedManyToManyField) or
                 isinstance(f, ChainedForeignKey))
                for f in foreign_model_class._meta.get_fields()]):
        raise PermissionDenied("Smart select disallowed")

    # filter queryset using limit_choices_to
    limit_choices_to = get_limit_choices_to(foreign_key_app_name, foreign_key_model_name, foreign_key_field_name)
    queryset = get_queryset(model_class, manager, limit_choices_to)

    results = do_filter(queryset, keywords)

    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, 'ordering', False):
        results = list(results)
        sort_results(results)

    serialized_results = serialize_results(results)
    return JsonResponse(serialized_results, safe=False)
Exemple #10
0
def filterchain(request,
                app,
                model,
                field,
                foreign_key_app_name,
                foreign_key_model_name,
                foreign_key_field_name,
                display_field,
                value,
                manager=None):
    model_class = get_model(app, model)
    m2m = is_m2m(model_class, field)
    if '__' in field:
        if value != 'ALL':
            keywords = get_keywords(field[field.find('__') + 2:],
                                    value,
                                    m2m=m2m)
        else:
            keywords = {}
    else:
        if value != 'ALL':
            keywords = get_keywords(field, value, m2m=m2m)
        else:
            keywords = {}

    # SECURITY: Make sure all smart selects requests are opt-in
    foreign_model_class = get_model(foreign_key_app_name,
                                    foreign_key_model_name)
    if not any([(isinstance(f, ChainedManyToManyField)
                 or isinstance(f, ChainedForeignKey))
                for f in foreign_model_class._meta.get_fields()]):
        raise PermissionDenied("Smart select disallowed")

    # filter queryset using limit_choices_to
    limit_choices_to = get_limit_choices_to(foreign_key_app_name,
                                            foreign_key_model_name,
                                            foreign_key_field_name)
    queryset = get_queryset(model_class, manager, limit_choices_to)

    results = do_filter(queryset, keywords)
    '''results=queryset.all()
    filtered_results=[]
    for result in results:
        for keyword, value in iteritems(keywords):
            if value=='ALL' or (hasattr(result,keyword) and getattr(result,keyword)!=None and str(getattr(result,keyword).id)==value):
                filtered_results.append(result)
    results=filtered_results'''

    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, 'ordering', False):
        results = list(results)
        sort_results(results)

    serialized_results = serialize_results(results, display_field)
    return JsonResponse(serialized_results, safe=False)
Exemple #11
0
def filterchain(request,
                app,
                model,
                field,
                foreign_key_app_name,
                foreign_key_model_name,
                foreign_key_field_name,
                value,
                chain_field,
                manager=None,
                pk=None):

    chain_field = chain_field.split("__")
    chain_field.reverse()

    fk_model = get_model(foreign_key_app_name, foreign_key_model_name)
    if pk:
        obj = fk_model.objects.get(pk=pk)
        obj = getattr(obj, chain_field.pop())
    else:
        f = fk_model._meta.get_field_by_name(chain_field.pop())[0]
        obj = f.rel.to.objects.get(pk=value)
    while len(chain_field):
        obj = getattr(obj, chain_field.pop())
    value = obj.pk
    #an alternative is
    #- traverse relations given in chain_field to get the final model
    #- reverse chain_field
    #- filter final model with (reversed_chain_field=value)
    #- filter queryset below with (field__in = [results from above[)
    #that would allow other kinds of relations in chain_field - now it's only foreign key traversed forward

    model_class = get_model(app, model)
    keywords = get_keywords(field, value)
    # filter queryset using limit_choices_to
    limit_choices_to = get_limit_choices_to(foreign_key_app_name,
                                            foreign_key_model_name,
                                            foreign_key_field_name)
    queryset = get_queryset(model_class, manager, limit_choices_to)

    results = queryset.filter(**keywords)

    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, 'ordering', False):
        results = list(results)
        sort_results(results)

    serialized_results = serialize_results(results)
    results_json = json.dumps(serialized_results)
    return HttpResponse(results_json, content_type='application/json')
Exemple #12
0
def filterchain(request, app, model, field, value, manager=None):
    model_class = get_model(app, model)
    keywords = get_keywords(field, value)
    queryset = get_queryset(model_class, manager)

    results = queryset.filter(**keywords)

    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, 'ordering', False):
        results = list(results)
        sort_results(results)

    serialized_results = serialize_results(results)
    results_json = json.dumps(serialized_results)
    return HttpResponse(results_json, content_type='application/json')
def filterchain_all(
    request,
    app,
    model,
    field,
    foreign_key_app_name,
    foreign_key_model_name,
    foreign_key_field_name,
    value,
):
    """Returns filtered results followed by excluded results below."""
    model_class = get_model(app, model)
    keywords = get_keywords(field, value)

    # SECURITY: Make sure all smart selects requests are opt-in
    foreign_model_class = get_model(foreign_key_app_name, foreign_key_model_name)
    if not any(
        [
            (isinstance(f, ChainedManyToManyField) or isinstance(f, ChainedForeignKey))
            for f in foreign_model_class._meta.get_fields()
        ]
    ):
        raise PermissionDenied("Smart select disallowed")

    # filter queryset using limit_choices_to
    limit_choices_to = get_limit_choices_to(
        foreign_key_app_name, foreign_key_model_name, foreign_key_field_name
    )
    queryset = get_queryset(model_class, limit_choices_to=limit_choices_to)

    filtered = list(do_filter(queryset, keywords))
    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, "ordering", False):
        sort_results(list(filtered))

    excluded = list(do_filter(queryset, keywords, exclude=True))
    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, "ordering", False):
        sort_results(list(excluded))

    # Empty choice to separate filtered and excluded results.
    empty_choice = {"value": "", "display": "---------"}

    serialized_results = (
        serialize_results(filtered) + [empty_choice] + serialize_results(excluded)
    )

    return JsonResponse(serialized_results, safe=False)
Exemple #14
0
def filterchain(request, app, model, field, foreign_key_app_name, foreign_key_model_name,
                foreign_key_field_name, value, manager=None):
    model_class = get_model(app, model)
    m2m = is_m2m(model_class, field)
    keywords = get_keywords(field, value, m2m=m2m)
    # filter queryset using limit_choices_to
    limit_choices_to = get_limit_choices_to(foreign_key_app_name, foreign_key_model_name, foreign_key_field_name)
    queryset = get_queryset(model_class, manager, limit_choices_to)

    results = do_filter(queryset, keywords)

    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, 'ordering', False):
        results = list(results)
        sort_results(results)

    serialized_results = serialize_results(results)
    return JsonResponse(serialized_results, safe=False)
def filterchain(request, app, model, field, foreign_key_app_name, foreign_key_model_name,
                foreign_key_field_name, value, manager=None):
    model_class = get_model(app, model)
    m2m = is_m2m(model_class, field)
    keywords = get_keywords(field, value, m2m=m2m)
    # filter queryset using limit_choices_to
    limit_choices_to = get_limit_choices_to(foreign_key_app_name, foreign_key_model_name, foreign_key_field_name)
    queryset = get_queryset(model_class, manager, limit_choices_to)

    results = queryset.filter(**keywords)

    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, 'ordering', False):
        results = list(results)
        sort_results(results)

    serialized_results = serialize_results(results)
    results_json = json.dumps(serialized_results)
    return HttpResponse(results_json, content_type='application/json')
Exemple #16
0
def filterchain(request, app, model, field, foreign_key_app_name, foreign_key_model_name,
                foreign_key_field_name, value, chain_field, manager=None, pk=None):
    
    chain_field = chain_field.split("__")
    chain_field.reverse()

    fk_model = get_model(foreign_key_app_name, foreign_key_model_name)
    if pk:
	obj = fk_model.objects.get(pk = pk)
	obj = getattr(obj, chain_field.pop())
    else:
        f = fk_model._meta.get_field_by_name(chain_field.pop())[0]
        obj = f.rel.to.objects.get(pk = value)
    while len(chain_field):
	obj = getattr(obj, chain_field.pop())
    value = obj.pk
    #an alternative is
    #- traverse relations given in chain_field to get the final model
    #- reverse chain_field
    #- filter final model with (reversed_chain_field=value)
    #- filter queryset below with (field__in = [results from above[)
    #that would allow other kinds of relations in chain_field - now it's only foreign key traversed forward

    model_class = get_model(app, model)
    keywords = get_keywords(field, value)
    # filter queryset using limit_choices_to
    limit_choices_to = get_limit_choices_to(foreign_key_app_name, foreign_key_model_name, foreign_key_field_name)
    queryset = get_queryset(model_class, manager, limit_choices_to)

    results = queryset.filter(**keywords)

    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, 'ordering', False):
        results = list(results)
        sort_results(results)

    serialized_results = serialize_results(results)
    results_json = json.dumps(serialized_results)
    return HttpResponse(results_json, content_type='application/json')
Exemple #17
0
def filterchain_all(request, app, model, field, foreign_key_app_name,
                    foreign_key_model_name, foreign_key_field_name, value):
    """Returns filtered results followed by excluded results below."""
    model_class = get_model(app, model)
    keywords = get_keywords(field, value)

    # SECURITY: Make sure all smart selects requests are opt-in
    foreign_model_class = get_model(foreign_key_app_name, foreign_key_model_name)
    if not any([(isinstance(f, ChainedManyToManyField) or
                 isinstance(f, ChainedForeignKey))
                for f in foreign_model_class._meta.get_fields()]):
        raise PermissionDenied("Smart select disallowed")

    # filter queryset using limit_choices_to
    limit_choices_to = get_limit_choices_to(foreign_key_app_name, foreign_key_model_name, foreign_key_field_name)
    queryset = get_queryset(model_class, limit_choices_to=limit_choices_to)

    filtered = list(do_filter(queryset, keywords))
    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, 'ordering', False):
        sort_results(list(filtered))

    excluded = list(do_filter(queryset, keywords, exclude=True))
    # Sort results if model doesn't include a default ordering.
    if not getattr(model_class._meta, 'ordering', False):
        sort_results(list(excluded))

    # Empty choice to separate filtered and excluded results.
    empty_choice = {'value': "", 'display': "---------"}

    serialized_results = (
        serialize_results(filtered) +
        [empty_choice] +
        serialize_results(excluded)
    )

    return JsonResponse(serialized_results, safe=False)
Exemple #18
0
def filterchain_all(request, app, model, field, value):
    """Returns filtered results followed by excluded results below."""

    model_class = get_model(app, model)
    keywords = get_keywords(field, value)
    queryset = get_queryset(model_class)

    filtered = list(queryset.filter(**keywords))
    sort_results(filtered)

    excluded = list(queryset.exclude(**keywords))
    sort_results(excluded)

    # Empty choice to separate filtered and excluded results.
    empty_choice = {'value': "", 'display': "---------"}

    serialized_results = (
        serialize_results(filtered) +
        [empty_choice] +
        serialize_results(excluded)
    )

    results_json = json.dumps(serialized_results, cls=UUIDEncoder)
    return HttpResponse(results_json, content_type='application/json')