示例#1
0
    def get_field_type(self, field_name, field_value, base_field_type):
        """Get field type.

        :return:
        """
        if not self.filter_fields:
            return None

        field_options = self.get_field_options(field_name)
        if isinstance(field_options, dict) and "lookups" in field_options:
            lookups = field_options.get("lookups", [])
        else:
            lookups = list(ALL_LOOKUP_FILTERS_AND_QUERIES)

        params = {VALUE: base_field_type}
        for lookup in lookups:
            query_cls = LOOKUP_FILTER_MAPPING.get(lookup)
            if not query_cls:
                continue
            params.update({lookup: query_cls()})

        return graphene.Argument(
            type(
                "{}{}{}{}".format(DYNAMIC_CLASS_NAME_PREFIX,
                                  to_pascal_case(self.prefix),
                                  self.connection_field.type.__name__,
                                  to_pascal_case(field_name)),
                (graphene.InputObjectType, ),
                params,
            ))
示例#2
0
 def get_graphene_argument_type(name, params):
     global nested_input_count
     nested_input_count += 1
     return graphene.Argument(
         type(
             "{}{}{}{}{}".format(
                 DYNAMIC_CLASS_NAME_PREFIX,
                 to_pascal_case(self.prefix),
                 self.connection_field.type.__name__,
                 to_pascal_case(name),
                 str(nested_input_count),
             ),
             (graphene.InputObjectType,),
             params,
         )
     )
示例#3
0
    def get_backend_query_fields(self, items, is_filterable_func,
                                 get_type_func):
        """Construct backend query fields.

        :param items:
        :param is_filterable_func:
        :param get_type_func:
        :return:
        """
        params = self.get_backend_default_query_fields_params()
        for field, value in items:
            if is_filterable_func(field):
                # Getting other backend specific fields (schema dependant)
                if self.field_belongs_to(field):
                    params.update({
                        field:
                        self.get_field_type(field, value, get_type_func(value))
                    })

        return {
            self.prefix:
            graphene.Argument(
                type(
                    "{}{}{}BackendFilter".format(
                        DYNAMIC_CLASS_NAME_PREFIX, to_pascal_case(self.prefix),
                        self.connection_field.type.__name__),
                    (graphene.InputObjectType, ),
                    params,
                ))
        }
示例#4
0
    def get_backend_query_fields(self, items, is_filterable_func,
                                 get_type_func):
        """Construct backend filtering fields.

        :param items:
        :param is_filterable_func:
        :param get_type_func:
        :return:
        """
        params = {}
        for field, value in items:
            if is_filterable_func(field):
                # Getting other backend specific fields (schema dependant)
                if self.field_belongs_to(field):
                    params.update({field: field})
        return {
            self.prefix:
            graphene.Argument(
                graphene.List(
                    graphene.Enum.from_enum(
                        enum.Enum(
                            "{}{}{}BackendEnum".format(
                                DYNAMIC_CLASS_NAME_PREFIX,
                                to_pascal_case(self.prefix),
                                self.connection_field.type.__name__),
                            params))))
        }
示例#5
0
    def get_backend_query_fields(self, items, is_filterable_func,
                                 get_type_func):
        """Construct backend filtering fields.

        :param items:
        :param is_filterable_func:
        :param get_type_func:
        :return:
        """
        # Note, that this is not the same as ``self.source_fields``.
        _keys = list(self.connection_field.type._meta.node._meta.fields.keys())
        _keys.remove('_id')
        params = zip(_keys, _keys)
        return {
            self.prefix:
            graphene.Argument(
                graphene.List(
                    graphene.Enum.from_enum(
                        enum.Enum(
                            "{}{}{}BackendEnum".format(
                                DYNAMIC_CLASS_NAME_PREFIX,
                                to_pascal_case(self.prefix),
                                self.connection_field.type.__name__),
                            params))))
        }
示例#6
0
    def get_field_type(self, field_name, field_value, base_field_type):
        """Get field type.

        :return:
        """
        params = {
            VALUE: base_field_type,  # Value to search on. Required.
            BOOST: graphene.Int(),  # Boost the given field with. Optional.
        }
        return graphene.Argument(
            type(
                "{}{}{}{}".format(DYNAMIC_CLASS_NAME_PREFIX,
                                  to_pascal_case(self.prefix),
                                  self.connection_field.type.__name__,
                                  to_pascal_case(field_name)),
                (graphene.InputObjectType, ),
                params,
            ))
示例#7
0
    def get_nested_field_type(self, field_name, field_value, base_field_type,
                              field_options):
        params = {}
        for sub_field_name in field_options.get("properties", []):
            _field_name = "{}.{}".format(field_name, sub_field_name)
            _field_type = self.get_field_type(_field_name, field_value,
                                              base_field_type)
            _field_type.__name__ = "{}{}{}{}".format(
                DYNAMIC_CLASS_NAME_PREFIX, to_pascal_case(self.prefix),
                self.connection_field.type.__name__,
                to_pascal_case(_field_name.replace(".", "_")))
            params.update({sub_field_name: _field_type})

        return graphene.Argument(
            type(
                "{}{}{}{}".format(DYNAMIC_CLASS_NAME_PREFIX,
                                  to_pascal_case(self.prefix),
                                  self.connection_field.type.__name__,
                                  to_pascal_case(field_name.replace(".",
                                                                    "_"))),
                (graphene.InputObjectType, ),
                params,
            ))