Beispiel #1
0
    def connection_resolver(cls, resolver, connection, default_manager,
                            max_limit, enforce_first_or_last, root, info,
                            **args):
        first = args.get("first")
        last = args.get("last")

        if enforce_first_or_last:
            assert first or last, (
                "You must provide a `first` or `last` value to properly paginate the `{}` connection."
            ).format(info.field_name)

        if max_limit:
            if first:
                assert first <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the `first` limit of {} records."
                ).format(first, info.field_name, max_limit)
                args["first"] = min(first, max_limit)

            if last:
                assert last <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the `last` limit of {} records."
                ).format(last, info.field_name, max_limit)
                args["last"] = min(last, max_limit)

        iterable = resolver(root, info, **args)
        on_resolve = partial(cls.resolve_connection, connection,
                             default_manager, args)

        if Promise.is_thenable(iterable):
            return Promise.resolve(iterable).then(on_resolve)

        return on_resolve(iterable)
Beispiel #2
0
    def connection_resolver(cls, resolver, connection, default_manager, max_limit,
                            enforce_first_or_last, root, args, context, info):
        first = args.get('first')
        last = args.get('last')

        if enforce_first_or_last:
            assert first or last, (
                'You must provide a `first` or `last` value to properly paginate the `{}` connection.'
            ).format(info.field_name)

        if max_limit:
            if first:
                assert first <= max_limit, (
                    'Requesting {} records on the `{}` connection exceeds the `first` limit of {} records.'
                ).format(first, info.field_name, max_limit)
                args['first'] = min(first, max_limit)

            if last:
                assert last <= max_limit, (
                    'Requesting {} records on the `{}` connection exceeds the `last` limit of {} records.'
                ).format(first, info.field_name, max_limit)
                args['last'] = min(last, max_limit)

        iterable = resolver(root, args, context, info)
        on_resolve = partial(cls.resolve_connection, connection, default_manager, args)

        if Promise.is_thenable(iterable):
            return Promise.resolve(iterable).then(on_resolve)

        return on_resolve(iterable)
Beispiel #3
0
    def connection_resolver(
        cls,
        resolver,
        connection,
        default_manager,
        max_limit,
        enforce_first_or_last,
        filterset_class,
        filters_name,
        root,
        info,
        **args,
    ):

        # Disable `enforce_first_or_last` if not querying for `edges`.
        values = [
            field.name.value
            for field in info.field_asts[0].selection_set.selections
        ]
        if "edges" not in values:
            enforce_first_or_last = False

        first = args.get("first")
        last = args.get("last")

        if enforce_first_or_last:
            assert first or last, (
                "You must provide a `first` or `last` value to properly "
                "paginate the `{}` connection.").format(info.field_name)

        if max_limit:
            if first:
                assert first <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the "
                    "`first` limit of {} records.").format(
                        first, info.field_name, max_limit)
                args["first"] = min(first, max_limit)

            if last:
                assert last <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the "
                    "`last` limit of {} records.").format(
                        last, info.field_name, max_limit)
                args["last"] = min(last, max_limit)

        iterable = resolver(root, info, **args)

        on_resolve = partial(cls.resolve_connection, connection,
                             default_manager, args)

        filter_input = args.get(filters_name)

        if filter_input and filterset_class:
            iterable = filterset_class(data=dict(filter_input),
                                       queryset=iterable,
                                       request=info.context).qs

        if Promise.is_thenable(iterable):
            return Promise.resolve(iterable).then(on_resolve)
        return on_resolve(iterable)
    def connection_resolver(cls, resolver, connection, default_search,
                            max_limit, enforce_first_or_last, root, info,
                            **args):
        first = args.get('first')
        last = args.get('last')

        if enforce_first_or_last:
            assert first or last, (
                'You must provide a `first` or `last` value to properly paginate the `{}` connection.'
            ).format(info.field_name)

        if max_limit:
            if first:
                assert first <= max_limit, (
                    'Requesting {} records on the `{}` connection exceeds the `first` limit of {} records.'
                ).format(first, info.field_name, max_limit)
                args['first'] = min(first, max_limit)

            if last:
                assert last <= max_limit, (
                    'Requesting {} records on the `{}` connection exceeds the `last` limit of {} records.'
                ).format(first, info.field_name, max_limit)
                args['last'] = min(last, max_limit)

        results = resolver(root, info, **args)
        on_resolve = partial(cls.resolve_connection, connection,
                             default_search, args)

        if Promise.is_thenable(results):
            return Promise.resolve(results).then(on_resolve)

        return on_resolve(results)
