示例#1
0
 def get_value(self, instance):
     """
     Given an instance (i.e. any object containing data), extract the field of data to be
     displayed in a cell of this column
     """
     if callable(self.accessor):
         return self.accessor(instance)
     else:
         return multigetattr(instance, self.accessor)
示例#2
0
文件: mixins.py 项目: tnir/wagtail
 def to_row_dict(self, item):
     """Returns an OrderedDict (in the order given by list_export) of the exportable information for a model instance"""
     row_dict = OrderedDict(
         (field, multigetattr(item, field)) for field in self.list_export
     )
     return row_dict
示例#3
0
 def get_link_url(self, instance, parent_context):
     if self._get_url_func:
         return self._get_url_func(instance)
     else:
         id = multigetattr(instance, self.id_accessor)
         return reverse(self.url_name, args=(quote(id), ))
示例#4
0
    def test_multigetattr(self):
        self.assertEqual(multigetattr(self.thing, "colour"), "green")
        self.assertEqual(multigetattr(self, "thing.colour"), "green")
        self.assertEqual(multigetattr(self.thing, "limbs.arms"), 2)
        self.assertEqual(multigetattr(self.thing, "speak"), "raaargh")
        self.assertEqual(multigetattr(self, "thing.speak.0"), "r")

        with self.assertRaises(AttributeError):
            multigetattr(self.thing, "name")

        with self.assertRaises(AttributeError):
            multigetattr(self.thing, "limbs.antennae")

        with self.assertRaises(AttributeError):
            multigetattr(self.thing, "speak.999")

        with self.assertRaises(TypeError):
            multigetattr(self.thing, "feed")

        with self.assertRaises(SuspiciousOperation):
            multigetattr(self.thing, "poke")
        self.assertFalse(self.thing.poke_was_called)