Example #1
0
def test_make_dsl_class():
    XY = utils._make_dsl_class(object, 'X', suffix='Y')

    assert XY.__name__ == 'XY'
    assert XY.__bases__ == (object, )
def test_make_dsl_class():
    XY = utils._make_dsl_class(object, 'X', suffix='Y')

    assert XY.__name__ == 'XY'
    assert XY.__bases__ == (object, )
    This wraps a field so that when get_from_instance is called, the field's
    values are iterated over
    """
    # alter the original field's get_from_instance so it iterates over the
    # values that the field's get_from_instance() method returns
    original_get_from_instance = field.get_from_instance

    def get_from_instance(self, instance):
        for value in original_get_from_instance(instance):
            yield value

    field.get_from_instance = MethodType(get_from_instance, field)

    return field


class TemplateField(StringField):
    def __init__(self, template_name, **kwargs):
        self._template_name = template_name
        super().__init__(**kwargs)

    def get_from_instance(self, instance):
        context = {'object': instance}
        return render_to_string(self._template_name, context)


# take all the basic fields from elasticsearch-dsl, and make them subclass EMField
for f in FIELDS:
    fclass = _make_dsl_class(EMField, f, suffix="Field")
    globals()[fclass.__name__] = fclass
Example #4
0
    This wraps a field so that when get_from_instance is called, the field's
    values are iterated over
    """
    # alter the original field's get_from_instance so it iterates over the
    # values that the field's get_from_instance() method returns
    original_get_from_instance = field.get_from_instance

    def get_from_instance(self, instance):
        for value in original_get_from_instance(instance):
            yield value

    field.get_from_instance = MethodType(get_from_instance, field)

    return field


class TemplateField(StringField):
    def __init__(self, template_name, **kwargs):
        self._template_name = template_name
        super().__init__(**kwargs)

    def get_from_instance(self, instance):
        context = {'object': instance}
        return render_to_string(self._template_name, context)


# take all the basic fields from elasticsearch-dsl, and make them subclass EMField
for f in FIELDS:
    fclass = _make_dsl_class(EMField, f, suffix="Field")
    globals()[fclass.__name__] = fclass
def test_make_dsl_class():
    t = utils._make_dsl_class(object, 'X')

    assert t.__name__ == 'X'