From 49eaf00e46e94d320877e86c679b2ada30319ed5 Mon Sep 17 00:00:00 2001 From: NateScarlet Date: Wed, 12 Jun 2019 23:19:22 +0800 Subject: [PATCH] Improve code style --- graphene_django/fields.py | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/graphene_django/fields.py b/graphene_django/fields.py index ad9bff3..5ec59eb 100644 --- a/graphene_django/fields.py +++ b/graphene_django/fields.py @@ -158,34 +158,27 @@ class DjangoConnectionField(ConnectionField): ): first = kwargs.get("first") last = kwargs.get("last") - - if not (first is None or first > 0): + if first is not None and first <= 0: raise ValueError( - "`first` argument must be positive, got `{first}`".format(**locals())) - if not (last is None or last > 0): + "`first` argument must be positive, got `{first}`".format(first=first)) + if last is not None and last <= 0: raise ValueError( - "`last` argument must be positive, got `{last}`".format(**locals())) - + "`last` argument must be positive, got `{last}`".format(last=last)) if enforce_first_or_last and not (first or last): raise ValueError( "You must provide a `first` or `last` value " - "to properly paginate the `{info.field_name}` connection.".format(**locals())) + "to properly paginate the `{info.field_name}` connection.".format(info=info)) 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) - kwargs["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) - kwargs["last"] = min(last, max_limit) - if first is None and last is None: kwargs['first'] = max_limit + else: + count = min(i for i in (first, last) if i) + if count > max_limit: + raise ValueError(("Requesting {count} records " + "on the `{info.field_name}` connection " + "exceeds the limit of {max_limit} records.").format( + count=count, info=info, max_limit=max_limit)) iterable = resolver(root, info, **kwargs) queryset = cls.resolve_queryset(connection, default_manager, info, kwargs)