示例#1
0
def _generate_list_filter_class(inner_type):
    """
    Return a Filter class that will resolve into a List(`inner_type`) graphene type.

    This allows us to do things like use `__in` and `__overlap` filters that accept
    graphene lists instead of a comma delimited value string that's interpolated into
    a list by django_filters.BaseCSVFilter (which is used to define
    django_filters.BaseInFilter)
    """

    form_field = type(f"List{inner_type.__name__}FormField",
                      (django.forms.Field, ), {})
    filter_class = type(
        f"{inner_type.__name__}ListFilter",
        (Filter, ),
        {
            "field_class":
            form_field,
            "__doc__":
            (f"{inner_type.__name__}ListFilter is a small extension of a raw "
             f"django_filters.Filter that allows us to express graphql "
             f"List({inner_type.__name__}) arguments using FilterSets. "
             f"Note that the given values are passed directly into queryset filters."
             ),
        },
    )
    convert_form_field.register(form_field)(
        lambda x: graphene.List(inner_type, required=x.required))

    return filter_class
示例#2
0
def _generate_list_filter_class(inner_type):
    """
    Source: https://github.com/graphql-python/graphene-django/issues/190

    Returns a Filter class that will resolve into a List(`inner_type`) graphene
    type.

    This allows us to do things like use `__in` filters that accept graphene
    lists instead of a comma delimited value string that's interpolated into
    a list by django_filters.BaseCSVFilter (which is used to define
    django_filters.BaseInFilter)
    """

    form_field = type(
        "List{}FormField".format(inner_type.__name__),
        (django.forms.Field, ),
        {},
    )
    filter_class = type(
        "{}ListFilter".format(inner_type.__name__),
        (django_filters.Filter, ),
        {
            "field_class":
            form_field,
            "__doc__":
            ("{0}ListFilter is a small extension of a raw django_filters.Filter "
             "that allows us to express graphql List({0}) arguments using FilterSets."
             "Note that the given values are passed directly into queryset filters."
             ).format(inner_type.__name__),
        },
    )
    convert_form_field.register(form_field)(
        lambda x: graphene.List(inner_type, required=x.required))

    return filter_class
示例#3
0
def init_converter():
    convert_form_field.register(
        MultipleChoiceField,
        lambda field: graphene.List(graphene.String, required=field.required),
    )

    convert_form_field.register(
        ListCharField,
        lambda field: graphene.List(graphene.String, required=field.required),
    )
def register_model_and_form_fields(model_field, form_field, scalar,
                                   description):
    # The odd call syntax is because `register` is usually used as a decorator.
    # We want to avoid this, because if you use decorators at the top-level,
    # they might run multiple times, or not at the right time.
    # We want the registration process to happen once during the loading of
    # this app, so we do it in this function like this instead.
    convert_django_field.register(gis_models.PointField)(make_field_converter(
        scalar, description))
    convert_form_field.register(gis_forms.PointField)(make_field_converter(
        scalar, description))
示例#5
0
文件: utils.py 项目: benzerbett/DUCT
def generate_list_filter_class(inner_type):
    form_field = type(
        "List{}FormField".format(inner_type.__name__),
        (django.forms.Field, ),
        {},
    )
    filter_class = type(
        "{}ListFilter".format(inner_type.__name__),
        (django_filters.Filter, ),
        {
            "field_class":
            form_field,
            "__doc__":
            ("{0}ListFilter is a small extension "
             "of a raw django_filters.Filter "
             "that allows us to express graphql List({0}) "
             "arguments using FilterSets.").format(inner_type.__name__),
        },
    )
    convert_form_field.register(form_field)(
        lambda x: graphene.List(inner_type, required=x.required))

    return filter_class
示例#6
0
def _generate_filter_class(inner_type, filter_type=None, non_null=False):
    _filter_type = filter_type or django_filters.Filter
    form_field = type(
        "{}FormField".format(inner_type.__name__),
        (_filter_type.field_class, ),
        {},
    )
    filter_class = type(
        "{}Filter".format(inner_type.__name__),
        (_filter_type, ),
        {
            "field_class":
            form_field,
            "__doc__":
            ("{0}Filter is a small extension of a raw {1} "
             "that allows us to express graphql ({0}) arguments using FilterSets."
             "Note that the given values are passed directly into queryset filters."
             ).format(inner_type.__name__, _filter_type),
        },
    )
    convert_form_field.register(form_field)(
        lambda _: graphene.NonNull(inner_type) if non_null else inner_type())

    return filter_class