Beispiel #5
0
    def connection_resolver(
        cls,
        resolver,
        connection,
        default_manager,
        max_limit,
        enforce_first_or_last,
        filterset_class,
        filters_name,
        root,
        info,
        **args,
    ):

        # Disable `enforce_first_or_last` if not querying for `edges`.
        values = [
            field.name.value for field in info.field_asts[0].selection_set.selections
        ]
        if "edges" not in values:
            enforce_first_or_last = False

        first = args.get("first")
        last = args.get("last")

        if enforce_first_or_last:
            assert first or last, (
                "You must provide a `first` or `last` value to properly "
                "paginate the `{}` connection."
            ).format(info.field_name)

        if max_limit:
            if first:
                assert first <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the "
                    "`first` limit of {} records."
                ).format(first, info.field_name, max_limit)
                args["first"] = min(first, max_limit)

            if last:
                assert last <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the "
                    "`last` limit of {} records."
                ).format(last, info.field_name, max_limit)
                args["last"] = min(last, max_limit)

        iterable = resolver(root, info, **args)

        on_resolve = partial(cls.resolve_connection, connection, default_manager, args)

        filter_input = args.get(filters_name)
        if filter_input and filterset_class:
            iterable = filterset_class(
                data=dict(filter_input), queryset=iterable, request=info.context
            ).qs

        if Promise.is_thenable(iterable):
            return Promise.resolve(iterable).then(on_resolve)
        return on_resolve(iterable)
Beispiel #6
0
 def connection_resolver(cls, resolver, connection_type, root, info,
                         **args):
     iterable = resolver(root, info, **args)
     if isinstance(connection_type, graphene.NonNull):
         connection_type = connection_type.of_type
     on_resolve = partial(cls.resolve_connection, connection_type, args)
     if Promise.is_thenable(iterable):
         return Promise.resolve(iterable).then(on_resolve)
     return on_resolve(iterable)
Beispiel #7
0
    def connection_resolver(
        cls,
        resolver,
        connection,
        default_manager,
        queryset_resolver,
        max_limit,
        enforce_first_or_last,
        root,
        info,
        **args
    ):
        first = args.get("first")
        last = args.get("last")
        offset = args.get("offset")
        before = args.get("before")

        if enforce_first_or_last:
            assert first or last, (
                "You must provide a `first` or `last` value to properly paginate the `{}` connection."
            ).format(info.field_name)

        if max_limit:
            if first:
                assert first <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the `first` limit of {} records."
                ).format(first, info.field_name, max_limit)
                args["first"] = min(first, max_limit)

            if last:
                assert last <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the `last` limit of {} records."
                ).format(last, info.field_name, max_limit)
                args["last"] = min(last, max_limit)

        if offset is not None:
            assert before is None, (
                "You can't provide a `before` value at the same time as an `offset` value to properly paginate the `{}` connection."
            ).format(info.field_name)

        # eventually leads to DjangoObjectType's get_queryset (accepts queryset)
        # or a resolve_foo (does not accept queryset)
        iterable = resolver(root, info, **args)
        if iterable is None:
            iterable = default_manager
        # thus the iterable gets refiltered by resolve_queryset
        # but iterable might be promise
        iterable = queryset_resolver(connection, iterable, info, args)
        on_resolve = partial(
            cls.resolve_connection, connection, args, max_limit=max_limit
        )

        if Promise.is_thenable(iterable):
            return Promise.resolve(iterable).then(on_resolve)

        return on_resolve(iterable)
Beispiel #8
0
 def resolver(self, parent_resolver, root, info, *args, **kwargs):
     routing_key = None
     if self._classifier:
         routing_key = self._classifier(root, info, *args, **kwargs)
         if Promise.is_thenable(routing_key):
             return Promise.resolve(
                 routing_key).then(lambda key: self._resolver(
                     key, parent_resolver, root, info, *args, **kwargs))
     return self._resolver(routing_key, parent_resolver, root, info, *args,
                           **kwargs)
Beispiel #9
0
    def relay_connection_resolver(
        cls, resolver, connection, max_limit, enforce_first_or_last, root, info, **args
    ):

        # eventually leads to DjangoObjectType's get_queryset (accepts queryset)
        # or a resolve_foo (does not accept queryset)
        iterable = resolver(root, info, **args)

        on_resolve = partial(
            cls.resolve_connection, connection, args, max_limit=max_limit
        )

        if Promise.is_thenable(iterable):
            return Promise.resolve(iterable).then(on_resolve)

        return on_resolve(iterable)
