예제 #1
0
    def query_result_to_dict(self, obj):
        """
        Converts the result of single ``query`` result into a dictionary via the
        field specification of this entity.

        :param obj: A :ref:`declarative <sqla:declarative_toplevel>` object.
        :rtype: dict
        """
        data = {}
        for field in self.fields:
            fieldname = field.name
            tempvals = set()
            for path in field.paths:
                for value in iterate_path_values(path, obj):
                    if value is not None:
                        if isinstance(value, list):
                            tempvals.update(set(value))
                        else:
                            tempvals.add(value)
            if field.transformfunc is not None:
                tempvals = field.transformfunc(tempvals)
            if isinstance(tempvals, set) and len(tempvals) == 1:
                tempvals = tempvals.pop()
            if tempvals is not None and tempvals:
                data[fieldname] = tempvals

        if (config.CFG.getboolean("sir", "wscompat") and self.compatconverter is
            not None):
            data["_store"] = tostring(self.compatconverter(obj).to_etree())

        return data
예제 #2
0
    def query_result_to_dict(self, obj):
        """
        Converts the result of single ``query`` result into a dictionary via the
        field specification of this entity.

        :param obj: A :ref:`declarative <sqla:declarative_toplevel>` object.
        :rtype: dict
        """
        data = {}
        for field in self.fields:
            fieldname = field.name
            tempvals = set()
            for path in field.paths:
                tempvals.update(set(chain(iter
                                for iter in iterate_path_values(path, obj)
                                if iter is not None)))
            if field.transformfunc is not None:
                tempvals = field.transformfunc(tempvals)
            if isinstance(tempvals, set) and len(tempvals) == 1:
                tempvals = tempvals.pop()
            if tempvals is not None and tempvals:
                # TO-DO (samj1912): Remove this typecast once https://github.com/django-haystack/pysolr/pull/229
                # is merged.
                if isinstance(tempvals, set):
                    tempvals = list(tempvals)
                data[fieldname] = tempvals

        if (config.CFG.getboolean("sir", "wscompat") and self.compatconverter is
            not None):
            data["_store"] = tostring(self.compatconverter(obj).to_etree())

        return data
예제 #3
0
 def test_non_sqlalchemy_paths(self):
     res = list(iterate_path_values("__tablename__", self.c))
     self.assertEqual(res, [models.C.__tablename__])
예제 #4
0
 def test_many_to_one(self):
     res = list(iterate_path_values(self.b_path, self.b))
     self.assertEqual(res, [1])
예제 #5
0
 def test_attribute_without_relationship(self):
     res = list(iterate_path_values("id", self.c))
     self.assertEqual(res, [1])
예제 #6
0
 def test_one_to_many(self):
     res = list(iterate_path_values(self.c_path, self.c))
     self.assertEqual(res, [1, 2])
예제 #7
0
 def test_attribute_without_relationship(self):
     res = list(iterate_path_values("id", self.c))
     self.assertEqual(res, [1])
예제 #8
0
 def test_one_to_many(self):
     res = list(iterate_path_values(self.c_path, self.c))
     self.assertEqual(res, [1, 2])
예제 #9
0
 def test_non_sqlalchemy_paths(self):
     res = list(iterate_path_values("__tablename__", self.c))
     self.assertEqual(res, [models.C.__tablename__])
예제 #10
0
 def test_many_to_one(self):
     res = list(iterate_path_values(self.b_path, self.b))
     self.assertEqual(res, [1])