def get_codings(self): """Return a sequence of codings with pre-fetched values""" # late import to prevent cycles from amcat.models.coding.coding import CodingValue q = CodingValue.objects.filter(coding__codingjob__exact=self) q = q.select_related("field__fieldtype", "value__strval", "value__intval", "coding") q = q.order_by("coding", 'field__fieldnr') # possible optimzation: use running values list because of sort order values_per_coding = {} # coding : [(field, value), ...] for val in q: values_per_coding.setdefault(val.coding, []).append((val.field, val.value)) for coding, values in values_per_coding.iteritems(): set_cache(coding, coding.get_values.__name__, values) yield coding
def test_set_cache(self): """Can we manually set the cache?""" t = TestCaching.TestClass(x=4, y=1) # Are set values used? set_cache(t, "xprop", 99) self.assertEqual(t.xprop, 99) self.assertFalse(t.changed) # Does reset bring back real value? reset(t) self.assertEqual(t.xprop, 4) self.assertTrue(t.changed) # Same for named prop t.changed = False set_cache(t, t.get_y.__name__, 7, "y_cache") self.assertEqual(t.get_y(), 7) self.assertFalse(t.changed) reset(t, "y_cache") self.assertEqual(t.get_y(), 1) self.assertTrue(t.changed)