Beispiel #10
0
    def connection_resolver(cls,
                            resolver,
                            connection_type,
                            root,
                            info,
                            connection_field=None,
                            **args):
        first = args.get("first")
        last = args.get("last")
        enforce_first_or_last = args.get("enforce_first_or_last")
        max_limit = args.get("max_limit")
        # connection_field = args.get("connection_field")

        if enforce_first_or_last:
            assert first or last, (
                "You must provide a `first` or `last` value to properly "
                "paginate the `{}` connection.").format(info.field_name)

        if max_limit:
            if first:
                assert first <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds "
                    "the `first` limit of {} records.").format(
                        first, info.field_name, max_limit)
                args["first"] = min(first, max_limit)

            if last:
                assert last <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds "
                    "the `last` limit of {} records.").format(
                        last, info.field_name, max_limit)
                args["last"] = min(last, max_limit)

        iterable = resolver(root, info, **args)
        if isinstance(connection_type, graphene.NonNull):
            connection_type = connection_type.of_type

        on_resolve = partial(cls.resolve_connection,
                             connection_type,
                             args,
                             connection_field=connection_field)

        if Promise.is_thenable(iterable):
            return Promise.resolve(iterable).then(on_resolve)

        return on_resolve(iterable)
Beispiel #11
0
    def connection_resolver(
        cls,
        resolver,
        connection,
        default_manager,
        queryset_resolver,
        max_limit,
        enforce_first_or_last,
        csd_filter_fields,
        root,
        info,
        **args,
    ):
        cls.validate_first_last_limit(args, enforce_first_or_last, max_limit,
                                      info)

        # eventually leads to DjangoObjectType's get_queryset (accepts queryset)
        # or a resolve_foo (does not accept queryset)
        iterable = resolver(root, info, **args)
        if iterable is None:
            iterable = default_manager
        # thus the iterable gets refiltered by resolve_queryset
        # but iterable might be promise
        iterable = queryset_resolver(connection, iterable, info, args)

        filter_dict = args.get('filter', {})
        if filter_dict:
            normalized_filter_dict = convert_dict_camel_to_underscore(
                filter_dict)
            iterable = get_filtered_queryset(normalized_filter_dict, iterable,
                                             csd_filter_fields)

        # apply ordering
        order_list = args.get('orderBy', [])
        if order_list:
            order_list = [camel_to_underscore(order) for order in order_list]
            iterable = iterable.order_by(*order_list)

        on_resolve = partial(cls.resolve_connection, connection, args)

        if Promise.is_thenable(iterable):
            return Promise.resolve(iterable).then(on_resolve)

        return on_resolve(iterable)
Beispiel #12
0
    def _resolver(self, routing_key, parent_resolver, root, info, *args,
                  **kwargs):
        handler = None

        if routing_key:
            handler = self._get_handler(root,
                                        info,
                                        *args,
                                        routing_key=routing_key,
                                        **kwargs)

        if not handler:
            handler = self._default_handler or parent_resolver

        if handler:
            if Promise.is_thenable(handler):
                return Promise.resolve(handler(root, info, *args, **kwargs))

            return handler(root, info, *args, **kwargs)
        return None
Beispiel #13
0
    def connection_resolver(
        cls,
        resolver,
        connection,
        default_manager,
        queryset_resolver,
        max_limit,
        enforce_first_or_last,
        filterset_class,
        filters_name,
        root,
        info,
        **args,
    ):

        # Disable `enforce_first_or_last` if not querying for `edges`.
        values = [
            field.name.value for field in info.field_asts[0].selection_set.selections
        ]
        if "edges" not in values:
            enforce_first_or_last = False

        first = args.get("first")
        last = args.get("last")

        if enforce_first_or_last:
            assert first or last, (
                "You must provide a `first` or `last` value to properly "
                "paginate the `{}` connection."
            ).format(info.field_name)

        if max_limit:
            if first:
                assert first <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the "
                    "`first` limit of {} records."
                ).format(first, info.field_name, max_limit)
                args["first"] = min(first, max_limit)

            if last:
                assert last <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the "
                    "`last` limit of {} records."
                ).format(last, info.field_name, max_limit)
                args["last"] = min(last, max_limit)

        iterable = resolver(root, info, **args)

        if iterable is None:
            iterable = default_manager
        # thus the iterable gets refiltered by resolve_queryset
        # but iterable might be promise
        iterable = queryset_resolver(connection, iterable, info, args)

        on_resolve = partial(cls.resolve_connection, connection, args)

        filter_input = args.get(filters_name)

        if filter_input and filterset_class:
            instance = filterset_class(
                data=dict(filter_input), queryset=iterable, request=info.context
            )
            # Make sure filter input has valid values
            if not instance.is_valid():
                raise GraphQLError(json.dumps(instance.errors.get_json_data()))
            iterable = instance.qs

        if Promise.is_thenable(iterable):
            return Promise.resolve(iterable).then(on_resolve)
        return on_resolve(iterable)
