コード例 #1
0
    def __new__(cls, name, bases, attrs):
        attrs['fields'] = {}

        # Inherit any fields from parent(s).
        try:
            parents = [b for b in bases if issubclass(b, SearchIndex)]
            # Simulate the MRO.
            parents.reverse()

            for p in parents:
                fields = getattr(p, 'fields', None)

                if fields:
                    attrs['fields'].update(fields)
        except NameError:
            pass

        # Build a dictionary of faceted fields for cross-referencing.
        facet_fields = {}

        for field_name, obj in attrs.items():
            # Only need to check the FacetFields.
            if hasattr(obj, 'facet_for'):
                if not obj.facet_for in facet_fields:
                    facet_fields[obj.facet_for] = []

                facet_fields[obj.facet_for].append(field_name)

        for field_name, obj in attrs.items():
            if isinstance(obj, SearchField):
                field = attrs.pop(field_name)
                field.set_instance_name(field_name)
                attrs['fields'][field_name] = field

                # Only check non-faceted fields for the following info.
                if not hasattr(field, 'facet_for'):
                    if field.faceted == True:
                        # If no other field is claiming this field as
                        # ``facet_for``, create a shadow ``FacetField``.
                        if not field_name in facet_fields:
                            shadow_facet_name = get_facet_field_name(
                                field_name)
                            shadow_facet_field = field.facet_class(
                                facet_for=field_name)
                            shadow_facet_field.set_instance_name(
                                shadow_facet_name)
                            attrs['fields'][
                                shadow_facet_name] = shadow_facet_field

        # Assigning default 'objects' query manager if it does not already exist
        if not attrs.has_key('objects'):
            try:
                attrs['objects'] = SearchIndexManager(
                    attrs['Meta'].index_label)
            except (KeyError, AttributeError):
                attrs['objects'] = SearchIndexManager(DEFAULT_ALIAS)

        return super(DeclarativeMetaclass,
                     cls).__new__(cls, name, bases, attrs)
コード例 #2
0
    def __new__(cls, name, bases, attrs):
        attrs['fields'] = {}

        # Inherit any fields from parent(s).
        try:
            parents = [b for b in bases if issubclass(b, SearchIndex)]
            # Simulate the MRO.
            parents.reverse()

            for p in parents:
                fields = getattr(p, 'fields', None)

                if fields:
                    attrs['fields'].update(fields)
        except NameError:
            pass

        # Build a dictionary of faceted fields for cross-referencing.
        facet_fields = {}

        for field_name, obj in attrs.items():
            # Only need to check the FacetFields.
            if hasattr(obj, 'facet_for'):
                if obj.facet_for not in facet_fields:
                    facet_fields[obj.facet_for] = []

                facet_fields[obj.facet_for].append(field_name)

        built_fields = {}

        for field_name, obj in attrs.items():
            if isinstance(obj, SearchField):  # noqa: F405
                field = attrs[field_name]
                field.set_instance_name(field_name)
                built_fields[field_name] = field

        attrs['fields'].update(built_fields)

        # Assigning default 'objects' query manager if it does not already exist
        if 'objects' not in attrs:
            try:
                attrs['objects'] = SearchIndexManager(
                    attrs['Meta'].index_label)
            except (KeyError, AttributeError):
                attrs['objects'] = SearchIndexManager(
                    DEFAULT_ALIAS)  # noqa: F405

        return type.__new__(cls, name, bases, attrs)