Beispiel #14
0
    def connection_resolver(
        cls,
        resolver,
        connection,
        default_manager,
        queryset_resolver,
        max_limit,
        enforce_first_or_last,
        filterset_class,
        filters_name,
        root,
        info,
        **args,
    ):
        # Disable `enforce_first_or_last` if not querying for `edges`.
        values = [
            field.name.value
            for field in info.field_asts[0].selection_set.selections
        ]
        if "edges" not in values:
            enforce_first_or_last = False

        first = args.get("first")
        last = args.get("last")

        if enforce_first_or_last and not (first or last):
            raise GraphQLError(
                f"You must provide a `first` or `last` value to properly paginate "
                f"the `{info.field_name}` connection.")

        if max_limit:
            if first:
                assert first <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the "
                    "`first` limit of {} records.").format(
                        first, info.field_name, max_limit)
                args["first"] = min(first, max_limit)

            if last:
                assert last <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the "
                    "`last` limit of {} records.").format(
                        last, info.field_name, max_limit)
                args["last"] = min(last, max_limit)

        iterable = resolver(root, info, **args)

        if iterable is None:
            iterable = default_manager
        # thus the iterable gets refiltered by resolve_queryset
        # but iterable might be promise
        iterable = queryset_resolver(connection, iterable, info, args)

        on_resolve = partial(cls.resolve_connection,
                             connection,
                             args,
                             max_limit=max_limit)

        iterable = cls.filter_iterable(iterable, filterset_class, filters_name,
                                       info, **args)

        if Promise.is_thenable(iterable):
            return Promise.resolve(iterable).then(on_resolve)
        return on_resolve(iterable)
Beispiel #15
0
    def connection_resolver(
        cls,
        resolver,
        connection,
        default_manager,
        queryset_resolver,
        max_limit,
        enforce_first_or_last,
        filterset_class,
        filters_name,
        root,
        info,
        **args,
    ):
        enforce_first_or_last = cls.is_first_or_last_required(info)

        first = args.get("first")
        last = args.get("last")

        if enforce_first_or_last and not (first or last):
            raise GraphQLError(
                f"You must provide a `first` or `last` value to properly paginate "
                f"the `{info.field_name}` connection.")

        if max_limit:
            if first:
                assert first <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the "
                    "`first` limit of {} records.").format(
                        first, info.field_name, max_limit)
                args["first"] = min(first, max_limit)

            if last:
                assert last <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the "
                    "`last` limit of {} records.").format(
                        last, info.field_name, max_limit)
                args["last"] = min(last, max_limit)

        iterable = resolver(root, info, **args)

        if iterable is None:
            iterable = default_manager
        # thus the iterable gets refiltered by resolve_queryset
        # but iterable might be promise
        iterable = queryset_resolver(connection, iterable, info, args)

        on_resolve = partial(cls.resolve_connection,
                             connection,
                             args,
                             max_limit=max_limit)

        # for nested filters get channel from ChannelContext object
        if "channel" not in args and hasattr(root, "channel_slug"):
            args["channel"] = root.channel_slug

        iterable = cls.filter_iterable(iterable, filterset_class, filters_name,
                                       info, **args)

        if Promise.is_thenable(iterable):
            return Promise.resolve(iterable).then(on_resolve)
        return on_resolve(iterable)
Beispiel #16
0
    def connection_resolver(cls, resolver, connection, default_manager,
                            queryset_resolver, max_limit,
                            enforce_first_or_last, root, info, **args):
        first = args.get("first")
        last = args.get("last")

        if enforce_first_or_last:
            assert first or last, (
                "You must provide a `first` or `last` value to properly paginate the `{}` connection."
            ).format(info.field_name)

        if max_limit:
            if first:
                assert first <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the `first` limit of {} records."
                ).format(first, info.field_name, max_limit)
                args["first"] = min(first, max_limit)

            if last:
                assert last <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the `last` limit of {} records."
                ).format(last, info.field_name, max_limit)
                args["last"] = min(last, max_limit)

        # eventually leads to DjangoObjectType's get_queryset (accepts queryset)
        # or a resolve_foo (does not accept queryset)
        iterable = resolver(root, info, **args)
        if iterable is None:
            iterable = default_manager
        # thus the iterable gets refiltered by resolve_queryset
        # but iterable might be promise
        iterable = queryset_resolver(connection, iterable, info, args)

        club = args.get('club', None)
        customized_club_filters = [
            'all_amendments', 'all_interpellations', 'all_debate_appearances'
        ]
        current_resolver = resolver.args[0]
        if club and current_resolver in customized_club_filters:
            id_tuple = from_global_id(club)
            if id_tuple[0] == 'ClubType':
                dfilter_key = 'date'
                if current_resolver == 'all_amendments':
                    filter_key = 'submitters'
                elif current_resolver == 'all_interpellations':
                    filter_key = 'asked_by'
                elif current_resolver == 'all_debate_appearances':
                    filter_key = 'debater'
                    dfilter_key = 'start'
                iterable = iterable.filter(
                    Q(
                        **{
                            '{}__club_memberships__club__id'.format(filter_key):
                            id_tuple[1]
                        })
                    & Q(
                        **{
                            '{}__club_memberships__start__lte'.format(filter_key):
                            F(dfilter_key)
                        })
                    & (Q(
                        **{
                            '{}__club_memberships__end__gte'.format(filter_key):
                            F(dfilter_key)
                        }
                    ) | Q(
                        **{
                            '{}__club_memberships__end__isnull'.format(filter_key):
                            True
                        }))).distinct()

        order = args.get('orderBy', None)
        if order:
            iterable = iterable.order_by(*order)

        on_resolve = partial(cls.resolve_connection, connection, args)

        if Promise.is_thenable(iterable):
            return Promise.resolve(iterable).then(on_resolve)

        return on_resolve(iterable)
Beispiel #17
0
 def _resolver(root, info, *args, **kwargs):
     if self._acl_validator(root, info, *args, **kwargs):
         result = resolver(root, info, *args, **kwargs)
         if Promise.is_thenable(result):
             return Promise.resolve(result)
         return result
Beispiel #18
0
    def connection_resolver(
        cls,
        resolver,
        connection,
        default_manager,
        queryset_resolver,
        max_limit,
        enforce_first_or_last,
        filterset_class,
        filters_name,
        sort_enum,
        root,
        info,
        **args,
    ):
        sort_by = args.get("sort_by")
        # Disable `enforce_first_or_last` if not querying for `edges`.
        values = [field.name.value for field in info.field_asts[0].selection_set.selections]
        if "edges" not in values:
            enforce_first_or_last = False

        first = args.get("first")
        last = args.get("last")

        if enforce_first_or_last and not (first or last):
            raise GraphQLError(
                f"You must provide a `first` or `last` value to properly paginate "
                f"the `{info.field_name}` connection."
            )

        if max_limit:
            if first:
                assert first <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the "
                    "`first` limit of {} records."
                ).format(first, info.field_name, max_limit)
                args["first"] = min(first, max_limit)

            if last:
                assert last <= max_limit, (
                    "Requesting {} records on the `{}` connection exceeds the "
                    "`last` limit of {} records."
                ).format(last, info.field_name, max_limit)
                args["last"] = min(last, max_limit)

        iterable = resolver(root, info, **args)

        if iterable is None:
            iterable = default_manager

        # gql_optimizer query
        iterable = gql_optimizer.query(iterable, info)
        if sort_by:
            try:
                custom_sort_by = getattr(sort_enum, f"qs_with_{sort_by['field']}", None)
                if custom_sort_by:
                    iterable = custom_sort_by(iterable)
                iterable = iterable.order_by(f"{sort_by['direction']}{sort_by['field']}")
            except FieldError:
                raise GraphQLError("Received sorter is invalid.")
        # thus the iterable gets refiltered by resolve_queryset
        # but iterable might be promise
        iterable = queryset_resolver(connection, iterable, info, args)

        on_resolve = partial(cls.resolve_connection, connection, args)

        filter_input = args.get(filters_name)

        if filter_input and filterset_class:
            instance = filterset_class(
                data=dict(filter_input), queryset=iterable, request=info.context
            )
            # Make sure filter input has valid values
            if not instance.is_valid():
                raise GraphQLError(json.dumps(instance.errors.get_json_data()))
            iterable = instance.qs

        if Promise.is_thenable(iterable):
            return Promise.resolve(iterable).then(on_resolve)
        return on_resolve(iterable)